Skip to content

Commit

Permalink
fix: avoid logging unnecessary errors in async cleanup functions
Browse files Browse the repository at this point in the history
Cleanup functions that access ETS tables may fail with `badarg` error during EMQX shutdown.
They are called asynchronously by `emqx_pool` workers and accessed ETS tables
may be already destroyed as their owners are shut down.
This fix catches ETS `badarg` errors before they can be caught and logged by `emqx_pool`.

Fixes: EMQX-9992
  • Loading branch information
SergeTupchiy committed Jul 14, 2023
1 parent 0dff428 commit 950d5ed
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 11 deletions.
22 changes: 13 additions & 9 deletions apps/emqx/src/emqx_broker_helper.erl
Expand Up @@ -153,13 +153,17 @@ code_change(_OldVsn, State, _Extra) ->
%%--------------------------------------------------------------------

clean_down(SubPid) ->
case ets:lookup(?SUBMON, SubPid) of
[{_, SubId}] ->
true = ets:delete(?SUBMON, SubPid),
true =
(SubId =:= undefined) orelse
ets:delete_object(?SUBID, {SubId, SubPid}),
emqx_broker:subscriber_down(SubPid);
[] ->
ok
try
case ets:lookup(?SUBMON, SubPid) of
[{_, SubId}] ->
true = ets:delete(?SUBMON, SubPid),
true =
(SubId =:= undefined) orelse
ets:delete_object(?SUBID, {SubId, SubPid}),
emqx_broker:subscriber_down(SubPid);
[] ->
ok
end
catch
error:badarg -> ok
end.
6 changes: 5 additions & 1 deletion apps/emqx/src/emqx_cm.erl
Expand Up @@ -734,7 +734,11 @@ code_change(_OldVsn, State, _Extra) ->
%%--------------------------------------------------------------------

clean_down({ChanPid, ClientId}) ->
do_unregister_channel({ClientId, ChanPid}),
try
do_unregister_channel({ClientId, ChanPid})
catch
error:badarg -> ok
end,
ok = ?tp(debug, emqx_cm_clean_down, #{client_id => ClientId}).

stats_fun() ->
Expand Down
6 changes: 5 additions & 1 deletion apps/emqx_gateway/src/emqx_gateway_cm.erl
Expand Up @@ -823,7 +823,11 @@ code_change(_OldVsn, State, _Extra) ->
do_unregister_channel_task(Items, GwName, CmTabs) ->
lists:foreach(
fun({ChanPid, ClientId}) ->
do_unregister_channel(GwName, {ClientId, ChanPid}, CmTabs)
try
do_unregister_channel(GwName, {ClientId, ChanPid}, CmTabs)
catch
error:badarg -> ok
end
end,
Items
).
Expand Down
1 change: 1 addition & 0 deletions changes/ce/fix-11065.en.md
@@ -0,0 +1 @@
Avoid logging irrelevant error messages during EMQX shutdown.

0 comments on commit 950d5ed

Please sign in to comment.