fix(terminator): ensure erl_signal_server init:stop

ensure init:stop can be triggered by kill signal even before the
terminator is ready
This commit is contained in:
Zaiming Shi 2021-08-06 11:02:52 +02:00
parent 304b322a0c
commit 75f9741d75
1 changed files with 19 additions and 9 deletions

View File

@ -45,7 +45,17 @@ is_running() -> is_pid(whereis(?TERMINATOR)).
%% @doc Send a signal to activate the terminator. %% @doc Send a signal to activate the terminator.
graceful() -> graceful() ->
?TERMINATOR ! ?DO_IT, try
_ = gen_server:call(?TERMINATOR, ?DO_IT, infinity)
catch
_ : _ ->
%% failed to notify terminator, probably due to not started yet
%% or node is going down, either case, the caller
%% should issue a shutdown to be sure
%% NOTE: not exit_loop here because we do not want to
%% block erl_signal_server
init:stop()
end,
ok. ok.
%% @doc Shutdown the Erlang VM and wait until the terminator dies or the VM dies. %% @doc Shutdown the Erlang VM and wait until the terminator dies or the VM dies.
@ -72,7 +82,13 @@ init(_) ->
ok = emqx_machine_signal_handler:start(), ok = emqx_machine_signal_handler:start(),
{ok, #{}}. {ok, #{}}.
handle_info(?DO_IT, State) -> handle_info(_, State) ->
{noreply, State}.
handle_cast(_Cast, State) ->
{noreply, State}.
handle_call(?DO_IT, _From, State) ->
try try
emqx_machine:stop_apps(normal) emqx_machine:stop_apps(normal)
catch catch
@ -87,13 +103,7 @@ handle_info(?DO_IT, State) ->
after after
init:stop() init:stop()
end, end,
{noreply, State}; {reply, ok, State};
handle_info(_, State) ->
{noreply, State}.
handle_cast(_Cast, State) ->
{noreply, State}.
handle_call(_Call, _From, State) -> handle_call(_Call, _From, State) ->
{noreply, State}. {noreply, State}.