[erlang-questions] simple_one_for_one supervisor - what happens at restart? (also: gen_tcp)

Jesper Louis Andersen <>
Sat May 14 12:48:24 CEST 2016


On Sat, May 14, 2016 at 9:46 AM, Chandru <
> wrote:

> Cleanup can be done in the terminate callback if you use
> process_flag(trap_exit, true).


There is a subtlety here which, in some circumstances, creates trouble and
one has to be aware of. This is in addition to what Loïc mentions.

Observation: In many systems, processes have dependencies and needs each
other to operate. Nice cleanup cannot be done in the situation where your
immediate dependencies are gone, but under a controlled shutdown, you can
arrange the shutdown order such that the shutdown is graceful.

Example: You are processing some kind of request in a web server. You
decide to close down the application. Now, what you want to happen is a
graceful shutdown: you stop accepting new requests, but you run the current
requests to an end (up to some timeout). In settings where the server count
is very dynamic and servers can get "elastically" added or removed, this is
important because otherwise you would be losing requests under shutdown.

Observation: processes under supervision have an ordering imposed on them.
Their termination happens in the opposite order of spawning. This can be
used to enforce the gracefulness constraint.

Another trick, which works cross-application, is to use the
Module:prep_stop/1 phase of termination to tell your other applications
(ranch, cowboy, yaws) that they should start graceful termination of their
workers. Once completed, you can stop your own supervision tree.

"Naked" processes which are not linked into a supervision tree can
terminate in any order they see fit. In particular, the Erlang VM regards
itself as being "done" once all its applications are shut down. In
particular a line process with process_flag(trap_exit, true) waiting on
disk is not going to be allowed to hold up termination of the VM. A naked
process has its use, but one has to be extremely careful around them. If
they start failing, you have no easy way to know what their relation is.

-- 
J.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20160514/1f29108a/attachment.html>


More information about the erlang-questions mailing list