Skip to content

Commit 972ae98

Browse files
Merge pull request #27 from rabbitmq/exit-oom
Flush mailbox before exit to prevent OOM during crash report formatting
2 parents 00bd10a + 12d57f6 commit 972ae98

1 file changed

Lines changed: 15 additions & 2 deletions

File tree

src/gen_batch_server.erl

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,7 @@ loop_wait(#state{config = #config{hibernate_after = Hib}} = State00, Parent) ->
289289
?MODULE, State0#state.debug, State0);
290290
{'EXIT', Parent, Reason} ->
291291
terminate(Reason, State0),
292+
flush_mailbox(),
292293
exit(Reason);
293294
_ ->
294295
enter_loop_batched(Msg, Parent, State0)
@@ -343,6 +344,7 @@ loop_batched(#state{debug = Debug} = State0, Parent) ->
343344
?MODULE, Debug, State0);
344345
{'EXIT', Parent, Reason} ->
345346
terminate(Reason, State0),
347+
flush_mailbox(),
346348
exit(Reason);
347349
_ ->
348350
enter_loop_batched(Msg, Parent, State0)
@@ -386,6 +388,7 @@ complete_batch(#state{batch = Batch0,
386388
handle_batch_result(Result, State0, Debug0);
387389
Class:Reason:Stacktrace ->
388390
terminate(Reason, State0),
391+
flush_mailbox(),
389392
erlang:raise(Class, Reason, safe_stacktrace(Stacktrace))
390393
end.
391394

@@ -415,6 +418,7 @@ handle_batch_result({ok, Actions, Inner, {continue, Continue}}, State0, Debug0)
415418
debug = Debug});
416419
handle_batch_result({stop, Reason}, State0, _Debug0) ->
417420
terminate(Reason, State0),
421+
flush_mailbox(),
418422
exit(Reason).
419423

420424
handle_actions(Actions, Debug0) ->
@@ -437,6 +441,7 @@ handle_continue(Continue, #state{config = #config{module = Mod},
437441
handle_continue_result(Result, State0);
438442
Class:Reason:Stacktrace ->
439443
terminate(Reason, State0),
444+
flush_mailbox(),
440445
erlang:raise(Class, Reason, safe_stacktrace(Stacktrace))
441446
end.
442447

@@ -446,6 +451,7 @@ handle_continue_result({ok, Inner, {continue, NextContinue}}, State0) ->
446451
handle_continue(NextContinue, State0#state{state = Inner});
447452
handle_continue_result({stop, Reason}, State0) ->
448453
terminate(Reason, State0),
454+
flush_mailbox(),
449455
exit(Reason).
450456

451457
handle_debug_in(#state{debug = Dbg0} = State, Msg) ->
@@ -467,6 +473,7 @@ system_continue(Parent, Debug, State) ->
467473
-spec system_terminate(term(), pid(), list(), term()) -> no_return().
468474
system_terminate(Reason, _Parent, _Debug, State) ->
469475
terminate(Reason, State),
476+
flush_mailbox(),
470477
exit(Reason).
471478

472479
system_get_state(State) ->
@@ -499,8 +506,6 @@ sys_get_log(Debug) ->
499506
write_debug(Dev, Event, Name) ->
500507
io:format(Dev, "~p event = ~p~n", [Name, Event]).
501508

502-
%% Send function
503-
504509
do_send(Dest, Msg) ->
505510
try erlang:send(Dest, Msg)
506511
catch
@@ -538,3 +543,11 @@ safe_stacktrace(Stacktrace) ->
538543
(Frame) ->
539544
Frame
540545
end, Stacktrace).
546+
547+
%% Flush all messages from the mailbox. This prevents proc_lib crash reports
548+
%% from pretty-printing potentially large messages (e.g. WAL write commands
549+
%% containing binary payloads), which can cause unbounded memory growth.
550+
flush_mailbox() ->
551+
receive _ -> flush_mailbox()
552+
after 0 -> ok
553+
end.

0 commit comments

Comments
 (0)