[erlang-questions] Right way to terminate process chain

Jesper Louis Andersen jesper.louis.andersen@REDACTED
Sun Feb 7 15:52:44 CET 2016

On Sun, Feb 7, 2016 at 2:31 PM, Alexander Petrovsky <askjuise@REDACTED>

> I have only two ideas:
> - trap_exit, catch EXIT message and terminate
> - call gen_server(XPid|YPid|ZPid) from terminate callback in every
> process, but it causes 'little temporary' deadlock.

When you have a simple way to close down the process chain, you can often
just terminate it gracefully and message the others you are done. If you
terminate abnormally, the links will make sure to reap the process tree.

When you cannot close down the process chain in this way easily though,
there are monitors. Call erlang:monitor(process, Pid) and handle the 'DOWN'
message which arrives in your inbox. Monitors are unidirectional, so you
will have to monitor those processes you need to watch out for
individually. But this is the common way to handle termination by means of
an async exception. Also, note the semantics of monitors if the Pid you
monitor isn't there. And that you can call erlang:demonitor(MonitorRef,
[flush]) to linearize monitor messages in your inbox. All in all the API is
nice to work with in a safe way with a bit of attention.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20160207/cc3ecb16/attachment.htm>

More information about the erlang-questions mailing list