From df0c158b2ae655c6b0aa0371fdc69db3b3b4548a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Wed, 26 Aug 2026 18:23:43 +0300 Subject: [PATCH] feat(infra): cap every cargo-run test binary in a 10G systemd scope Three kernel OOM kills in 48h came from test binaries launched outside the omp-worker scope; a cap that lives in the launcher misses every other path. The cargo target runner now wraps each test/bin in a transient user scope (MemoryMax=10G, MemorySwapMax=2G), resolved via git rev-parse so every worktree checkout carries it. EC_NOMEMGUARD=1 bypasses; fails open where systemd is absent. --- .cargo/config.toml | 7 +++++++ scripts/memguard-runner.sh | 21 +++++++++++++++++++++ 2 files changed, 28 insertions(+) create mode 100644 .cargo/config.toml create mode 100755 scripts/memguard-runner.sh 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 \ + -- "$@"