Linking a port / exit normal in case of kill -X <os_pid>

erlang
port
Tags: #<Tag:0x00007fbcb608e038> #<Tag:0x00007fbcb608de80>

#1

Hi there,
I want to open a os process from elixir and get informed in case it crashes. I tried it with port and link but when I kill the external os process the exit always reports normal.
I was expecting to get something else then normal when it exits abnormal like in kill -9 or kill -15.

Since I am actually using elixir 1.5 my test code looks like:

       :erlang.process_flag(:trap_exit, true)
        port = Port.open({:spawn, "sleep 360"}, [:exit_status])
        Process.link port
        :erlang.monitor :port, port

And I get the following:

{#Port<0.6363>, {:exit_status, 137}}
{:EXIT, #Port<0.6363>, :normal}
{:DOWN, #Reference<0.3242658756.1180172289.143115>, :port, #Port<0.6363>, :normal}

The only thing I could use as a workaround is the exit status as it looks like.

Does one of you have a good idea how to get link working for this case?

Is this a bug or a feature?

Communication with os processes, killing them or sending signals is quite tricky anyway in erlang…


#2

Everything works as expected.

When you kill the process in unix, its exit status is set to non-zero value, depending on the signal you’ve sent (128 + signal number, so in your case it is 128+9=137).


#3

I was expecting exit beeing not normal when the process got killed or died unexpectedly so a link would be usefull.
Why is the exit normal in such cases?


#4

I think you are missing the difference between OS and Erlang processes.

This is an exit of the erlang process, not the unix one. Port.open creates an erlang process, which runs the external command and waits for it to end. From the erlang point of view, the port process finished normally - it did the job.


#5

I get the difference but why should elixir assume it did the job/its a normal exit if the process was e.g. segfaulting. This does not make much sense to me


#6

It’s not possible to know it segfaulted, and a return code from an application may not actually be an error but could be returning valid information that is actually wanted (this is done quite commonly), only the user can know.


#7

I was expecting those cases would also get reflected in the exit beeing not normal so link would make sense.

grych

    March 12

Everything works as expected.

DEvil0000:
{#Port<0.6363>, {:exit_status, 137}}

When you kill the process in unix, its exit status is set to non-zero value, depending on the signal you’ve sent (128 + signal number, so in your case it is 128+9=137).