3
votes

I have a question about timing of monitored/linked process dying, and I can't think of how to test it in practice. Here is the scenario I am worried about.

Let's say I have a process called master and slave.

  1. master sets trap_exit to true.
  2. master does {ok, Pid} = slave:start_link() thus linking the two.
  3. master does the equivalent of gen_server:call(Pid, Msg).
  4. Before the message arrives at Pid that process crashes.

Question:

  1. Will master receive an EXIT message first? or
  2. Will master fail with {noproc,{gen_server.. exception since Pid is already dead?
1

1 Answers

2
votes

Not a proofed answer but here is what might happen.

First thing we should understand is that EXIT is a message that is being sent to the masters mailbox. In contrast to the noproc error, which is an error that is being generated within the master process context (not a message).
This means that you will probably get both. The question is who will come first.

Since the EXIT message is being sent immediately and since you said that the process crashes before the call arrived, it makes sense that you will first get the EXIT message. Although, since they are independent, there is an option that before the EXIT message arrives to the masters mailbox, your gen_server:call will return with a noproc error.

In case you are asking this for your implementation purpose, I would suggest covering both cases. I really don't think that Erlang promises which one will be first.