aft icon indicating copy to clipboard operation
aft copied to clipboard

Linux memory cap: an OOM kill gets no reason when systemd removes the scope first

Open iceteaSA opened this issue 1 week ago • 2 comments

Summary

On Linux, the kernel can stop a task that has a memory cap with an OOM kill. If systemd removes the scope first, AFT reports the task as Failed (exit 137) with an empty status_reason. It does not say that the memory cap stopped the task. This occurs with the systemd-run fallback for the memory cap (cgroup v2, delegated user manager), and is most frequent when the payload is killed at its start.

This is the observation in our test report on #1219. The memory-cap tests sometimes fail at memory_limit_tests.rs:342, assert!(reason.contains("64 MiB memory cap"), "{reason}"), with an empty reason.

Line numbers are for main at f32defdd7.

Cause

  1. In the systemd-run fallback, linux::create gives the cgroup of the transient scope itself, not a child cgroup (crates/aft/src/bash_background/memory_limit.rs:547-555).
  2. linux::attach writes the payload PID directly into that scope (memory_limit.rs:605). Monitor::attach then stops the bootstrap shell (memory_limit.rs:264-266). After that, the payload is the only process in the scope.
  3. The comment at memory_limit.rs:563-564 says that a populated descendant keeps the scope's cgroup alive. But AFT makes no descendant cgroup on this path, so nothing keeps the scope alive.
  4. The OOM group kill (memory.oom.group=1, memory_limit.rs:551) makes the scope empty. Then systemd stops the unit (--collect, memory_limit.rs:528) and removes its cgroup. We did a check with a 16 MiB scope. systemd removed the cgroup a few milliseconds after it logged the OOM kill.
  5. AFT gets the cap result only from <scope>/memory.events, on watchdog polls (scope_runtime.rs:375-417) and once after the exit (settle_memory_accounting, scope_runtime.rs:470-485).
  6. linux::oom changes a failed read into "not exceeded": std::fs::read_to_string(path.join("memory.events")).is_ok_and(...) (memory_limit.rs:589-598).
  7. Thus, when the cgroup is removed before the read, the task keeps the plain exit (128 + SIGKILL = 137 from reap_child) with no reason.

Deterministic test design

These tests do not use the size of the payload:

  1. Unit level:
    • Make a temporary directory with a memory.events file that contains oom_kill 1.
    • Make a monitor from it with Monitor::recover. Use a memory policy type that is not the task-scope type.
    • Check that exceeded() is true.
    • Remove the directory. Call exceeded() on a new monitor. Today the result is false.
  2. Registry level:
    • Start kill -KILL $$ with memory_limit_mib = Some(64).
    • Before the reap, put a recovered monitor into the registry's memory monitors, as memory_limit_tests.rs:833-840 does. The monitor must point to a cgroup path that does not exist.
    • Wait for the terminal state. Today the task always ends as Failed with an empty status_reason.

Suggested repair

  1. Keep the scope alive until AFT has recorded the terminal state. Put the payload into a child cgroup of the delegated scope. Or keep the bootstrap process in the scope until then.
  2. If AFT cannot read memory.events after a SIGKILL exit, report the cap result as unknown, not as "not exceeded".

A test that removes the cgroup directory before the read after the exit would prevent a return of this defect.

iceteaSA avatar Nov 21 '26 08:11 iceteaSA

Thank you for the careful trace and the test design. We confirmed it on main. In the systemd-run fallback, the payload ends up as the only process in the scope. The OOM group kill empties the scope, and systemd removes the cgroup before AFT reads memory.events. The failed read then counts as "cap not exceeded".

The fix we're building keeps the scope alive until AFT has recorded the result. The payload runs in its own child cgroup, which carries the memory cap and the OOM group setting. A small holder process waits in a sibling cgroup, and AFT releases it only after it has read the payload's memory.events. If AFT itself restarts while the command runs, it reads the result when the payload's cgroup still exists. Otherwise it keeps today's outcome and logs a warning naming the task.

It should land in the release after 0.67. We'll update this issue then.

aft-alfonso[bot] avatar Nov 22 '26 10:11 aft-alfonso[bot]

Fixed in 0.68.0. On Linux, a command that its memory cap kills now reports the cap as the cause, instead of an unknown result. Thanks for the report, @iceteaSA.

Closing this. If you still see an unknown result for a memory-capped command after upgrading, reopen it.

aft-alfonso[bot] avatar Nov 26 '26 01:11 aft-alfonso[bot]