Linux memory cap: an OOM kill gets no reason when systemd removes the scope first
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
- In the
systemd-runfallback,linux::creategives the cgroup of the transient scope itself, not a child cgroup (crates/aft/src/bash_background/memory_limit.rs:547-555). linux::attachwrites the payload PID directly into that scope (memory_limit.rs:605).Monitor::attachthen stops the bootstrap shell (memory_limit.rs:264-266). After that, the payload is the only process in the scope.- The comment at
memory_limit.rs:563-564says 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. - 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. - 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). linux::oomchanges a failed read into "not exceeded":std::fs::read_to_string(path.join("memory.events")).is_ok_and(...)(memory_limit.rs:589-598).- Thus, when the cgroup is removed before the read, the task keeps the plain exit (
128 + SIGKILL= 137 fromreap_child) with no reason.
Deterministic test design
These tests do not use the size of the payload:
- Unit level:
- Make a temporary directory with a
memory.eventsfile that containsoom_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 isfalse.
- Make a temporary directory with a
- Registry level:
- Start
kill -KILL $$withmemory_limit_mib = Some(64). - Before the reap, put a recovered monitor into the registry's memory monitors, as
memory_limit_tests.rs:833-840does. The monitor must point to a cgroup path that does not exist. - Wait for the terminal state. Today the task always ends as
Failedwith an emptystatus_reason.
- Start
Suggested repair
- 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.
- If AFT cannot read
memory.eventsafter 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.
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.
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.