Reputation: 2904
I usually see examples of process monitoring where the code for handling a monitored process exit is the following:
handle_info({:DOWN, ref, :process, pid}, state)
But I have also seen examples where they are matching an :EXIT
instead of a :DOWN
message.
So far, I have only been able to trigger :DOWN
messages in my own examples, which includes standard Process.exit
and GenServer.stop
messages, as well as raising an exception in the monitored process.
When will I receive an :EXIT
message?
Upvotes: 3
Views: 3209
Reputation: 222128
:EXIT
is sent to the process that another process tried to exit using Process.exit
(with a reason other than :kill
) but the process was trapping exits. :DOWN
is sent to a process that is monitoring another process and the monitored process exits for any reason.
Here's an example of both:
pid = spawn(fn ->
Process.flag(:trap_exit, true)
receive do
x -> IO.inspect {:child, x}
end
end)
Process.monitor(pid)
Process.sleep(500)
Process.exit(pid, :normal)
Process.sleep(500)
# A process cannot trap `:kill`; it _has_ to exit.
Process.exit(pid, :kill)
receive do
x -> IO.inspect {:parent, x}
end
Output:
{:child, {:EXIT, #PID<0.70.0>, :normal}}
{:parent, {:DOWN, #Reference<0.0.8.223>, :process, #PID<0.73.0>, :normal}}
Upvotes: 15