6

What happens when the process which owns a spawned port dies and is restarted by the supervisor?

  1. Is there a way for the old port not to die with its owner and for the new owner to "take over"?

  2. Failing that, is it possible to ensure that the spawned process terminates when its port dies?

Alexey Romanov
  • 167,066
  • 35
  • 309
  • 487

1 Answers1

5

First, notice that you don't want the port owner to die. So move any "dangerous" code out to another process and make the port owner as dumb as possible. This is error-mitigation the Erlang way. Now, if that process dies, something is really bad, so in that case it may be clever to recycle the port as well. But since we moved everything out, we are counting on that not happening.

Regarding 2, the port will send a specific message when it terminates, so you can arrange for your spawned process to gracefully detect this and terminate with it. See

http://www.erlang.org/doc/reference_manual/ports.html

I GIVE CRAP ANSWERS
  • 18,739
  • 3
  • 42
  • 47
  • I don't think the port sends a special message to the spawned process, and I don't see this in the documentation you link to. What will happen is that the pipe to the process is closed and this you can catch in the process itself (e.g. lookout for eof on your input) – Peer Stritzinger Nov 09 '10 at 07:17
  • But have also to say that I absolutly agree with your first paragraph – Peer Stritzinger Nov 09 '10 at 07:18
  • I am sorry, but you are wrong. Table 14.3 lists as the last message exit reasons. You can also call link/1 on a port to link to it if you will. Also, notice that to get this message, your process must be trapping exit signals via process_flag/2, or otherwise it wont work. – I GIVE CRAP ANSWERS Nov 10 '10 at 01:25
  • I think you are talking of different things here. Peer is describing how the external port process sees that the port has been closed (eof on its input pipe) while NOT CRAPPY ANSWERS is describing the how the connected erlang process sees it. – rvirding Nov 10 '10 at 08:19