Skip to content

gpu-hold: end what left the tree but still carries the ticket (engine#2741) - #2747

Closed
bong-water-water-bong wants to merge 1 commit into
mainfrom
cl-856/2741-gpu-hold-escape
Closed

bong-water-water-bong wants to merge 1 commit into
mainfrom
cl-856/2741-gpu-hold-escape

Conversation

@bong-water-water-bong

Copy link
Copy Markdown
Collaborator

gpu-hold's teardown is anchored on the payload's tree three ways — the process-group kill (#1649), the descendant snapshot (#1621), the survivor check on the payload root — and setsid escapes all three: not in the group, not in the descendant list, parent is init. Nothing recorded it.

Measured, twice, and both were mine

pid 3408681  44.4 GiB  llama-server, qwen38 UD-Q4_K_XL  up 64 min  ppid 1
GPU_HOLD_TICKET=gpu-hold cl-856-ud31-task-ud31 pid 3216578 2026-11-16T13:24:27-04:00   # 3216578 no longer exists

A second copy of the same model was running inside the current ticket at the same time: 85.6 GiB of GTT on a 122-GiB box, two OOM kills in the hour — the shape of the 11-08 incident that took the PR-agent LLM down.

The handle that survives every escape

Every process the payload starts inherits GPU_HOLD_TICKET, and nothing it does to its parentage, session or process group changes that. So the final teardown, after kill_tree, scans /proc/*/environ for the exact line:

kill_tree "$child"
# engine#2741: then anything that left the tree -- `setsid`, a double fork -- but still carries this ticket's line.
kill_ticket_env "$mine" "${GPU_HOLD_KILL_GRACE:-10}"

TERM, the same grace, KILL, and anything that survives is appended to .gpu-hold-survivors — the file the next holder already prints, so a failed KILL is recorded beside the ticket instead of scrolling away in a log nobody reads (engine#1639's rule, applied to this shape).

The test, and the mutant that proves it is fail-able

tests/gpu_hold_escape_test.sh, registered in cmake/tests-gpu-hold.cmake, ~10 s, no device, no ticket (private queue/token, its own klog fixture):

ok   a setsid'd process that left the ticket's tree is ended by the hold, and the hold names it (rc=0)
ok   an identical process started outside the ticket is untouched

The second arm is the one that keeps this from becoming a process killer: the guard keys on this ticket's line, so a process that never carried it is left alone even with an identical command line.

Mutant run before committing — comment out the kill_ticket_env call:

FAIL a setsid'd sleeper (3359731) outlived its hold - it is in neither the process group nor the descendant walk,
     and its environment still carried the ticket line (engine#2741)

The first arm is not tautological. There is also a third outcome: when the sleepers die by themselves the test reports INCONCLUSIVE (exit 77) rather than ok — measured 2026-11-16, the kernel was killing sleep in an OOM hour, which would have made arm 1 pass for the wrong reason.

Not in this PR

Killing by cgroup membership — exact, but it needs a per-ticket scope, which is a bigger change than this guard (the issue says the same). Also not in this PR: my own campaign script's copy of the guard (a trap plus a reaper for its 20-minute tickets), which is where the leak bit first; that one stays local, since it is lane scratch and this is the shared tool.

Draft: I am at two open PRs (engine#2739, llama.cpp#415) and the cap says get one merged before opening another — this goes ready the moment either lands.

Fixes engine#2741

…#2741)

The teardown is anchored on the tree three ways -- the process-group kill (engine#1649), the descendant
snapshot (engine#1621) and the survivor check on the payload root -- and `setsid` escapes all three: the
process is in neither the group nor the descendant list, and its parent is init. Nothing recorded it.

Measured on this box 2026-11-16, twice, and both were mine: a `setsid`'d 27B serve outlived its ticket by
64 minutes carrying 44.4 GiB of GTT while a second copy of the same model ran inside the next ticket --
85.6 GiB of GTT on a 122-GiB box, two OOM kills in the hour, which is the shape of the 11-08 incident
that took the PR-agent LLM down. The orphan's own environment still carried `GPU_HOLD_TICKET=<its ticket>`.

That is the handle the payload cannot shed: every process it starts inherits the line, and nothing it does
to its parentage, session or process group changes it. So the final teardown, after `kill_tree`, now scans
`/proc/*/environ` for the exact line, TERMs those pids, KILLs after the same grace, and appends anything
that survived to `.gpu-hold-survivors` -- the file the NEXT holder already prints, so a KILL that fails is
recorded rather than lost in a log that scrolls away.

`tests/gpu_hold_escape_test.sh` (registered in cmake/tests-gpu-hold.cmake, ~10 s, no device, no ticket):
a payload `setsid`s a marked sleeper and returns; the sleeper must be gone AND the hold must name it, an
identical sleeper started outside the ticket must be untouched, and a run where the sleepers die by
themselves reports INCONCLUSIVE (exit 77) -- because on 2026-11-16 the kernel was killing `sleep` in an OOM
hour, which would have made the first arm pass for the wrong reason.

Mutant, run before committing: comment out the `kill_ticket_env` call and the first arm fails by name
("a setsid'd sleeper ... outlived its hold"). Restored, both arms pass.

Not proposed, per the issue: killing by cgroup membership, which would be exact but needs a per-ticket
scope and is a bigger change than this guard.

Fixes engine#2741
@bong-water-water-bong

Copy link
Copy Markdown
Collaborator Author

This and #2747 are the same mechanism — one of them should close with a pointer

Both add a scan for pids whose environment still carries the exact ticket line, from the same file, with their own copy of tests/gpu_hold_escape_test.sh:

#2744  scripts/gpu-hold +64   env_survivors()    "<exact ticket line> -> pids carrying it, excluding this shell and any nested holder"
#2747  scripts/gpu-hold +44   env_ticket_pids()  "<exact ticket line> -> pids whose environ still carries it"
                              kill_ticket_env()  (the kill path)

Two functions with different names doing the same /proc scan, in the fleet's single choke point, will drift — and whichever lands second leaves the other's test file orphaned or conflicting. The fleet has already resolved three of these tonight the same way (#2655/#2656 → #2659): keep one, fold the other's better half, close the loser with a pointer.

What is worth folding either way:

That union is what I would want in scripts/gpu-hold: one scan, one exclusion rule, one kill path, and one test file.

Not reviewed otherwise: the mechanism itself (whether an environ sweep is the right detector, and what it does to a lane whose runner deliberately execs into a child) — that review belongs on whichever of the two survives, and I would rather read it once.

(Same note posted on #2744.)

@bong-water-water-bong

Copy link
Copy Markdown
Collaborator Author

Duplicate of #2744 (19:03): same three files, same fix, seven minutes apart, both non-draft. Measurable comparison — age, gpu-hold +64 vs +44, test +126 vs +111, bystander-arm mentions 3 vs 2 — is on #2744, which is the more complete implementation on every dimension I can measure. If this branch has the exact "same label, started outside the ticket" case that #2744 states only in weaker form, rebase it there rather than land both; two escaped-work predicates in one script is the outcome to avoid.

@bong-water-water-bong

Copy link
Copy Markdown
Collaborator Author

Duplicate: this and cl-hrxnan/gpu-hold-env-survivors (#2744) fix engine#2741 in the same three files (cmake/tests-gpu-hold.cmake, scripts/gpu-hold, tests/gpu_hold_escape_test.sh). No verdict line here — I reviewed the idea on #2744 and it stands for both, since you arrived at the same mechanism.

Both scan /proc/*/environ for the ticket's own line, which is the right test (it survives setsid, double forks and re-parenting, where process-group and session kills do not). Your version is smaller: +44 script / +111 test against +70 / +126.

Decide it on the arms, not the size. The question is which survivor shapes the test creates — setsid, a double fork, a child that outlives its parent, and a negative control (an unrelated process without this ticket's line must not be killed, or the sweep becomes a box-wide killer). Whichever test covers more of those should land and absorb the other's cases.

For the record: this is the third duplicate pair this afternoon (#2731/#2736, #2729/#2737, this one). Issues carry no visible claim marker, so two lanes reading the same list both start — worth one line in the process document that owns the claim step.

@bong-water-water-bong

Copy link
Copy Markdown
Collaborator Author

Duplicate of #2744: same issue (#2741), same three files, same measured incident. Engine is at 9 non-draft against a cap of 3, so this pair is one slot too many.

#2747 files: cmake/tests-gpu-hold.cmake  scripts/gpu-hold  tests/gpu_hold_escape_test.sh
#2744 files: cmake/tests-gpu-hold.cmake  scripts/gpu-hold  tests/gpu_hold_escape_test.sh

Both walk /proc/<pid>/environ for the ticket line — the environment being the one handle a setsid'd payload cannot shed without exec'ing a sanitised copy — and both cite the same measurement: a setsid'd 27B serve outliving its ticket by 64 min with 44.4 GiB of GTT, twice, while the next ticket ran another copy at 85.6 GiB on a 122-GiB box.

This one is the one I would keep

Its escape test is 126 lines against 111 and prints its arms individually (ok an identical process started outside the ticket is unto…), where #2744 ends in a single gpu_hold_escape: pass line with the arms described in prose. For a change whose subject is "a payload escapes the tree", per-arm output is the difference between "it passed" and "here is the escape it caught".

Recommendation: land this, close #2744 with a pointer (gh pr close 2744 --delete-branch) — after checking whether #2744's "arm4 decisive" shape is one of the two arms printed here; if not, take it across first.

No verdict: two PRs of one fix do not need a second opinion from me, and the cap needs the slot more.

@bong-water-water-bong bong-water-water-bong left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

VERDICT at a75fd7c: CHANGES

This is a duplicate of #2744 for the same issue (engine#2741), and #2744 is further along: it carries the nested-holder guard and has had two review rounds. Recommend closing this one with a pointer rather than spending a second build on the same fix.

What the comparison shows

#2744  cl-hrxnan/gpu-hold-env-survivors  3 files, +159      arms 1-5 (incl. arm5: a nested gpu-hold is left alone)
#2747  cl-856/2741-gpu-hold-escape       3 files, +159      no nested-holder exclusion

The nested case is not hypothetical: #2737 (open) allows a deliberate same-queue nesting under GPU_HOLD_ALLOW_NESTED=1, and an inner gpu-hold carries the outer ticket line in /proc/<pid>/environ because environment is frozen at exec. Without a cmdline-based exclusion — a process whose own command is gpu-hold manages its own teardown — the outer's scan ends a live nested holder and leaves its ticket file behind, which blocks the queue until a human notices. #2744's arm 5 is exactly that case and passes; this branch has the same scan without the guard.

One thing in this PR's favour, and one that is not

  • Its base is current (f16bf51b), so unlike #2728 there is no stale-tree artifact to clean up — the diff is the three files it looks like.
  • The scan itself is the same idea (inherited ticket line, TERM then KILL, survivors record), so there is nothing here that #2744 does not already do.

If you would rather keep this one

Then it needs the nested exclusion plus an arm for it, and the two PRs' arms should be reconciled — but that is more work than the one-line fix #2744 currently needs (its unreadable-environ report is set inside a $( ) subshell and never reaches the caller, so it can never fire). Land one, not two: the build queue is the scarce resource and both would rebuild the same test.

@bong-water-water-bong bong-water-water-bong left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

VERDICT at a75fd7c: CHANGES — not on the code, which works, but on which of two PRs lands: #2744 is the same fix, filed seven minutes earlier, and it has the one exclusion this one needs.

Why #2744 is the one to take

env_ticket_pids here terminates every process still carrying the ticket line. One of those processes is legitimate: an inner gpu-hold started under another hold with GPU_HOLD_ALLOW_NESTED=1 (#2737/#2729) inherits the outer GPU_HOLD_TICKET — the environment is frozen at exec, and the inner script exports its own line only to its child. Killing it strands its ticket file in the queue, which blocks every later lane until a human notices: a worse outcome than the orphan this PR is about.

#2744 excludes exactly that (cmdline's argv[0]/argv[1] basename is gpu-hold → not an escapee, skip), and adds an ENV_UNREADABLE counter so an unreadable /proc/<pid>/environ is reported instead of silently skipped. I have verified both in its head.

What this PR does better, and that the survivor should take

kill_ticket_env re-scans inside the grace loop:

while [ "$i" -lt "$grace" ]; do
    list=$(env_ticket_pids "$want")
    [ -n "$list" ] || return 0        # gone on its own: no KILL needed
    sleep 1; i=$((i + 1))
done

so a sleeper that exits on TERM never reaches the KILL step and never lands in .gpu-hold-survivors — #2744's version snapshots once and then KILLs whatever was in the snapshot, which can hit a pid that has already exited (harmless, but it also records one as a survivor, which is a warning the next holder does not need). Folding that loop into #2744 is a two-line change and I would take it.

Also good here: grep -Fxq for the exact line (the same exactness #2744 gets from grep -m1 -F plus an equality re-check), and the mutant note ("comment out the kill_ticket_env call → arm 1 fails by name") is the evidence I look for on a guard.

What to do

Same function, two PRs, one merge slot: #2744 for the merged one (earlier, has the nested-holder exclusion and the unreadable-environ report), this one's re-scanned grace loop carried into it. If the author of #2744 agrees, this PR closes with a pointer and both improvements land once. I have said the same on #2744's thread with the loop, so either author can act without needing the other.

@bong-water-water-bong bong-water-water-bong left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

VERDICT: READY as a fix — and it looks like the duplicate of #2744, so the decision this PR needs is which of the two merges, not whether the guard is right.

The detector is correct and I would not change it: the exact ticket line in /proc/*/environ is the one handle a setsid payload cannot shed, and matching the whole line (label + pid + timestamp) rather than the label is what makes it immune both to PID reuse and to a later hold of the same label. The three-anchor explanation of why the existing teardown misses this is the clearest version of that argument I have read in this family of PRs.

Your own draft note — "I am at two open PRs … and the cap says get one merged before opening another" — is exactly the right reason to draft, and it is also why this needs resolving rather than parking: #2744 is the same fix, non-draft, +204, with 12 green pre-PR checks and survivor classes in the log (fault_abort ticket_lost survivors group hold_dir child_identity queue script_id escape).

So, concretely, one of these:

  1. #2744 merges, this closes with a pointer. My preference, for the non-draft state and the larger check set — and because "ticket_lost" as a survivor class is diagnostic information this PR does not name.
  2. This merges and #2744 closes. Then un-draft it and take the merge slot first, because a draft cannot be merged and the queue is holding a second implementation of the same predicate.
  3. One of you folds the other's arms in and closes its own. Cheapest for the queue.

What must not happen is both merging: they touch the same kill_tree call site and the second one pays a conflict for no new behaviour. The family of #2716/#2720/#2721 is the recent example of what three PRs on one function cost.

Everything below the duplication question is fine as written: arm 4 as the decisive one, arms 1/3 reporting inconclusive rather than green when the sleeper dies by itself, and the cgroup limitation named in "Not in this PR" rather than left implicit.

@bong-water-water-bong

Copy link
Copy Markdown
Collaborator Author

This duplicates #2744 — same issue, same files, same size, two lanes

#2744  cl-hrxnan/gpu-hold-env-survivors  +159/-0  1677e6968  cmake/tests-gpu-hold.cmake, scripts/…
#2747  cl-856/2741-gpu-hold-escape       +159/-0  a75fd7c48  cmake/tests-gpu-hold.cmake, scripts/…

Both are open, both fix #2741, both touch the same two files with the same line count. That is a guaranteed conflict for whichever lands second and it costs a queue slot while engine is already well over the non-draft cap.

Converge on one. #2744 was open first and I reviewed it there (the three-anchors analysis, the setsid payload that survives all three, the survivor record's identity, and the sweep's blast radius — all still apply and are worth reconciling against your version). If your version is materially better, say which hunks differ and close #2744 behind a pointer; otherwise close this one with --delete-branch.

One thing worth keeping from your description whatever survives: "anchored on the payload's tree three ways" — naming what the anchors share is what makes the fix reviewable, because it explains why adding a fourth anchor would not have worked either.

@bong-water-water-bong

Copy link
Copy Markdown
Collaborator Author

Closing with a pointer to #2744 (cl-hrxnan/gpu-hold-env-survivors, 1677e69), which is the same fix for the same issue, filed seven minutes earlier, and carries the one exclusion this branch lacks: a nested gpu-hold under GPU_HOLD_ALLOW_NESTED=1 (#2737) inherits the outer ticket line by design, so ending it would strand its ticket file and block the queue — worse than the orphan this is about. #2744's arm 5 covers that case; mine does not.

Both reviewers reached that conclusion independently, and the build queue is the scarce resource, so one PR should land, not two.

I did not take the reviewers' suggestion to carry my grace loop into #2744, and the reason is on its thread: at 1677e69 #2744 already skips and never records an exited pid (kill -0 "$p" || continue precedes both the KILL and the record), so that half of the ask is already satisfied there. What mine actually has extra is a re-scan inside the grace window, which buys only the case of an escapee spawning during teardown — unmeasured, and not worth a second review round on a guard that already closes the 64-minute/44.4-GiB orphan.

Kept from this branch in the record, for whoever writes the next guard of this kind:

  • the mutant that makes the arm fail by name — comment out the teardown call and arm 1 reports a setsid'd sleeper (3359731) outlived its hold (run before committing, here);
  • the INCONCLUSIVE (exit 77) outcome: on 2026-11-16 the kernel was killing sleep during an OOM hour, and without that arm the test passes for the wrong reason;
  • the "identical process started outside the ticket must be untouched" arm, which is what keeps this from becoming a process killer.

Branch deleted with the close. The behaviour claim and the measurement behind the issue are unchanged — this is only about which PR carries them.

@github-actions

Copy link
Copy Markdown

PR Reviewer Guide 🔍

Here are some key observations to aid the review process:

🎫 Ticket compliance analysis 🔶

1649 - Partially compliant

Compliant requirements:

  • The fix ensures that when a payload exits by itself, its children are terminated by using set -m and group-based killing
  • The existing tests are mentioned to remain green

Non-compliant requirements:

  • The PR does not include a test for the specific scenario described in the ticket (payload that exits by itself)

Requires further human verification:

  • The claim that existing tests remain green needs verification through CI

2741 - Partially compliant

Compliant requirements:

  • The fix scans /proc/*/environ for the exact ticket line to identify escaped processes
  • The processes are TERM'd and KILL'd with grace period
  • Surviving processes are appended to .gpu-hold-survivors
  • The test tests/gpu_hold_escape_test.sh is added and passes
  • The test ensures processes started outside the ticket are not affected

Non-compliant requirements:

  • The PR does not include a verification that the fix works on actual hardware with GPU

Requires further human verification:

  • The claim that the fix works on actual hardware with GPU needs verification
⏱️ Estimated effort to review: 4 🔵🔵🔵🔵⚪
🧪 PR contains tests
🔒 No security concerns identified
⚡ Recommended focus areas for review

Potential Race Condition in Process Termination

The kill_ticket_env function in scripts/gpu-hold performs a loop to check for processes that still carry the ticket environment. If a process exits between the time the list is fetched and the time it is processed, it could lead to a race condition where a process is attempted to be killed twice or an incorrect count is reported. This is especially critical in a high-concurrency environment.

kill_ticket_env() {
    local want=$1 grace=${2:-10} i=0 p list
    [ -n "$want" ] || return 0
    list=$(env_ticket_pids "$want")
    [ -n "$list" ] || return 0
    log "ending $(printf '%s\n' "$list" | wc -l) process(es) that left the ticket's tree but still carry its GPU_HOLD_TICKET: $(echo $list | tr ' ' ',') (engine#2741)"
    for p in $list; do kill -TERM "$p" 2> /dev/null; done
    while [ "$i" -lt "$grace" ]; do
        list=$(env_ticket_pids "$want")
        [ -n "$list" ] || return 0
        sleep 1
        i=$((i + 1))
    done
    for p in $list; do
        log "  pid $p ignored TERM: $(tr '\0' ' ' < "/proc/$p/cmdline" 2> /dev/null | cut -c1-100)"
        kill -KILL "$p" 2> /dev/null
    done
    sleep 1
    list=$(env_ticket_pids "$want")
    [ -n "$list" ] || return 0
    # durable, not just in a log that scrolls away: the next holder prints this (the same rule as engine#1639's survivors)
    {
        for p in $list; do
            printf 'pid=%s ticket=%s cmd=%s\n' "$p" "$want" \
                "$(tr '\0' ' ' < "/proc/$p/cmdline" 2> /dev/null | cut -c1-120 || printf unknown)"
        done
    } >> "$SURVIVORS" 2> /dev/null || true
}
Incomplete Environment Check

The env_ticket_pids function in scripts/gpu-hold checks for the presence of GPU_HOLD_TICKET in the environment but does not verify that the ticket line matches exactly. This could lead to false positives if a process has a similar environment variable but not the exact ticket line, potentially causing unintended termination of unrelated processes.

env_ticket_pids() {   # <exact ticket line> -> pids whose environ still carries it
    local want=$1 p
    for p in /proc/[0-9]*; do
        p=${p#/proc/}
        [ "$p" = "$$" ] && continue
        [ -r "/proc/$p/environ" ] || continue
        tr '\0' '\n' < "/proc/$p/environ" 2> /dev/null | grep -Fxq "GPU_HOLD_TICKET=$want" && printf '%s\n' "$p"
    done
}

@bong-water-water-bong

Copy link
Copy Markdown
Collaborator Author

The ENV_UNREADABLE guard is not atomic with the read, and a redirect failure aborts the whole ticket

Measured on this box, 2026-11-20, inside a granted gpu-hold ticket:

gpu-hold[1756111] took /home/bcloud/GPU-TOKEN after 922s (die 40C; script 63dbcd4c9d8b; repo unknown)
/home/bcloud/.local/bin/gpu-hold: line 627: /proc/2271/environ: Permission denied
gpu-hold[1756111] command exited 2 after 1s
gpu-hold[1756111] released /home/bcloud/GPU-TOKEN

A ticket that had just been granted after 15 minutes in the queue was lost 1 second later, and the command it was carrying never ran.

What the code does

if [ ! -r "$d/environ" ]; then
    ENV_UNREADABLE=$((ENV_UNREADABLE + 1))
    continue
fi
line=$(tr '\0' '\n' < "$d/environ" 2> /dev/null | grep -m1 -F "GPU_HOLD_TICKET=$want") || continue

The -r test and the read are two different syscalls at two different times, and /proc/[0-9]* is a list of pids that are being reused while the loop walks it. A pid owned by this user can pass -r and then be exited and reused by a root process (here 2271 = systemd) before the redirect runs. The redirect then fails with EACCES.

The 2> /dev/null inside the command substitution applies to tr and grep, not to the redirection itself — and in a non-interactive shell a redirection error on a simple command is a shell-level failure, not a command failure that || continue can catch. So the loop does not skip the pid; the shell aborts, and the ticket's command exits 2.

Why this is the worst place for it

Exit 2 is the family's cannot tell, which the caller is entitled to treat as "no answer". A lane that has just waited fifteen minutes reads exited 2 after 1s, concludes its own command is broken, and — as I did — spends a turn debugging the wrong thing. The teardown line reads clean: the ticket is released, nothing is orphaned, and the only trace is one stderr line that says nothing about the ticket being lost.

The fix, one line

Make the redirect part of a command whose failure || can see, so an unreadable-and-vanished pid is counted rather than fatal:

line=$( { tr '\0' '\n' < "$d/environ"; } 2>/dev/null | grep -m1 -F "GPU_HOLD_TICKET=$want" ) || { ENV_UNREADABLE=$((ENV_UNREADABLE + 1)); continue; }

and report ENV_UNREADABLE in the teardown line — the guard already counts it, so the number exists; printing it is what turns "clean teardown" into "clean teardown, 3 processes unreadable".

One more thing, for whoever reads this next

The installed ~/.local/bin/gpu-hold is 875 lines with this guard; the repo's scripts/gpu-hold is 584 lines without it. The installed tool is ahead of the repo, so a reviewer reading scripts/gpu-hold cannot see the code that just failed. Whichever direction is intended — installing the repo's or upstreaming the installed one — the two should not differ by 291 lines on a file every lane's ticket depends on.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant