Explorar o código

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

Shawn %!s(int64=4) %!d(string=hai) anos
pai
achega
31a1942b61
Modificáronse 2 ficheiros con 19 adicións e 3 borrados
  1. 1 1
      src/emqx_alarm_handler.erl
  2. 18 2
      src/emqx_misc.erl

+ 1 - 1
src/emqx_alarm_handler.erl

@@ -61,7 +61,7 @@ handle_event({set_alarm, {system_memory_high_watermark, []}}, State) ->
     {ok, 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()}),
     {ok, State};
 

+ 18 - 2
src/emqx_misc.erl

@@ -52,6 +52,8 @@
         , hexstr2bin/1
         ]).
 
+-define(OOM_FACTOR, 1.25).
+
 %% @doc Parse v4 or v6 string format address to tuple.
 %% `Host' itself is returned if it's not an ip string.
 maybe_parse_ip(Host) ->
@@ -216,12 +218,26 @@ do_check_oom([{Val, Max, Reason}|Rest]) ->
 
 tune_heap_size(#{max_heap_size := MaxHeapSize}) ->
     %% If set to zero, the limit is disabled.
-    erlang:process_flag(max_heap_size, #{size => MaxHeapSize,
-                                         kill => false,
+    erlang:process_flag(max_heap_size, #{size => must_kill_heap_size(MaxHeapSize),
+                                         kill => true,
                                          error_logger => true
                                         });
 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()).
 proc_name(Mod, Id) ->
     list_to_atom(lists:concat([Mod, "_", Id])).