[erlang-questions] beginning erlang/OTP - spawning gen_servers and supervisors

Bernard Duggan bernie@REDACTED
Sun May 9 13:40:39 CEST 2010


On 9/05/2010 2:08 PM, AJ Heller wrote:
> Don't you find it odd, then, that a gen_server can be spawned the same
> way, and it continues to run after its parent process exits?
>   
    It would be odd had Geoff's description been completely correct, but
it was wrong in one crucial point: Linked processes don't automatically
die when a process they are linked to exits with code 'normal'.  That's
why echo_sup doesn't exit when the spawning process exits.  So why does
the supervisor die?  The internals of the supervisor module are in fact
themselves implemented as a gen_server, but with process_flag(trap_exit,
true) set.  The result of this is that when the parent process dies,
terminate() gets called (which doesn't happen when trap_exit is
disabled) and the supervisor shuts down.  It makes sense in the context
of a supervisor, since a supervisor is spawned by its parent in a
supervision tree - if it didn't die whenever its parent shutdown,
whatever the reason, you'd have dangling "branches" of the tree.

    For what it's worth, I've been writing erlang for a couple of years
now and am still only just getting completely to grips with the process
shutdown and trap_exit semantics, particularly as they relate to
gen_servers and supervisors.  It all works great once you figure it out,
but it's not exactly intuitive (which isn't meant as a criticisim -
everything makes perfect sense from a design perspective once you puzzle
it all out).

Cheers,

Bernard


More information about the erlang-questions mailing list