forge
Explore
Log in

Don't you dare go Hollow

edith_codecs

Clone
★ 0

0 watching0 forksRust370.3 MiB511a95bTahinli4 days ago4017 commits

memguard-runner.sh56 lines, 2.9 KiBHistoryRawDownload
1#!/usr/bin/env bash
2# Wraps a test/bin invocation in a transient user cgroup scope so a runaway
3# allocation dies inside its own cgroup instead of triggering a machine-wide
4# OOM kill. Used as the cargo target runner (see .cargo/config.toml) so every
5# `cargo test`/`cargo run` invocation of this workspace is capped, from any
6# checkout (main repo or worktree).
7#
8# The cap is EC_MEMGUARD_MAX (default 10G), applied as MemoryMax on every scope.
9# scripts/remote-run.sh sets it to 6G on the lane servers.
10set -euo pipefail
11
12if [ "${EC_NOMEMGUARD:-0}" = "1" ]; then
13 echo "memguard-runner: EC_NOMEMGUARD=1, running without memory cap" >&2
14 exec "$@"
15fi
16
17if ! systemd-run --user --scope --quiet -p MemoryMax=${EC_MEMGUARD_MAX:-10G} true >/dev/null 2>&1; then
18 echo "memguard-runner: systemd-run --user --scope unavailable, running without memory cap" >&2
19 exec "$@"
20fi
21
22# Explicit unique unit name: systemd-run derives `run-p<pid>.scope` when no
23# --unit is given, so a scope left loaded from an earlier run (or a reused
24# pid) collides with "Unit run-pNNN.scope was already loaded or has a
25# fragment file" -- the wrapped binary then never starts, which reads as a
26# broken tree rather than a runner collision. Measured 2026-09-29: three
27# lanes hit it in one batch and their workaround was invoking the test
28# binary directly, losing the memory cap.
29#
30# The collision is detected by PROBING, never by the wrapped command's exit
31# status: `systemd-run --scope` returns the command's own status, so
32# retrying on non-zero would run every failing test twice.
33unit="memguard-$$-$(date +%s%N)"
34# The probe must NOT reuse `$unit`: a transient scope stays loaded after it
35# exits, so probing and then exec'ing under the same --unit fails with
36# "unit was already loaded or has a fragment file" -- measured directly on
37# 2026-09-29 (the probe's own scope was the collision). Probe under "-probe".
38if ! systemd-run --user --scope --quiet --unit="${unit}-probe" -p MemoryMax=${EC_MEMGUARD_MAX:-10G} true >/dev/null 2>&1; then
39 # Clear a stale fragment for this name (and any leftover default-named
40 # scope) and probe once more before giving up on the cap.
41 rm -f "${XDG_RUNTIME_DIR:-/run/user/$(id -u)}/systemd/user/${unit}.scope" \
42 "${HOME}/.config/systemd/user/${unit}.scope" \
43 "${HOME}"/.config/systemd/user/run-p*.scope 2>/dev/null || true
44 systemctl --user daemon-reload 2>/dev/null || true
45 if ! systemd-run --user --scope --quiet --unit="${unit}-r" -p MemoryMax=${EC_MEMGUARD_MAX:-10G} true >/dev/null 2>&1; then
46 echo "memguard-runner: scope unavailable even after clearing stale fragments, running without memory cap" >&2
47 exec "$@"
48 fi
49 exec systemd-run --user --scope --quiet --same-dir --unit="${unit}-r" \
50 -p MemoryMax=${EC_MEMGUARD_MAX:-10G} -p MemorySwapMax=2G \
51 -- "$@"
52fi
53
54exec systemd-run --user --scope --quiet --same-dir --unit="$unit" \
55 -p MemoryMax=${EC_MEMGUARD_MAX:-10G} -p MemorySwapMax=2G \
56 -- "$@"