From 38d0251c7cc59ed6642340bdd141eea45bc475a0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Tue, 29 Sep 2026 03:46:43 +0300 Subject: [PATCH] memguard-runner: unique scope name + probe under a distinct name 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.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.sh | 34 +++++++++++++++++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) diff --git a/scripts/memguard-runner.sh b/scripts/memguard-runner.sh index 78161c65..0de01c7c 100755 --- a/scripts/memguard-runner.sh +++ b/scripts/memguard-runner.sh @@ -16,6 +16,38 @@ if ! systemd-run --user --scope --quiet -p MemoryMax=10G true >/dev/null 2>&1; t exec "$@" fi -exec systemd-run --user --scope --quiet --same-dir \ +# Explicit unique unit name: systemd-run derives `run-p.scope` when no +# --unit is given, so a scope left loaded from an earlier run (or a reused +# pid) collides with "Unit run-pNNN.scope was already loaded or has a +# fragment file" -- the wrapped binary then never starts, which reads as a +# broken tree rather than a runner collision. Measured 2026-09-29: three +# lanes hit it in one batch and their workaround was invoking the test +# binary directly, losing the memory cap. +# +# The collision is detected by PROBING, never by the wrapped command's exit +# status: `systemd-run --scope` returns the command's own status, so +# retrying on non-zero would run every failing test twice. +unit="memguard-$$-$(date +%s%N)" +# The probe must NOT reuse `$unit`: a transient scope stays loaded after it +# exits, so probing and then exec'ing under the same --unit fails with +# "unit was already loaded or has a fragment file" -- measured directly on +# 2026-09-29 (the probe's own scope was the collision). Probe under "-probe". +if ! systemd-run --user --scope --quiet --unit="${unit}-probe" -p MemoryMax=10G true >/dev/null 2>&1; then + # Clear a stale fragment for this name (and any leftover default-named + # scope) and probe once more before giving up on the cap. + rm -f "${XDG_RUNTIME_DIR:-/run/user/$(id -u)}/systemd/user/${unit}.scope" \ + "${HOME}/.config/systemd/user/${unit}.scope" \ + "${HOME}"/.config/systemd/user/run-p*.scope 2>/dev/null || true + systemctl --user daemon-reload 2>/dev/null || true + if ! systemd-run --user --scope --quiet --unit="${unit}-r" -p MemoryMax=10G true >/dev/null 2>&1; then + echo "memguard-runner: scope unavailable even after clearing stale fragments, running without memory cap" >&2 + exec "$@" + fi + exec systemd-run --user --scope --quiet --same-dir --unit="${unit}-r" \ + -p MemoryMax=10G -p MemorySwapMax=2G \ + -- "$@" +fi + +exec systemd-run --user --scope --quiet --same-dir --unit="$unit" \ -p MemoryMax=10G -p MemorySwapMax=2G \ -- "$@"