From 81c9dcb6aed9a4afa9baa3e759db9830abefa131 Mon Sep 17 00:00:00 2001 From: Zaiming Shi Date: Fri, 6 Aug 2021 08:51:48 +0200 Subject: [PATCH] refactor(emqx_machine_terminator): future-proof try-catch Ensure exceptions in emqx_machine:stop_apps/0 is caught and call init:stop/0 in the after clause --- .../src/emqx_machine_terminator.erl | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/apps/emqx_machine/src/emqx_machine_terminator.erl b/apps/emqx_machine/src/emqx_machine_terminator.erl index fdd75d3ad..f26287258 100644 --- a/apps/emqx_machine/src/emqx_machine_terminator.erl +++ b/apps/emqx_machine/src/emqx_machine_terminator.erl @@ -27,6 +27,8 @@ handle_cast/2, handle_call/3, handle_info/2, terminate/2, code_change/3]). +-include_lib("emqx/include/logger.hrl"). + -define(TERMINATOR, ?MODULE). -define(DO_IT, graceful_shutdown). @@ -62,8 +64,20 @@ init(_) -> {ok, #{}}. handle_info(?DO_IT, State) -> - ok = emqx_machine:stop_apps(normal), - init:stop(), + try + emqx_machine:stop_apps(normal) + catch + C : E : St -> + Apps = [element(1, A) || A <- application:which_applications()], + ?SLOG(error, #{msg => "failed_to_stop_apps", + exception => C, + reason => E, + stacktrace => St, + remaining_apps => Apps + }) + after + init:stop() + end, {noreply, State}; handle_info(_, State) -> {noreply, State}.