Skip to content
This repository was archived by the owner on Nov 17, 2020. It is now read-only.

Commit 9c34251

Browse files
committed
Introduce supervisor2 prep_stop callback
Allows to stop dependencies on supervisor shutdown when it reaches the max restart intensity
1 parent 18580f7 commit 9c34251

File tree

1 file changed

+18
-1
lines changed

1 file changed

+18
-1
lines changed

src/supervisor2.erl

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,8 @@
142142
[ChildSpec :: child_spec()]}}
143143
| ignore.
144144

145+
-callback prep_stop() -> ok.
146+
145147
-define(restarting(_Pid_), {restarting,_Pid_}).
146148

147149
%%% ---------------------------------------------------
@@ -629,6 +631,7 @@ handle_info({'EXIT', Pid, Reason}, State) ->
629631
{ok, State1} ->
630632
{noreply, State1};
631633
{shutdown, State1} ->
634+
prep_stop(State1),
632635
{stop, shutdown, State1}
633636
end;
634637

@@ -801,7 +804,8 @@ restart_child(Pid, Reason, State) ->
801804
try_restart(RestartType, Reason, Child, State) ->
802805
case handle_restart(RestartType, Reason, Child, State) of
803806
{ok, NState} -> {noreply, NState};
804-
{shutdown, State2} -> {stop, shutdown, State2}
807+
{shutdown, State2} -> prep_stop(State2),
808+
{stop, shutdown, State2}
805809
end.
806810

807811
do_restart(RestartType, Reason, Child, State) ->
@@ -1504,3 +1508,16 @@ report_progress(Child, SupName) ->
15041508
Progress = [{supervisor, SupName},
15051509
{started, extract_child(Child)}],
15061510
error_logger:info_report(progress, Progress).
1511+
1512+
prep_stop(#state{module = Mod}) ->
1513+
%% Catch any exception - including non-existing prep_stop -
1514+
%% and continue stopping the supervision tree.
1515+
%% This is only executed when children are terminating,
1516+
%% because any other call from a top supervisor or application
1517+
%% will cause a deadlock stopping applications within prep_stop.
1518+
try
1519+
Mod:prep_stop()
1520+
catch
1521+
_:_ ->
1522+
ok
1523+
end.

0 commit comments

Comments
 (0)