[erlang-questions] Stopping a process with its supervision subtree

Karolis Petrauskas <>
Wed Sep 14 10:42:45 CEST 2016


Hello,

In my application I have a processes (lets say P_X), that are started
by starting a dedicated supervisor (S_X), which starts the process P_X
and several related processes as its children. The S_X supervisors are
started under the one_for_one supervisor (say MainSup). The
supervision tree schematically looks as the following for two P_X
started, when X = 1..2.

    MainSup
      * S_1
          * P_1
          * Other_1
      * S_2
          * P_2
          * Other_2

I need to stop the entire supervisor S_X when the process P_X is done
with its job.

Is it OK to call supervisor:stop_child(MainSup, X) from P_X process? I
experimented with that and it works. Just I'm now not sure, if this is
a correct way for doing that. The following is an excerpt from the
P_X:

    handle_info(deadline, State = #state{x = X}) ->
        ok = supervisor:terminate_child(MainSup, X),
        io:format("Stopped ~p~n", [X]),
        {stop, normal, State};

The last two lines are not executed, as the supervisor:terminate_child
is synchronous and returns only after the P_X is terminated.

Another option, as I understand, would be to start a process above the
MainSup and monitor all P_X processes and then stop the corresponding
supervisors S_X when the DOWN messages are received. Although I would
like to avoid creating such processes as well as to implement own
supervisor.

Karolis


More information about the erlang-questions mailing list