Skip to content

Don't need room on a dying process's heap to signal its links and monitors - #2501

Open
gabrielmancini wants to merge 2 commits into
atomvm:release-0.7from
iobee-io:fix-down-signal-oom
Open

gabrielmancini wants to merge 2 commits into
atomvm:release-0.7from
iobee-io:fix-down-signal-oom

Conversation

@gabrielmancini

Copy link
Copy Markdown

context_monitors_handle_terminate builds the EXIT signal for a linked process, and the DOWN signal for a monitoring one, on the heap of the process that is terminating. When that heap cannot take the tuple, for example a heap at its max_heap_size, the VM aborts:

Cannot handle out of memory.

mailbox_send_term_signal already copies the term into the signal, so the tuple is only needed until then. With this change it is built on a stack heap (BEGIN_WITH_STACK_HEAP, as elsewhere in context.c), and the exit reason is delivered as before.

Field data: the serial logs of our ESP32 boards have this message at about 190 distinct times between 21 October and 26 November. In the VM our boards run today it is only printed at these two places. Older VMs logged most of them, and their sources were not checked.

Not covered: if the malloc of the signal itself fails, mailbox_message_create_from_term returns NULL and mailbox_send_term_signal posts it, which dereferences it. That is shared by every term signal and is unchanged here.

Tests: test_oom_exit_signal lets processes with a bounded max_heap_size die with their heaps filled by 0 to 255 cells, monitored and linked, both by raising an error and by exiting with a reason that lives on their own heap, which must arrive whole.

  • generic_unix, Linux x86_64: without the fix it fails 5 of 5 runs, and each path aborts on its own (3 of 3 runs for monitors, 3 of 3 for links); with it, it passes 5 of 5. The whole test-erlang suite passes, and the new test also passes on BEAM with test-erlang -b, with and without -s prime_smp. BEAM prints an error report for each worker it kills.
  • ESP32 on QEMU, with the module temporarily added to the ESP32 test app: without the fix the VM prints Cannot handle out of memory. and calls abort(); with it, the test passes. The only failure in that run is test_socket, which also fails on release-0.7 without this change.

…itors

context_monitors_handle_terminate built the EXIT signal for a linked
process, and the DOWN signal for a monitoring one, on the heap of the
process that was terminating, and aborted the VM with "Cannot handle out
of memory" when that heap could not take it, for example with a heap at
its max_heap_size.

mailbox_send_term_signal copies the term into the signal, so the tuple is
only needed until then: build it on a stack heap. The exit reason is
delivered as before.

Signed-off-by: Gabriel Mancini <gabriel.mancini@gmail.com>

@petermm petermm left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review: local EXIT and DOWN delivery at the process heap limit

Commit: 13356686dd6c, reviewed against its parent. Verdict: No merge blockers found. Keep the C change; tighten the regression test. The independent Oracle review reached the same conclusion on the C change and flagged the exit-reason assertion below.

The temporary heaps have room for the EXIT tuple and for the DOWN tuple plus either reference representation. mailbox_send_term_signal copies the entire term tree synchronously, while the temporary tuple and the dying process's reason are both still live. The change preserves the alias reference's owner and ticks and leaves test registration complete. See context.c:899–964, mailbox.c:257–299, and memory.c:423–455.

P2 · Require a structured exit reason to arrive intact

test_oom_exit_signal.erl:44–77 uses the same accepted reasons for both worker modes and every heap pressure level. For example, a notification that incorrectly reports out_of_memory instead of {data, Data} would still pass every exit_with_data iteration. The content check is useful, but only runs if the notification already has the right shape.

Require the exact nonempty payload at Cells = 1, where the tiny worker has ample room under the 256-word cap. Keep accepting OOM outcomes in the rest of the sweep, but accept boom only from fail. Both EXIT and DOWN use the same assertion.

--- a/tests/erlang_tests/test_oom_exit_signal.erl
+++ b/tests/erlang_tests/test_oom_exit_signal.erl
@@ -46,7 +46,7 @@
         monitor, {max_heap_size, ?MAX_HEAP}
     ]),
     receive
