Cap every cargo-run test binary in a 10G systemd scope
Browse files at this commitdf0c158b2ae655c6b0aa0371fdc69db3b3b4548aParent: 463079b- feat
- cap every cargo-run test binary in a 10G systemd scope
2 changed files +28 -0
- .cargo/config.toml +7 -0
- scripts/memguard-runner.sh +21 -0
.cargo/config.tomladded+7 -0
| @@ -0,0 +1,7 @@ | |||
| 1 | +[target.x86_64-unknown-linux-gnu] | ||
| 2 | +# Wrap every test/bin run in scripts/memguard-runner.sh so a runaway | ||
| 3 | +# allocation gets killed inside its own cgroup instead of OOM-killing the | ||
| 4 | +# machine. Located via `git rev-parse --show-toplevel` from the invoker's cwd | ||
| 5 | +# (cargo does not resolve runner paths relative to this file, and an absolute | ||
| 6 | +# path would break other worktree checkouts of this repo). | ||
| 7 | +runner = ["bash", "-c", "root=$(git rev-parse --show-toplevel) && exec \"$root/scripts/memguard-runner.sh\" \"$@\"", "--"] | ||
scripts/memguard-runner.shadded+21 -0
| @@ -0,0 +1,21 @@ | |||
| 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 | +set -euo pipefail | ||
| 8 | + | ||
| 9 | +if [ "${EC_NOMEMGUARD:-0}" = "1" ]; then | ||
| 10 | + echo "memguard-runner: EC_NOMEMGUARD=1, running without memory cap" >&2 | ||
| 11 | + exec "$@" | ||
| 12 | +fi | ||
| 13 | + | ||
| 14 | +if ! systemd-run --user --scope --quiet -p MemoryMax=10G true >/dev/null 2>&1; then | ||
| 15 | + echo "memguard-runner: systemd-run --user --scope unavailable, running without memory cap" >&2 | ||
| 16 | + exec "$@" | ||
| 17 | +fi | ||
| 18 | + | ||
| 19 | +exec systemd-run --user --scope --quiet --same-dir \ | ||
| 20 | + -p MemoryMax=10G -p MemorySwapMax=2G \ | ||
| 21 | + -- "$@" | ||