tools/guard.sh: kill a command's process tree past a memory or time cap; CLAUDE.md requires it for every suite, fuzzer and sweep
A background test run consumed 100 GB and crashed the machine on 2026-09-03 (the process did not survive; the likely source was the half-finished tolerant-parser recovery under the truncate-anywhere fuzzers). macOS enforces no memory cap, so this poller is the cap: 8 GB / 30 min by default, TERM then KILL over the whole tree, exit 137 with the reason. Proved on a deliberate runaway allocator. Co-Authored-By: Claude Fable 5 <[email protected]>
This commit is contained in:
1 parent
63bbc23e37
commit
4d70de1976
2 files changed
+84
No files matched your search
@@ -128,6 +128,20 @@ Rules:
|
||||
- Update `TODO.md`: check items your feature completes (leave the item
|
||||
text intact).
|
||||
|
||||
## Runaway guard (never run a suite or sweep bare)
|
||||
|
||||
On 2026-09-03 a background test run consumed 100 GB and crashed the
|
||||
machine. macOS enforces no memory cap, so the cap is ours:
|
||||
`tools/guard.sh [-m GB] [-t SECONDS] -- <command>` kills a command's
|
||||
process tree when its resident set or wall clock exceeds the limits
|
||||
(defaults 8 GB / 30 min).
|
||||
|
||||
- **Every `cargo test`, every fuzzer, every corpus or opentitan sweep,
|
||||
and every `lucisv`/`lucisv-server` run over a tree goes through
|
||||
`tools/guard.sh`** — dev, QA and lead alike, foreground or background.
|
||||
- A guard kill is a finding: report the command and the limit it hit;
|
||||
do not raise the limit to make it pass.
|
||||
|
||||
## Parallel lanes (the 2-agent loop)
|
||||
|
||||
Up to **two dev/QA lanes may run concurrently** when — and only when —
|
||||
|
||||
Executable
+70
@@ -0,0 +1,70 @@
|
||||
#!/usr/bin/env bash
|
||||
# Run a command under a memory and wall-clock guard.
|
||||
#
|
||||
# Usage: tools/guard.sh [-m MAX_GB] [-t MAX_SECONDS] -- <command...>
|
||||
# tools/guard.sh -m 8 -t 900 -- cargo test --workspace
|
||||
#
|
||||
# Every second the resident set of the command's whole process tree is
|
||||
# summed; if it exceeds MAX_GB (default 8) or the clock exceeds MAX_SECONDS
|
||||
# (default 1800) the tree is killed (TERM, then KILL) and the script exits
|
||||
# 137 with a line saying why. Exists because a runaway test or sweep once
|
||||
# consumed 100 GB and took the machine down (2026-09-03): agents run every
|
||||
# test suite, fuzzer and corpus/opentitan sweep through this.
|
||||
#
|
||||
# macOS has no enforced `ulimit -v`, so the guard is a poller, not a cap:
|
||||
# a process that allocates faster than one second's worth of headroom can
|
||||
# still overshoot briefly. Standard tooling only (ps, awk, kill).
|
||||
|
||||
set -u
|
||||
|
||||
max_gb=8
|
||||
max_s=1800
|
||||
while [ $# -gt 0 ]; do
|
||||
case $1 in
|
||||
-m) max_gb=$2; shift 2 ;;
|
||||
-t) max_s=$2; shift 2 ;;
|
||||
--) shift; break ;;
|
||||
*) echo "usage: $0 [-m MAX_GB] [-t MAX_SECONDS] -- <command...>" >&2; exit 2 ;;
|
||||
esac
|
||||
done
|
||||
[ $# -gt 0 ] || { echo "guard.sh: no command" >&2; exit 2; }
|
||||
|
||||
"$@" &
|
||||
root=$!
|
||||
start=$(date +%s)
|
||||
|
||||
# The command's process tree: descendants by parent pid, transitively.
|
||||
tree() {
|
||||
local all="$1" next
|
||||
while :; do
|
||||
next=$(ps -axo pid=,ppid= | awk -v set=" $all " '
|
||||
{ if (index(set, " " $2 " ") > 0 && index(set, " " $1 " ") == 0) printf "%s ", $1 }')
|
||||
[ -z "$next" ] && break
|
||||
all="$all $next"
|
||||
done
|
||||
echo "$all"
|
||||
}
|
||||
|
||||
kill_tree() {
|
||||
local pids; pids=$(tree "$root")
|
||||
kill -TERM $pids 2>/dev/null
|
||||
sleep 2
|
||||
kill -KILL $pids 2>/dev/null
|
||||
}
|
||||
|
||||
max_kb=$((max_gb * 1024 * 1024))
|
||||
while kill -0 "$root" 2>/dev/null; do
|
||||
pids=$(tree "$root")
|
||||
rss=$(ps -o rss= -p $(echo "$pids" | tr ' ' ',') 2>/dev/null | awk '{s+=$1} END {print s+0}')
|
||||
now=$(date +%s)
|
||||
if [ "$rss" -gt "$max_kb" ]; then
|
||||
echo "guard.sh: memory ${rss} kB exceeded ${max_gb} GB — killing: $*" >&2
|
||||
kill_tree; exit 137
|
||||
fi
|
||||
if [ $((now - start)) -gt "$max_s" ]; then
|
||||
echo "guard.sh: ${max_s} s exceeded — killing: $*" >&2
|
||||
kill_tree; exit 137
|
||||
fi
|
||||
sleep 1
|
||||
done
|
||||
wait "$root"
|
||||
Reference in new issue
Block a user