-        {'DOWN', Ref, process, Pid, Reason} -> ok = expected(Cells, Reason)
+        {'DOWN', Ref, process, Pid, Reason} -> ok = expected(How, Cells, Reason)
     end,
     monitored(How, Cells + 1).
 
@@ -62,19 +62,21 @@
 linked(How, Cells) ->
     Pid = spawn_opt(?MODULE, worker, [{How, Cells}], [link, {max_heap_size, ?MAX_HEAP}]),
     receive
-        {'EXIT', Pid, Reason} -> ok = expected(Cells, Reason)
+        {'EXIT', Pid, Reason} -> ok = expected(How, Cells, Reason)
     end,
     linked(How, Cells + 1).
 
 % The reason may live on the heap of the process that died: it must arrive
 % whole. A worker may also run out of memory before it gets to exit.
-expected(Cells, {data, Data}) -> check_data(Data, Cells);
-expected(_Cells, {boom, _Stacktrace}) -> ok;
-expected(_Cells, boom) -> ok;
-expected(_Cells, out_of_memory) -> ok;
-expected(_Cells, {out_of_memory, _}) -> ok;
-expected(_Cells, killed) -> ok;
-expected(_Cells, Other) -> {unexpected, Other}.
+expected(exit_with_data, Cells, {data, Data}) -> check_data(Data, Cells);
+% This small worker has room to construct its reason; do not accept OOM here.
+expected(exit_with_data, 1, Other) -> {unexpected, Other};
+expected(fail, _Cells, {boom, _Stacktrace}) -> ok;
+expected(fail, _Cells, boom) -> ok;
+expected(_How, _Cells, out_of_memory) -> ok;
+expected(_How, _Cells, {out_of_memory, _}) -> ok;
+expected(_How, _Cells, killed) -> ok;
+expected(_How, _Cells, Other) -> {unexpected, Other}.
 
 check_data(Data, Cells) ->
     Data = fill(Cells, []),

P3 · Fail promptly when a notification never matches

The two receives in test_oom_exit_signal.erl:48–66 have no deadline. A dropped notification or one with the wrong PID or reference would stall test-erlang until an external timeout, without identifying the failing iteration. Follow the existing monitor test's five-second deadline and report which signal and worker case failed. This snippet builds on the P2 diff.

--- a/tests/erlang_tests/test_oom_exit_signal.erl
+++ b/tests/erlang_tests/test_oom_exit_signal.erl
@@ -47,5 +47,7 @@
     ]),
     receive
         {'DOWN', Ref, process, Pid, Reason} -> ok = expected(How, Cells, Reason)
+    after 5000 ->
+        error({missing_down, How, Cells})
     end,
     monitored(How, Cells + 1).
@@ -63,5 +65,7 @@
     Pid = spawn_opt(?MODULE, worker, [{How, Cells}], [link, {max_heap_size, ?MAX_HEAP}]),
     receive
         {'EXIT', Pid, Reason} -> ok = expected(How, Cells, Reason)
+    after 5000 ->
+        error({missing_exit, How, Cells})
     end,
     linked(How, Cells + 1).

Out of scope: The mailbox still allocates signal storage from the system heap, and term sizing can allocate scratch space. System allocator failure remains unsafe; that predates this change. The fix is for exhaustion of the dying process's bounded VM heap, not for system-wide OOM.

Verification: Both diffs applied cleanly in order to a temporary copy of the committed test, and the amended test passed on OTP 29. The AtomVM runtime was reviewed from source, not built or tested for this review.

Accept each reason only from the worker mode that produces it, require the
data to arrive in the smallest case, and fail after five seconds when a
notification does not arrive.

Signed-off-by: Gabriel Mancini <gabriel.mancini@gmail.com>
@gabrielmancini

Copy link
Copy Markdown
Author

Thanks! Applied both test changes: stricter exit reasons per worker mode, and a 5 s deadline on the receives.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants