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

Youngkin, Rich <>
Thu Mar 12 14:41:25 CET 2015


On Wed, Mar 11, 2015 at 5:20 PM, Fred Hebert <> wrote:

> On 03/11, Youngkin, Rich wrote:
> > "terminate/2 will also be called when its parent (the process that
> spawned
> > it) dies, if and only if the gen_server is trapping exits."
> >
> > [...]
> >
> > The Erlang documentation on Supervisors states "Note that all child
> > processes implemented using the standard OTP behavior modules
> automatically
> > adhere to the shutdown protocol.". I'm having trouble reconciling these
> > LYSE with the Erlang Supervisor docs.
> >
> > Thanks,
> > Rich
>
> The references I found to the shutdown protocol is:
>
>
> http://www.erlang.org/documentation/doc-4.9.1/doc/design_principles/sup_princ.html#shutdown
>
> "The supervisor process sends an exit signal to the child process and
> waits for acknowledgment. The child process should handle the
> {'EXIT',ParentPid,shutdown} message and terminate with reason shutdown
> if it traps exit signals."
>

> The last line here is important -- if it traps exit signals. This is in
> agreement with what LYSE mentions. If you don't trap exit signals, you
> can't get the signal, it just kills the process directly.
>

So let's say that:
  1. Supervisor1 starts Supervisor2 and GenServerA in that order.
  2. A Supervisor1 is given a child_spec with a simple_one_for_one restart
strategy.
  3. GenServerA implements a start/0 API function that results in calling
supervisor:start_child(Supervisor1, [SomeArgs]) that creates ChildZ.
  4. ChildZ's init/1 function does not trap_exits.
  4. GenServerA also implements a stop/0 API function that results in
calling supervisor:terminate_child(Supervisor1, ChildZPid)

At step 4, will ChildZ:terminate/2 get called? Or does it have to
trap_exit? If it's the latter AND ChildZ doesn't trap_exit then it seems as
if ChildZ would have to implement handle_info('EXIT', ParentPid, shutdown)
which could then call terminate/2?

Thanks,
Rich
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20150312/66d41374/attachment.html>


More information about the erlang-questions mailing list