forge
Explore
Log in

Don't you dare go Hollow

memguard-runner: unique scope name + probe under a distinct name

Browse files at this commit

38d0251c7cc59ed6642340bdd141eea45bc475a0Ahmet Kaan GümüşParent: a460dca
Three lanes hit 'Unit run-pNNN.scope was already loaded or has a fragment
file' in one batch, and the workaround (invoking the test binary directly)
lost the memory cap. Two causes, both measured: systemd-run derives
run-p<pid>.scope when no --unit is given (pid reuse collides), and a
transient scope stays LOADED after it exits, so probing and exec'ing under
the same --unit collides with the probe's own scope.

Fix: unique --unit per invocation, and the availability probe runs under
${unit}-probe so it cannot collide with the exec. The collision is detected
by PROBING, never by the wrapped command's exit status -- systemd-run --scope
returns the command's own status, so a retry on non-zero would run every
failing test twice. Stale fragments are cleared before the second probe; if
the cap still cannot be acquired the runner falls back to the uncapped exec
with a loud message.
scripts/memguard-runner.shmodified+33 -1
@@ -16,6 +16,38 @@ if ! systemd-run --user --scope --quiet -p MemoryMax=10G true >/dev/null 2>&1; t
1616 exec "$@"
1717 fi
1818
19-exec systemd-run --user --scope --quiet --same-dir \
19+# Explicit unique unit name: systemd-run derives `run-p<pid>.scope` when no
20+# --unit is given, so a scope left loaded from an earlier run (or a reused
21+# pid) collides with "Unit run-pNNN.scope was already loaded or has a
22+# fragment file" -- the wrapped binary then never starts, which reads as a
23+# broken tree rather than a runner collision. Measured 2026-09-29: three
24+# lanes hit it in one batch and their workaround was invoking the test
25+# binary directly, losing the memory cap.
26+#
27+# The collision is detected by PROBING, never by the wrapped command's exit
28+# status: `systemd-run --scope` returns the command's own status, so
29+# retrying on non-zero would run every failing test twice.
30+unit="memguard-$$-$(date +%s%N)"
31+# The probe must NOT reuse `$unit`: a transient scope stays loaded after it
32+# exits, so probing and then exec'ing under the same --unit fails with
33+# "unit was already loaded or has a fragment file" -- measured directly on
34+# 2026-09-29 (the probe's own scope was the collision). Probe under "-probe".
35+if ! systemd-run --user --scope --quiet --unit="${unit}-probe" -p MemoryMax=10G true >/dev/null 2>&1; then
36+ # Clear a stale fragment for this name (and any leftover default-named
37+ # scope) and probe once more before giving up on the cap.
38+ rm -f "${XDG_RUNTIME_DIR:-/run/user/$(id -u)}/systemd/user/${unit}.scope" \
39+ "${HOME}/.config/systemd/user/${unit}.scope" \
40+ "${HOME}"/.config/systemd/user/run-p*.scope 2>/dev/null || true
41+ systemctl --user daemon-reload 2>/dev/null || true
42+ if ! systemd-run --user --scope --quiet --unit="${unit}-r" -p MemoryMax=10G true >/dev/null 2>&1; then
43+ echo "memguard-runner: scope unavailable even after clearing stale fragments, running without memory cap" >&2
44+ exec "$@"
45+ fi
46+ exec systemd-run --user --scope --quiet --same-dir --unit="${unit}-r" \
47+ -p MemoryMax=10G -p MemorySwapMax=2G \
48+ -- "$@"
49+fi
50+
51+exec systemd-run --user --scope --quiet --same-dir --unit="$unit" \
2052 -p MemoryMax=10G -p MemorySwapMax=2G \
2153 -- "$@"