mirror of
https://github.com/ninenines/cowboy.git
synced 2025-07-15 04:30:25 +00:00
Properly clean up timers when terminating
This commit is contained in:
parent
a2facaf2da
commit
daef32d907
1 changed files with 20 additions and 5 deletions
|
@ -99,9 +99,16 @@ shutdown_timeout(Children, Ref, Pid) ->
|
||||||
|
|
||||||
-spec terminate(children()) -> ok.
|
-spec terminate(children()) -> ok.
|
||||||
terminate(Children) ->
|
terminate(Children) ->
|
||||||
%% Ask all children to shutdown first.
|
%% For each child, either ask for it to shut down,
|
||||||
_ = [exit(Pid, shutdown) || #child{pid=Pid} <- Children],
|
%% or cancel its shutdown timer if it already is.
|
||||||
%% Loop until that time or until all children are dead.
|
%%
|
||||||
|
%% We do not need to flush stray timeout messages out because
|
||||||
|
%% we are either terminating or switching protocols,
|
||||||
|
%% and in the latter case we flush all messages.
|
||||||
|
_ = [case TRef of
|
||||||
|
undefined -> exit(Pid, shutdown);
|
||||||
|
_ -> erlang:cancel_timer(TRef, [{async, true}, {info, false}])
|
||||||
|
end || #child{pid=Pid, timer=TRef} <- Children],
|
||||||
before_terminate_loop(Children).
|
before_terminate_loop(Children).
|
||||||
|
|
||||||
before_terminate_loop([]) ->
|
before_terminate_loop([]) ->
|
||||||
|
@ -115,10 +122,18 @@ before_terminate_loop(Children) ->
|
||||||
infinity -> undefined;
|
infinity -> undefined;
|
||||||
_ -> erlang:start_timer(Time, self(), terminate)
|
_ -> erlang:start_timer(Time, self(), terminate)
|
||||||
end,
|
end,
|
||||||
|
%% Loop until that time or until all children are dead.
|
||||||
terminate_loop(Children, TRef).
|
terminate_loop(Children, TRef).
|
||||||
|
|
||||||
terminate_loop([], _) ->
|
terminate_loop([], TRef) ->
|
||||||
|
%% Don't forget to cancel the timer, if any!
|
||||||
|
case TRef of
|
||||||
|
undefined ->
|
||||||
ok;
|
ok;
|
||||||
|
_ ->
|
||||||
|
_ = erlang:cancel_timer(TRef, [{async, true}, {info, false}]),
|
||||||
|
ok
|
||||||
|
end;
|
||||||
terminate_loop(Children, TRef) ->
|
terminate_loop(Children, TRef) ->
|
||||||
receive
|
receive
|
||||||
{'EXIT', Pid, _} when TRef =:= undefined ->
|
{'EXIT', Pid, _} when TRef =:= undefined ->
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue