From 6897f0141b68fd34441c6a5fbe93a56b0ff857ec Mon Sep 17 00:00:00 2001 From: Thales Macedo Garitezi Date: Mon, 17 Jun 2024 15:48:31 -0300 Subject: [PATCH] fix(plugins): do not stop Elixir protected apps On one hand, Elixir plugins might include Elixir itself, when targetting a non-Elixir EMQX release. If, on the other hand, the EMQX release already includes Elixir, we shouldn't stop Elixir nor IEx, or the running IEx shell will break. --- apps/emqx_plugins/src/emqx_plugins.erl | 39 +++++++++++++++++++------- mix.exs | 1 + 2 files changed, 30 insertions(+), 10 deletions(-) diff --git a/apps/emqx_plugins/src/emqx_plugins.erl b/apps/emqx_plugins/src/emqx_plugins.erl index baad35af5..de42b22ad 100644 --- a/apps/emqx_plugins/src/emqx_plugins.erl +++ b/apps/emqx_plugins/src/emqx_plugins.erl @@ -986,14 +986,7 @@ start_app(App) -> %% but not the ones shared with others. ensure_apps_stopped(#{<<"rel_apps">> := Apps}) -> %% load plugin apps and beam code - AppsToStop = - lists:map( - fun(NameVsn) -> - {ok, AppName, _AppVsn} = parse_name_vsn(NameVsn), - AppName - end, - Apps - ), + AppsToStop = lists:filtermap(fun parse_name_vsn_for_stopping/1, Apps), case tryit("stop_apps", fun() -> stop_apps(AppsToStop) end) of {ok, []} -> %% all apps stopped @@ -1009,6 +1002,30 @@ ensure_apps_stopped(#{<<"rel_apps">> := Apps}) -> {error, Reason} end. +%% On one hand, Elixir plugins might include Elixir itself, when targetting a non-Elixir +%% EMQX release. If, on the other hand, the EMQX release already includes Elixir, we +%% shouldn't stop Elixir nor IEx. +-ifdef(EMQX_ELIXIR). +is_protected_app(elixir) -> true; +is_protected_app(iex) -> true; +is_protected_app(_) -> false. + +parse_name_vsn_for_stopping(NameVsn) -> + {ok, AppName, _AppVsn} = parse_name_vsn(NameVsn), + case is_protected_app(AppName) of + true -> + false; + false -> + {true, AppName} + end. +%% ELSE ifdef(EMQX_ELIXIR) +-else. +parse_name_vsn_for_stopping(NameVsn) -> + {ok, AppName, _AppVsn} = parse_name_vsn(NameVsn), + {true, AppName}. +%% END ifdef(EMQX_ELIXIR) +-endif. + stop_apps(Apps) -> RunningApps = running_apps(), case do_stop_apps(Apps, [], RunningApps) of @@ -1045,8 +1062,10 @@ stop_app(App) -> unload_moudle_and_app(App) -> case application:get_key(App, modules) of - {ok, Modules} -> lists:foreach(fun code:soft_purge/1, Modules); - _ -> ok + {ok, Modules} -> + lists:foreach(fun code:soft_purge/1, Modules); + _ -> + ok end, _ = application:unload(App), ok. diff --git a/mix.exs b/mix.exs index e37ab83b2..6ee0c73e5 100644 --- a/mix.exs +++ b/mix.exs @@ -270,6 +270,7 @@ defmodule EMQXUmbrella.MixProject do :debug_info, {:compile_info, [{:emqx_vsn, String.to_charlist(version)}]}, {:d, :EMQX_RELEASE_EDITION, erlang_edition(edition_type)}, + {:d, :EMQX_ELIXIR}, {:d, :snk_kind, :msg} ] end