Explorar el Código

fix(machine terminator): call `do_stop_apps/0` directly

Thales Macedo Garitezi hace 1 año
padre
commit
cea22e8697

+ 19 - 8
apps/emqx_machine/src/emqx_machine_app_booter.erl

@@ -39,6 +39,9 @@
     handle_info/2
     handle_info/2
 ]).
 ]).
 
 
+%% Internal exports (for `emqx_machine_terminator' only)
+-export([do_stop_apps/0]).
+
 %%------------------------------------------------------------------------------
 %%------------------------------------------------------------------------------
 %% Type declarations
 %% Type declarations
 %%------------------------------------------------------------------------------
 %%------------------------------------------------------------------------------
@@ -77,7 +80,7 @@ handle_call(#start_apps{}, _From, State) ->
     handle_start_apps(),
     handle_start_apps(),
     {reply, ok, State};
     {reply, ok, State};
 handle_call(#stop_apps{}, _From, State) ->
 handle_call(#stop_apps{}, _From, State) ->
-    handle_stop_apps(),
+    do_stop_apps(),
     {reply, ok, State};
     {reply, ok, State};
 handle_call(_Call, _From, State) ->
 handle_call(_Call, _From, State) ->
     {reply, ignored, State}.
     {reply, ignored, State}.
@@ -89,21 +92,29 @@ handle_info(_Info, State) ->
     {noreply, State}.
     {noreply, State}.
 
 
 %%------------------------------------------------------------------------------
 %%------------------------------------------------------------------------------
-%% Internal fns
+%% Internal exports
 %%------------------------------------------------------------------------------
 %%------------------------------------------------------------------------------
 
 
-handle_start_apps() ->
-    ?SLOG(notice, #{msg => "(re)starting_emqx_apps"}),
-    lists:foreach(fun start_one_app/1, emqx_machine_boot:sorted_reboot_apps()),
-    ?tp(emqx_machine_boot_apps_started, #{}).
-
-handle_stop_apps() ->
+%% Callers who wish to stop applications should not call this directly, and instead use
+%% `stop_apps/0`.  The only exception is `emqx_machine_terminator': it should call this
+%% block directly in its try-catch block, without the possibility of crashing this
+%% process.
+do_stop_apps() ->
     ?SLOG(notice, #{msg => "stopping_emqx_apps"}),
     ?SLOG(notice, #{msg => "stopping_emqx_apps"}),
     _ = emqx_alarm_handler:unload(),
     _ = emqx_alarm_handler:unload(),
     ok = emqx_conf_app:unset_config_loaded(),
     ok = emqx_conf_app:unset_config_loaded(),
     ok = emqx_plugins:ensure_stopped(),
     ok = emqx_plugins:ensure_stopped(),
     lists:foreach(fun stop_one_app/1, lists:reverse(emqx_machine_boot:sorted_reboot_apps())).
     lists:foreach(fun stop_one_app/1, lists:reverse(emqx_machine_boot:sorted_reboot_apps())).
 
 
+%%------------------------------------------------------------------------------
+%% Internal fns
+%%------------------------------------------------------------------------------
+
+handle_start_apps() ->
+    ?SLOG(notice, #{msg => "(re)starting_emqx_apps"}),
+    lists:foreach(fun start_one_app/1, emqx_machine_boot:sorted_reboot_apps()),
+    ?tp(emqx_machine_boot_apps_started, #{}).
+
 start_one_app(App) ->
 start_one_app(App) ->
     ?SLOG(debug, #{msg => "starting_app", app => App}),
     ?SLOG(debug, #{msg => "starting_app", app => App}),
     case application:ensure_all_started(App, emqx_machine_boot:restart_type(App)) of
     case application:ensure_all_started(App, emqx_machine_boot:restart_type(App)) of

+ 1 - 1
apps/emqx_machine/src/emqx_machine_terminator.erl

@@ -96,7 +96,7 @@ handle_call(?DO_IT, _From, State) ->
     try
     try
         %% stop port apps before stopping other apps.
         %% stop port apps before stopping other apps.
         emqx_machine_boot:stop_port_apps(),
         emqx_machine_boot:stop_port_apps(),
-        emqx_machine_boot:stop_apps()
+        emqx_machine_app_booter:do_stop_apps()
     catch
     catch
         C:E:St ->
         C:E:St ->
             Apps = [element(1, A) || A <- application:which_applications()],
             Apps = [element(1, A) || A <- application:which_applications()],