[erlang-questions] supervisors, gen_server:terminate/2, and trap_exit

Fred Hebert <>
Thu Mar 12 14:45:18 CET 2015


On 03/12, Imants Cekusins wrote:
> so in the context of gs G, started in many possible ways: by a
> supervisor, by a standalone process, by an OTP process, via gs:start,
> or gs:start_link, the cases when G process ends yet its terminate is
> not called, are:
> 
> 1) when another process calls exit(G, kill) (can only sup try this, or
> any parent, or any process even?);
> 

Any process can try it. trap_exit can also capture exit/2 calls made
with any argument *but* 'kill'.

I think most of the questions you ask come from not properly
understanding the signals in Erlang, and how trapping exits affects
them. I recommend reading possibly
http://learnyousomeerlang.com/errors-and-processes for this.

> 2) when G spawns a non-OTP process C, links to it, does not trap
> exits, C dies, brings G down with it, terminate is not called;
> 

That the process is OTP or not has *nothing* to do with signals and
trapping exits. Those are *primitives* of Erlang, and again, OTP is a
framework built on top of them. The true underlying question under all
of these is "can a process know it receives a signal or is it being
killed first".

The answer is the process is always killed first if it's not trapping
exits. If it's trapping exits, the signal is converted to an {'EXIT',
Pid, Reason} message, *except* when the reason is 'kill'. A signal can
be sent implicitly by a linked dying process, or by calling exit/2 on a
process.


More information about the erlang-questions mailing list