[erlang-questions] excute code in the supervisor when a child crashes?

Pablo Platt pablo.platt@REDACTED
Mon Oct 19 19:39:30 CEST 2009

Sounds like a good solution.
I'll create a gen_server that will call supervisor:start_child
then it'll monitor the pid and store it in an ETS so other clients will be able to send messags to the child.
If the child exists or crashes the monitoring gen_server will delete the ETS record of this process.

The complexity is if the monitoring process dies.
In this case it needs to scan the whole ETS table, re-monitor all live process and delete records of all the dead processes.

From: Dave Smith <dizzyd@REDACTED>
To: Pablo Platt <pablo.platt@REDACTED>
Cc: erlang-questions@REDACTED
Sent: Mon, October 19, 2009 7:22:41 PM
Subject: Re: [erlang-questions] excute code in the supervisor when a child  crashes?

You won't be able to do this sort of accounting work in a supervisor.
Typically this problem is solved by having a secondary process that
acts as an entry point to the supervisor (i.e. calls
supervisor:start_child) and maintains monitors on all processes. This
secondary process then does the necessary accounting work when the
child process exits.

The rationale for this approach is supervisors are system-critical
code and as such you really, really, REALLY don't want to introduce
bugs into them.  Processes and monitors are cheap (enough) in Erlang
that a secondary accounting process is a reasonable solution and lets
us avoid introducing new code into the supervision system.

Hope that helps...


On Mon, Oct 19, 2009 at 10:12 AM, Pablo Platt <pablo.platt@REDACTED> wrote:
> Hi,
> I have a supervisor with simple_one_for_one.
> When I start children with supervisor:start_child I save the child pid in an ETS table
> so I'll be able to exchange messages between all the child processes.
> When a child crashes I need to delete his pid from the ETS table or it'll grow constantly and eat all my memory.
> Is it possible to execute code when a child crashes or terminates?
> Thanks


More information about the erlang-questions mailing list