fix(force_shutdown): cannot suicide if the process hangs up

This commit is contained in:
Shawn 2021-08-09 09:28:50 +08:00 committed by turtleDeng
parent 391eb55324
commit 31a1942b61
2 changed files with 19 additions and 3 deletions

View File

@ -61,7 +61,7 @@ handle_event({set_alarm, {system_memory_high_watermark, []}}, State) ->
{ok, State}; {ok, State};
handle_event({set_alarm, {process_memory_high_watermark, Pid}}, State) -> handle_event({set_alarm, {process_memory_high_watermark, Pid}}, State) ->
emqx_alarm:activate(high_process_memory_usage, #{pid => Pid, emqx_alarm:activate(high_process_memory_usage, #{pid => list_to_binary(pid_to_list(Pid)),
high_watermark => emqx_os_mon:get_procmem_high_watermark()}), high_watermark => emqx_os_mon:get_procmem_high_watermark()}),
{ok, State}; {ok, State};

View File

@ -52,6 +52,8 @@
, hexstr2bin/1 , hexstr2bin/1
]). ]).
-define(OOM_FACTOR, 1.25).
%% @doc Parse v4 or v6 string format address to tuple. %% @doc Parse v4 or v6 string format address to tuple.
%% `Host' itself is returned if it's not an ip string. %% `Host' itself is returned if it's not an ip string.
maybe_parse_ip(Host) -> maybe_parse_ip(Host) ->
@ -216,12 +218,26 @@ do_check_oom([{Val, Max, Reason}|Rest]) ->
tune_heap_size(#{max_heap_size := MaxHeapSize}) -> tune_heap_size(#{max_heap_size := MaxHeapSize}) ->
%% If set to zero, the limit is disabled. %% If set to zero, the limit is disabled.
erlang:process_flag(max_heap_size, #{size => MaxHeapSize, erlang:process_flag(max_heap_size, #{size => must_kill_heap_size(MaxHeapSize),
kill => false, kill => true,
error_logger => true error_logger => true
}); });
tune_heap_size(undefined) -> ok. tune_heap_size(undefined) -> ok.
%% We multiply the size with factor ?OOM_FACTOR, to give the
%% process a chance to suicide by `check_oom/1`
must_kill_heap_size(Size) ->
MaxAllowedSize = case erlang:system_info(wordsize) of
8 -> % arch_64
(1 bsl 59) - 1;
4 -> % arch_32
(1 bsl 27) - 1
end,
case ceil(Size * ?OOM_FACTOR) of
Size0 when Size0 >= MaxAllowedSize -> MaxAllowedSize;
Size0 -> Size0
end.
-spec(proc_name(atom(), pos_integer()) -> atom()). -spec(proc_name(atom(), pos_integer()) -> atom()).
proc_name(Mod, Id) -> proc_name(Mod, Id) ->
list_to_atom(lists:concat([Mod, "_", Id])). list_to_atom(lists:concat([Mod, "_", Id])).