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

Dmitry Kolesnikov <>
Wed Sep 14 19:20:06 CEST 2016


Hello,

This is a good question! I’d like to get other's opinion on the subject as well. 
I would go with following pattern:

* S_X is {one_for_all, 0, 1} and all its child are permanent. 
* The process P_2 just {stop, normal, State} when the job is done.

I do not like to “leak” a knowledge of supervisor to child processes. I’ll try to avoid usage of supervisor:terminate_child(…). On another hand, this pattern has disadvantage. You’ll see a “supervisor” S_X crash in the log when P_2 stops due to “permanent” property.     

The usage of simple_one_to_one supervisor seems to be right for this type of use-cases but it misses concept of related processes. 

What do you think?

Best Regards, 
Dmitry 



> On Sep 14, 2016, at 11:42 AM, Karolis Petrauskas <> wrote:
> 
> 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
> _______________________________________________
> erlang-questions mailing list
> 
> http://erlang.org/mailman/listinfo/erlang-questions



More information about the erlang-questions mailing list