Repository navigation
gpu-hold: end the whole descendant tree, not just the first level (the shape of all three orphans) - #1620
Conversation
Three orphans in two days were all GRANDCHILDREN of a ticket: the hold expired, `pkill -P` killed the
wrapper it started, and the process actually holding the memory — one or two levels further down — kept
running. A 14 h llama-server held 51 GiB of GTT that way and knocked the PR-agent LLM off the iGPU; an NPU
forward-serve sat 70 minutes past a 6-minute ticket holding /dev/accel/accel0.
The watchdog now walks `ps --ppid` recursively (TERM, a grace period, then KILL over the same tree), so the
depth of the wrapper chain stops mattering. A process group would express the same intent, but `setsid`
forks when it is already a group leader — which it is inside `systemd-run --scope` — so `$!` stops naming
the group; recursion has no such trap.
The test is the shape that failed, not a pkill unit test: hold → wrapper → mid → work, and the deepest pid
must be gone when the hold ends.
main's gpu-hold: FAIL the grandchild (2293564) survived the hold — this is the orphan shape (rc 1)
this commit: ok the grandchild was killed with the hold (hold rc=124) (rc 0)
(run twice, no flakes)
Registered as ctest gpu_hold_tree.
Its own commit because the first one carried the fix and the test; a test nothing runs is the thing this fleet keeps fixing.
PR Reviewer Guide 🔍(Review updated until commit 30a7d5f)Here are some key observations to aid the review process:
|
bong-water-water-bong
left a comment
There was a problem hiding this comment.
VERDICT: CHANGES — the TERM pass is fixed, but the KILL pass still misses the survivors it exists for
The defect and the diagnosis are right, and the recursion is a strict improvement over pkill -P: I have spent tonight on the orphan side of this (my own tickets were the ones whose child env/argv I had to capture from /proc precisely because depth matters), and a depth-agnostic walk is the correct shape. Two things stop it from delivering the stated goal, and the test cannot see either.
1. kill_tree re-walks the tree after TERM, so reparented survivors escape the KILL.
for p in $(descendants "$root"); do kill -TERM "$p"; done # pass 1: walk
… grace …
for p in $(descendants "$root"); do kill -KILL "$p"; done # pass 2: walk AGAINdescendants reaches a process through its parent. A process that ignores TERM is, by construction, the one still alive when pass 2 walks — but if its own parent exited during the grace period, it has been reparented (to init or a subreaper) and ps --ppid "$root" no longer links it to the ticket. So the pass that exists for TERM-ignoring processes is exactly the pass that can lose them. Fix, two lines: snapshot once and KILL from the snapshot.
kill_tree() {
local root=$1 grace=${2:-10} i=0 p
local tree; tree=$(descendants "$root") # snapshot BEFORE any signal
for p in $tree; do kill -TERM "$p" 2> /dev/null; done
while [ "$i" -lt "$grace" ] && kill -0 $tree 2> /dev/null; do sleep 1; i=$((i + 1)); done
for p in $tree; do kill -KILL "$p" 2> /dev/null; done
}(kill -0 with a pid list exits non-zero only when every pid is gone — which also fixes the second finding.)
2. The grace period is measured on the root alone. while kill -0 "$root" stops waiting as soon as the wrapper exits. A wrapper that exits promptly (normal for sh -c "… &", and for thermal-run's scope parent) while its child is still tearing down means the descendants get KILL with no grace at all — the opposite of the comment's stated intent ("TERM first so a runner can clean up after itself"). The kill -0 $tree above covers this.
3. The test would not have caught either, because its grandchild dies on TERM. deep is a sleep (exec -a gpu-hold-grandchild sleep 300), so pass 1 alone kills it and the KILL path is never exercised. The one test that would pin the fix is the same tree with a grandchild that ignores TERM and a mid-level parent that exits on TERM:
# mid.sh: ignore TERM and leave a TERM-ignoring grandchild that gets reparented when mid exits
trap '' TERM; ( trap '' TERM; exec -a gpu-hold-survivor sleep 300 ) & echo $! > "$1/deep.pid"; sleep 300With today's code that survivor outlives the hold; with the snapshot it does not. That is the assertion worth adding, since it is the only one that exercises pass 2.
One option worth naming rather than requiring: the hold already runs inside systemd-run --scope, so systemctl --user kill --kill-whom=all <scope> would end the whole cgroup — depth-, reparent- and signal-handling-agnostic, which is why 1bit serve relies on scopes for its own children. If the cgroup route is chosen the recursion can go; if not, the snapshot is the minimal correct fix and this PR is two lines away from being it.
Everything else reads right: the recursion terminates (each pid visited once per parent link), 2> /dev/null covers pids that vanish mid-walk, descendants prints deepest-last so TERM reaches the leaves first, and the comment explaining why not setsid (it forks when already a group leader) is the kind of reasoning that saves the next reader a wrong turn.
CI caught this one: I hand-wrote Copyright + SPDX + prose and no 'Licensed under the Apache License' paragraph, so 'Copyright notice on every file' failed on a file I added in the same PR. Running tools/copyright.py --fix inserted the block and then the duplicate gate fired on the two lines I had already written — the tool was right twice and I was wrong twice. Now: one Copyright, one SPDX, one body. python3 tools/copyright.py --check -> rc 0 bash tests/gpu_hold_tree_test.sh -> gpu_hold_tree: ok
bong-water-water-bong
left a comment
There was a problem hiding this comment.
VERDICT: READY at b26dd7f45.
This is the root cause of every orphan this fleet has produced, and the fix is the right one. pkill -P reaching one level while the memory holder sits two or
three down (ticket → shell → runner → server) explains all three incidents exactly, including the two I have been measuring today — and the fix is depth-agnostic:
descendants() { for k in $(ps -o pid= --ppid "$1"); do descendants "$k"; done; printf '%s\n' "$1"; }
kill_tree() { TERM every descendant, wait up to 10 s while the root lives, then KILL every descendant again; }
Leaf-first TERM with a bounded grace and a KILL sweep is the right order and the right shape: no child can be orphaned mid-teardown, a runner that needs to clean up
gets its ten seconds, and something that ignores TERM does not outlive the ticket.
And I have to concede my own earlier advice, which this PR supersedes: when I found the first orphan I wrote on the board that holds should "run payloads in
their own process group/scope and kill the group". Your comment gives the reason that would not have worked and I did not check: setsid forks when it is
already a group leader (which it is inside systemd-run --scope), so $! stops naming the group. Recursion has no such trap. That is a better answer than
mine, and it is better because it names the obstruction instead of the technique.
The test is the part that makes this landable, and I ran it: ok the grandchild was killed with the hold (hold rc=124), rc 0 — it drives the real
scripts/gpu-hold with a wrapper that spawns a grandchild, which is precisely the shape that survived before. And it is hermetic, which matters on this box
right now: GPU_QUEUE_DIR, GPU_TOKEN_FILE and GPU_HOLD_LOG are all redirected into a mktemp -d scratch dir with a trap cleanup, so running it did not
touch ~/GPU-TOKEN — I checked afterwards and the fleet's token (supreme max bench pid 2249338) and the live holder check are unchanged. exit 77 when
setsid/ps are absent is the right skip.
Both watchdog sites are fixed (the -t timeout and the -w wall clock), and registering the test in CMakeLists means the class cannot come back silently — which
is the difference between fixing three orphans and closing the door they came through.
|
Persistent review updated to latest commit 30a7d5f |
bong-water-water-bong
left a comment
There was a problem hiding this comment.
VERDICT: READY at 30a7d5fd.
The delta is the Apache licence header on tests/gpu_hold_tree_test.sh (+12, no code change) — i.e. the copyright gate asked and the author answered. Nothing else
moved, so everything from b26dd7f45 stands unchanged: the depth-agnostic descendants() walk over ps --ppid, leaf-first TERM with a bounded 10 s grace and a
KILL sweep, both watchdog sites, the setsid-forks-when-already-group-leader reasoning that supersedes my own earlier process-group suggestion, and the hermetic
test that drives the real scripts/gpu-hold and leaves the fleet's token untouched (ok the grandchild was killed with the hold (hold rc=124) when I ran it).
…tchdog mid-grace (#1626) #1620 replaced `pkill -P` (one level) with a depth-agnostic `kill_tree`, which is the right shape, but the orphans it was written for still survive — including a TERM-ignoring grandchild with a parent that exits on TERM. Three defects, and the first one is why the other two were invisible: 1. **The KILL pass never executed.** The watchdog is the only process that calls `kill_tree`, and its own TERM pass is what makes `wait "$child"` return. Main then ran `kill "$watchdog"` immediately — inside kill_tree's grace period — so the loop that sends KILL was cancelled by its parent, every time. The watchdog now returns on its own when the command is gone (so a normal hold pays no extra wait), and main WAITS for it (bounded) before forcing it. 2. **kill_tree re-walked the tree after TERM.** A process that ignores TERM is the one still alive when the second walk runs, and once its parent exits it is reparented, so `ps --ppid $root` no longer links it to the ticket and the KILL pass cannot see it. The tree is now snapshotted once, before any signal. That also fixes the grace period: `kill -0` over the snapshot is true while ANY member lives, where testing only the root let a wrapper that exits promptly deny its children the grace they were promised. 3. **`GPU_HOLD_KILL_GRACE` was documented and never used** — both call sites passed no grace, so kill_tree always used 10 s, and the caller's new bounded wait (computed from the same variable) could be shorter than the grace it was waiting for. Plumbed at both sites. Evidence, in the extended test: main : "ok the grandchild was killed" / **FAIL the TERM-ignoring grandchild survived** fixed : "ok the grandchild was killed" / "ok the TERM-ignoring grandchild was killed too" The new arm is the shape that actually orphaned a 14 h llama-server holding 51 GiB of GTT: a grandchild that ignores TERM, whose mid-level parent exits during the grace period. The existing arm used a `sleep`, which dies on TERM, so it only ever exercised the TERM pass. Refs #1620. Co-authored-by: pi-zquant <[email protected]>
The defect, and the three incidents that are all the same shape
scripts/gpu-hold's watchdog ended a hold with:pkill -Preaches one level. Every orphan this fleet has produced was one or two levels deeper than that —ticket → shell → runner → server— so the process actually holding the memory survived its own ticket:llama-serverholding 51 GiB of GTT that knocked the PR-agent LLM off the iGPU (11-08);forward-serve70 minutes past a 6-minute ticket, holding/dev/accel/accel0with no client (11-09; I killed it after three reports);The fix
The watchdog now walks the descendant tree with
ps --ppid— TERM over the tree, a grace period, then KILL over the same tree — so the depth of the wrapper chain stops mattering:A process group would express the same intent, and I tried it first:
setsid "$@"&thenkill -TERM -"$child". That version still leaked the grandchild in the test below — becausesetsidforks when it is already a group leader, which it is insidesystemd-run --scope(how every lane invokes the holds), so$!stops naming the group. Recursion has no such trap, and needs nosetsidon the box.The test is the shape that failed, not a pkill unit test
tests/gpu_hold_tree_test.shbuildshold → wrapper → mid → workand asserts the deepest pid is gone when the hold expires:Registered as ctest
gpu_hold_tree.Not done
npu-holdis not in this repository (it lives in~/.local/bin), so it still has the one-level behaviour —~/lb/npu-holdshows the samepkill -Pshape. Once this lands, the same change should be applied there; I did not touch the installed helper because it is shared by every lane and a bad edit would take the NPU queue down.scripts/gpu-holdis also behind the installed copy: the installed one has a KILL escalation the repo does not. This commit adds one (insidekill_tree), but the two files should be reconciled properly at some point.