<p>From my understanding, permanent and temporary processes should be under different supervisors, generally the temp supervisor under the perm supervisor.</p>
<div class="gmail_quote">On Aug 3, 2011 6:21 AM, "Frédéric Trottier-Hébert" <<a href="mailto:fred.hebert@erlang-solutions.com">fred.hebert@erlang-solutions.com</a>> wrote:<br type="attribution">> <br>> <br>
> On 2011-08-02, at 21:13 PM, Sam Bobroff wrote:<br>> <br>>> Hello Erlangers,<br>>> <br>>> I've recently tracked down a bug in some code that seems to be caused by a problem with the supervisor module. What seems to be happening is that when a temporary child has been added to a one_for_all supervisor using start_child, if a permanent child of that supervisor exits then if that permanent child exits, the supervisor attempts to restart the temporary child (which seems counter to the documentation).<br>
> <br>> That one is fine. The 'one_for_all' is about each process supervised. In that case, the permanent child means 'when I crash, get the one_for_all strategy'. That strategy is applied no matter what the other kinds of children are. On the other hand, in that same supervisor, if the temporary child dies, then the permanent one won't be restarted because that restart strategy means 'in my case, I'm not that vital and you can ignore me dying'. As far as I know, this is expected behaviour.<br>
> <br>> <br>>> In addition the MFA for the child has been replaced with undefined and start attempt crashes, causing the supervisor to continue attempting restarts until it reaches it's restart intensity and shuts down.<br>
>> <br>>> This does not happen if the temporary child is added to the supervisor as part of the supervisor's initial spec. In this case the temporary child is restart but it's MFA is present so it is able to restart it successfully, although it still seems contrary to the documentation that the temporary child is restarted at all, although it depends on which section of the documentation you regard as more important (from the supervisor documentation):<br>
> <br>> Ah, that one gets trickier. If I take a look at the source, the issue seems to be in the supervisor's 'save_child' function. This one has this comment:<br>> <br>> %% Note we do not want to save the parameter list for temporary processes as<br>
> %% they will not be restarted, and hence we do not need this information.<br>> %% Especially for dynamic children to simple_one_for_one supervisors<br>> %% it could become very costly as it is not uncommon to spawn<br>
> %% very many such processes.<br>> <br>> So the arguments that you pass to 'proc_lib:start_link' get dropped. This check DOES NOT happen when the Child Spec is part of the initial supervisor specification.<br>
> <br>> One issue here is that we have two different ways of starting temporary children. The real issue however, seems to be that we assume that temporary processes will NOT be restarted, while the supervisor does attempt to do so.<br>
> <br>>> <br>>> <br>>> one_for_all - if one child process terminates and should be restarted, all other child processes are terminated and then all child processes are restarted.<br>>> <br>>> This is quite clear: "all child processes are restarted". But later:<br>
>> <br>>> <br>>> Restart defines when a terminated child process should be restarted. A permanent child process should always be restarted, a temporary child process should never be restarted and a transient child process should be restarted only if it terminates abnormally, i.e. with another exit reason than normal.<br>
>> <br>>> <br>>> <br>>> Again clear but conflicting: "a temporary child process should never be restarted".<br>> <br>> These rules are about the processes themselves as far as I know. It's well possible to expect a transient process to be restarted if it depends on a permanent one that crashed. I'm not sure I get why this would not be the case with a temporary child.<br>
>> <br>>> I found a similar issue mentioned in a bug report for R14B02, here:<br>>> <br>>> <a href="http://erlang.org/pipermail/erlang-bugs/2011-March/002273.html">http://erlang.org/pipermail/erlang-bugs/2011-March/002273.html</a><br>
> <br>> This is an interesting entry because it does suggest a fix that's still there and helps with the issue, but only when the process that crashed was the one we were looking for. The function now looks like this:<br>
> <br>> del_child(Name, [Ch|Chs]) when Ch#<a href="http://child.name">child.name</a> =:= Name, Ch#child.restart_type =:= temporary -><br>>     Chs;<br>> del_child(Name, [Ch|Chs]) when Ch#<a href="http://child.name">child.name</a> =:= Name -><br>
>     [Ch#child{pid = undefined} | Chs];<br>> del_child(Pid, [Ch|Chs]) when Ch#child.pid =:= Pid, Ch#child.restart_type =:= temporary -><br>>     Chs;<br>> del_child(Pid, [Ch|Chs]) when Ch#child.pid =:= Pid -><br>
>     [Ch#child{pid = undefined} | Chs];<br>> del_child(Name, [Ch|Chs]) -><br>>     [Ch|del_child(Name, Chs)];<br>> del_child(_, []) -><br>>     [].<br>> <br>> The issue there is that we're still giving temporary children in the list of processes to be restarted if they were not the ones to crash (Name/Pid). Then the sequent calls to 'terminate_children' and 'do_terminate_children' as part of the restart procedure also never clear these up, and the 'start_children' function doesn't do it either.<br>
> <br>> In normal cases, it seems the supervisor will use state_del_child as a function to do it, which doesn't happen here (we're using del_child). <br>> ---<br>> <br>> If anyone from the OTP team is looking, it'd be nice to have a confirm/deny on that explanation. I've tried a small patch, but haven't tested it in the large or recompiled anything:<br>
> <br>> 737a738,740<br>>> terminate_children([Child = #child{restart_type=temporary} | Children], SupName, Res) -><br>>>     do_terminate(Child, SupName),<br>>>     terminate_children(Children, SupName, Res);<br>
> <br>> Just adding these lines to R14B03 seems to resolve the issue by avoiding to restart the child in the first place.<br>> I can make a standard patch and bug submission if required/appreciated.<br>> <br>> --<br>
> Fred Hébert<br>> <a href="http://www.erlang-solutions.com">http://www.erlang-solutions.com</a><br>> <br></div>