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

Youngkin, Rich richard.youngkin@REDACTED
Wed Mar 25 05:53:58 CET 2015

Regarding how to ensure gen_server:terminate/2 is called...

Maybe I'm being too pedantic (big word, sorry!), but the official
documentation still confuses me.  I created/ran several tests to better
understand what actually is going on. LYSE says it best IMO, and using
smaller words and shorter sentences (always good for me :>). My remaining
comments are included in-line below.

On Wed, Mar 11, 2015 at 4:20 PM, Fred Hebert <mononcqc@REDACTED> 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.

The Erlang documentation on Supervisors leads me to believe that no special
treatment is needed in order to for terminate/2 to be called, i.e.,
"...automatically adhere to the shutdown protocol". That said, I should
have looked at the documentation on the "shutdown protocol". More below...

> >
> > 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."

In my tests I didn't have to handle(_info) {'EXIT', ParentPid, shutdown).
terminate/2 just gets called as a result of trapping exits when the
supervisor directs the child to terminate (i.e., when the application is
shutting down). So terminate/2 gets called, with reason "shutdown", not
handle_info({'EXIT', ParentPid, shutdown}). But maybe... {'EXIT',
ParentPid, shutdown} would be sent if the supervisor failed??? I didn't
test that, but supervisors don't fail, right?

> 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.

WRT to trap exit, yes.  WRT "The child process should handle the {'EXIT',
ParentPid, shutdown} message...", no.

> Regards,
> Fred.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20150324/d03dcc78/attachment.htm>

More information about the erlang-questions mailing list