diff --git a/.cargo/config.toml b/.cargo/config.toml new file mode 100644 index 00000000..ad84c55b --- /dev/null +++ b/.cargo/config.toml @@ -0,0 +1,7 @@ +[target.x86_64-unknown-linux-gnu] +# Wrap every test/bin run in scripts/memguard-runner.sh so a runaway +# allocation gets killed inside its own cgroup instead of OOM-killing the +# machine. Located via `git rev-parse --show-toplevel` from the invoker's cwd +# (cargo does not resolve runner paths relative to this file, and an absolute +# path would break other worktree checkouts of this repo). +runner = ["bash", "-c", "root=$(git rev-parse --show-toplevel) && exec \"$root/scripts/memguard-runner.sh\" \"$@\"", "--"] diff --git a/scripts/memguard-runner.sh b/scripts/memguard-runner.sh new file mode 100755 index 00000000..78161c65 --- /dev/null +++ b/scripts/memguard-runner.sh @@ -0,0 +1,21 @@ +#!/usr/bin/env bash +# Wraps a test/bin invocation in a transient user cgroup scope so a runaway +# allocation dies inside its own cgroup instead of triggering a machine-wide +# OOM kill. Used as the cargo target runner (see .cargo/config.toml) so every +# `cargo test`/`cargo run` invocation of this workspace is capped, from any +# checkout (main repo or worktree). +set -euo pipefail + +if [ "${EC_NOMEMGUARD:-0}" = "1" ]; then + echo "memguard-runner: EC_NOMEMGUARD=1, running without memory cap" >&2 + exec "$@" +fi + +if ! systemd-run --user --scope --quiet -p MemoryMax=10G true >/dev/null 2>&1; then + echo "memguard-runner: systemd-run --user --scope unavailable, running without memory cap" >&2 + exec "$@" +fi + +exec systemd-run --user --scope --quiet --same-dir \ + -p MemoryMax=10G -p MemorySwapMax=2G \ + -- "$@"