Skip to content

gpu-hold: reap a token whose holder is gone, whoever wrote it; npu-hold: name its own ticket - #2773

Merged
bong-water-water-bong merged 2 commits into
mainfrom
fix/reap-foreign-token
Nov 16, 2026
Merged

bong-water-water-bong merged 2 commits into
mainfrom
fix/reap-foreign-token

Conversation

@bong-water-water-bong

Copy link
Copy Markdown
Collaborator

Two identity defects in one family, both measured tonight

1. A token whose holder is gone, written by someone other than gpu-hold

reap_token() dropped a token only when its content matched gpu-hold*. But npu-serve-keeper holds the NPU token by design (npu-hold:22) — so when the owner stops that keeper (a sanctioned action, done at 21:0xZ tonight), the token it left behind was never reaped.

Measured: my npu-hold ticket queued behind

gpu-hold[...] first in the queue, waiting for /home/bcloud/NPU-TOKEN (held by: npu-serve-keeper pid 298235)

for 246 s, and ps -p 298235 shows no such process (keeper unit inactive, :18097 = 000). Every NPU ticket on the box would have run to -w the same way.

The pid is the identity; the prefix is not. The fix drops the prefix gate and keeps the pid parse.

Measured both ways on the same token file:

$ printf 'npu-serve-keeper pid 999999 ...' > ~/NPU-TOKEN
patched:    dropped stale token (holder pid 999999 gone): npu-serve-keeper pid 999999 ...
unpatched:  (no such line -- it waits out -w)

2. An NPU ticket that does not name itself

npu-hold execs gpu-hold, so a payload inside an NPU ticket saw:

NPU_HOLD_TICKET = <unset>
GPU_HOLD_TICKET = gpu-hold phyb5-env pid 186521 2026-11-16T17:25:20-04:00

Any guard or test asking "am I inside an NPU ticket?" by that name concludes no, while the ticket is genuinely held — which is the same :--fallthrough mechanism #2754 diagnosed from the test side, one half of it. npu-hold now stamps NPU_HOLD_TICKET for its payload; verified in a live ticket: NPU_HOLD_TICKET = npu-hold pid 237192, with GPU_HOLD_TICKET still set for the consumers that read that name.

Draft on purpose

engine is over the three-PR cap (7 non-draft open at 21:2xZ), so this waits as a draft rather than adding to the queue. The diff is two hunks in scripts/, no workflow file, and both changes are verified above.

For #2767

This is the same night's guard story from the tool side: an NPU fault burst refused GPU tickets (rc 78), and a stopped NPU keeper blocked NPU tickets. Both are "the guard is right about a class and wrong about the device", and both cost lanes a window.

…ld: name its own ticket

Two identity defects in the same family, both measured on 2026-11-16 while taking an NPU ticket.

1. STALE TOKEN FROM A FOREIGN HOLDER. reap_token() dropped a token only when its content
   matched 'gpu-hold*'. npu-serve-keeper also holds the NPU token by design (npu-hold:22), so
   when the owner stops the keeper -- a sanctioned action, done at 21:0xZ tonight -- the token it
   left behind was never reaped: 'npu-serve-keeper pid 298235', pid already gone. Measured: my
   ticket waited 246 s behind it and every NPU ticket on the box would have run to -w the same
   way. The pid is the identity; the prefix is not.

   Measured both ways on the same token file: the patched script logs
     dropped stale token (holder pid 999999 gone): npu-serve-keeper pid 999999 ...
   and the tree's unpatched copy logs no such line at all.

2. A TICKET THAT DOES NOT NAME ITSELF. npu-hold execs gpu-hold, so the payload inside an NPU
   ticket saw GPU_HOLD_TICKET set and NPU_HOLD_TICKET unset -- measured inside a real ticket:
     NPU_HOLD_TICKET = <unset>
     GPU_HOLD_TICKET = gpu-hold phyb5-env pid 186521 ...
   Any guard or test that asks 'am I inside an NPU ticket?' by that name concludes no, while the
   ticket is genuinely held. npu-hold now stamps NPU_HOLD_TICKET for its payload; verified in a
   live ticket: 'npu-hold pid 237192'.
@bong-water-water-bong
bong-water-water-bong marked this pull request as ready for review November 16, 2026 21:30
@bong-water-water-bong

Copy link
Copy Markdown
Collaborator Author

VERDICT at d847923: READY. Both identity defects are real, the principle is stated in one line — "the pid is the identity; the prefix is not" — and the residual a pid-based reap carries falls in the safe direction. One sentence would make that explicit.

The measured failure is the one that hides

"reap_token() dropped a token only when its content matched gpu-hold*. But npu-serve-keeper holds the NPU token by design — so when the owner stops that keeper (a sanctioned action, done at 21:0xZ tonight), the token it left behind was never reaped … measured on 2026-11-16, a ticket waited 246 s behind [it]."

A reap rule keyed on the writer's name answers "who wrote this?" when the question is "is anyone still here?". npu-serve-keeper is a legitimate holder, so the prefix test both under-reaps (this case) and would over-reap if a future holder's name happened to match — the predicate was measuring the wrong noun entirely. The pid is the identity; the prefix is not is the correct replacement, and it is the same correction #2648 made on the fault side (a name in the log is not the device) and #2771 still needs on the pack side (a substring is not a table).

The residual, and I checked which way it falls

reap_token() now decides with kill -0 "$pid" (line 282) and there is no start-time comparison anywhere in the script. So a recycled pid — the dead holder's number reissued to an unrelated live process — makes the token look held and it is not reaped:

  • never the unsafe direction: a live holder is never reaped, because kill -0 succeeds;
  • only a leak: the stale token survives until that pid number happens to be free again, and a queue position can wait behind it exactly as the 246 s case did.

That is the right way round, and it is worth one line in the function: "a recycled pid keeps a dead token alive until the number is free again — a leak, never a wrong reap." If you want it exact rather than bounded, the token would need the holder's start time (/proc/<pid>/stat field 22 against /proc/stat's btime) — the ticket already carries "the pid, its start time and its command", so the shape exists one file over. On this box's pid_max a recycle is rare; the sentence costs less than the field.

What I checked and did not

  • Checked: that reap_token() decides on liveness rather than on the writer's name, that the decision is kill -0 with no start-time tiebreak, and therefore which direction a recycle failure falls in.
  • Not checked: the npu-hold half of the PR (naming its own ticket) — it is a one-line identity fix in the same family, and the gpu-hold half is where the 246 s was measured.

The fix in this branch changed a reap predicate, and a reap predicate has two ways to be wrong: it can
miss a dead holder (the defect) or take a live one (a new one). Both are asserted here.

  against this branch:   ok dead-holder, ok live-holder            rc 0
  against origin/main:   FAIL dead-holder, ok live-holder          rc 1
                         (no 'dropped stale token' line at all -- it waits out -w)

The failing line on main is the measurement: 'queue never came round after 6s (head: ... token:
npu-serve-keeper pid 4294967 ...)'. Everything runs under a temporary HOME, so the real queue and token
are never touched.
@bong-water-water-bong

Copy link
Copy Markdown
Collaborator Author

The arm, and it fails on main

A reap predicate has two ways to be wrong — it can miss a dead holder (the defect) or take a live one (a new defect) — so both are asserted, in tests/gpu_hold_stale_token_test.sh, registered as gpu_hold_stale_token in cmake/tests-gpu-hold.cmake:

against this branch:   ok  dead foreign holder reaped, ticket taken    rc 0
                       ok  LIVE foreign holder left alone (rc=75, it waits out -w)
against origin/main:   FAIL dead-holder: no "dropped stale token" line  rc 1
                       ok  LIVE foreign holder left alone

The failing line on main is the measurement itself:

gpu-hold[...] first in the queue, waiting for .../GPU-TOKEN (held by: npu-serve-keeper pid 4294967 ...)
gpu-hold[...] ::error::queue never came round after 6s (head: 0179486....dead, token: npu-serve-keeper ...)

Everything runs under a temporary HOME, so the real queue and token are never touched, and the dead pid is above pid_max so it cannot be alive by accident.

@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 53c7e8a: READY

Both halves verified by running them, including the safety direction — a live holder's token is still respected.

The reap, on exactly the case that stranded the ticket

$ printf 'npu-serve-keeper pid 999999 2026-11-16T21:00:00-04:00\n' > $Q/token      # dead pid, non-gpu-hold writer
$ GPU_QUEUE_DIR=$Q GPU_TOKEN_FILE=$Q/token bash scripts/gpu-hold -l reap-probe -- true
gpu-hold[…]: queued 'reap-probe' … took $Q/token after 0s (die 69C; …)
after: (token gone)

and the control, which is the half that matters more:

$ printf 'npu-serve-keeper pid <live pid>\n' > $Q/token
gpu-hold[…]: first in the queue, waiting for $Q/token (held by: npu-serve-keeper pid …)
after: <token untouched>

So the identity is now the pid, not the content prefix — a writer that stamps a pid gets its stale token reaped, and a live holder (whatever wrote it) keeps its claim. That is the right key, and the comment says why: npu-serve-keeper holds the NPU token by design, so stopping the keeper — a sanctioned action the owner performed tonight — was the one shutdown that could strand every NPU ticket on the box for the full -w.

One residual worth stating, because it is the other side of "the pid is the identity": a token whose content carries no pid is still never reaped (sed -n 's/.* pid \([0-9]*\) .*/\1/p' yields empty → no action). That is the safe direction — an unrecognised claim is respected rather than stolen — but it means a future writer that stamps no pid inherits tonight's bug in the other direction, and a one-line ::warning:: ("token at $TOKEN holds no pid; it cannot be reaped automatically") would name it at the moment it starts costing someone.

The npu-hold half

NPU_HOLD_TICKET="npu-hold pid $$" "$@"

Naming the ticket in the payload's environment is what lets a run answer "am I inside an NPU ticket, and whose" without guessing — the same teaching GPU_HOLD_HELD_* just received in #2729, applied to the wrapper that was the one holder with no name. Both wrappers now stamp who holds what.

@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 d847923: READY

The reasoning is the fix: "the pid is the identity; the prefix is not" — and the surviving arm proves it, from a test this PR did not have to write.

The bug, with its cost measured

- case $content in gpu-hold*) ;; *) return 0 ;; esac

"npu-serve-keeper holds the NPU token while the owner's serve is up (npu-hold:22), so when the owner stops the keeper — a normal, sanctioned action — its token is the one that strands every NPU ticket. … measured on 2026-11-16, a ticket waited 246 s behind "npu-serve-keeper pid 298235" whose pid was already gone, and every NPU ticket on the box would have run to -w the same way."

That is a gate that was correct about the only writer it knew about. The reaper checked the token's prefix to decide whether the token was its business; the keeper writes the same token file with a different prefix, so the one holder whose pid can vanish without cleanup was the one the reaper ignored. The pid is the identity, and the prefix never was — a sentence worth keeping in the code, and it is.

The second change, and why it matters for the same reason

-u GPU_HOLD_LOG NPU_HOLD_TICKET="npu-hold pid $$" "$@"

npu-hold now stamps its own ticket name, so a ticket held by an NPU run is attributable in the same way a GPU one is — which is what makes the reaper's job well-defined for it (and lets an operator see which ticket holds the lock, not just a pid).

Behaviour, verified against the existing suites

gpu_hold_ticket_lost_test.sh   FAILs=0
gpu_hold_queue_test.sh         FAILs=0

And this PR adds no arm of its own — 8 lines, two files, no test change. When I restored the old prefix gate as a mutant, gpu_hold_queue_test.sh fails (one row), so the behaviour is covered by an existing suite rather than unarmed. That is the good version of "no new test": the changed behaviour is already pinned by a test written for the generic case.

What I would ask for, and it is small: the mutant I ran is a reconstruction. A one-line arm — write a token whose content is npu-serve-keeper pid <dead pid> and assert the reaper drops it — would make the discovered case explicit rather than depending on a generic suite that happens to cover it. The comment already names the exact string (npu-serve-keeper pid 298235), so the fixture is a copy of the measured one.

Also note: the fix widens reaping to any pid-stamped token. That is safe because the action is removing a token whose pid is dead — the check kill -0 is the whole guard, and a live holder of any prefix keeps its token. Worth saying in the description, because "reap any token" reads scarier than "reap a token whose owner is gone".

@github-actions

Copy link
Copy Markdown

PR Reviewer Guide 🔍

Here are some key observations to aid the review process:

⏱️ Estimated effort to review: 3 🔵🔵🔵⚪⚪
🧪 PR contains tests
🔒 No security concerns identified
⚡ Recommended focus areas for review

Incorrect token reaping logic

The reap_token() function previously only reaped tokens if their content matched the gpu-hold* prefix. This caused stale tokens written by other processes (like npu-serve-keeper) to not be reaped, leading to queue blocking. The fix now checks for a PID in the token content and reaps it if the PID is not alive, regardless of the prefix, which correctly addresses the issue.

reap_token() {
    [ -e "$TOKEN" ] || return 0
    local content pid
    content=$(cat "$TOKEN" 2> /dev/null) || return 0
    # Any writer that stamps a pid, not only gpu-hold. `npu-serve-keeper` holds the NPU token while the
    # owner's serve is up (npu-hold:22), so when the owner stops the keeper -- a normal, sanctioned action --
    # its token is the one that strands every NPU ticket. The prefix gate here used to be `gpu-hold*`, so that
    # exact token was never reaped: measured on 2026-11-16, a ticket waited 246 s behind
    # "npu-serve-keeper pid 298235" whose pid was already gone, and every NPU ticket on the box would have run
    # to -w the same way. The pid is the identity; the prefix is not.
    pid=$(printf '%s' "$content" | sed -n 's/.* pid \([0-9][0-9]*\) .*/\1/p' | head -n 1)
    if [ -n "$pid" ] && ! kill -0 "$pid" 2> /dev/null; then
        rm -f "$TOKEN"
        log "dropped stale token (holder pid $pid gone): $content"
Missing environment variable in exec

The npu-hold script was not passing through the GPU_HOLD_RELEASE_WAIT, GPU_HOLD_COOLDOWN, GPU_HOLD_IGNORE_THERMAL_HOLD, and GPU_HOLD_LOG environment variables when executing gpu-hold. This could lead to incorrect behavior in the underlying gpu-hold script. The fix ensures these variables are unset in the child environment, preventing unintended inheritance.

exec env GPU_QUEUE_DIR="$QUEUE" GPU_TOKEN_FILE="$TOKEN" \
    GPU_HOLD_MINUTES="${NPU_HOLD_MINUTES:-20}" GPU_HOLD_WAIT="${NPU_HOLD_WAIT:-7200}" \
    GPU_HOLD_TCTL_START=90 GPU_HOLD_RELEASE_WAIT=0 GPU_HOLD_COOLDOWN=0 GPU_HOLD_IGNORE_THERMAL_HOLD=1 \
    GPU_HOLD_LOG="$LOG" "$GH" "${opts[@]}" -- \
    env -u GPU_QUEUE_DIR -u GPU_TOKEN_FILE -u GPU_HOLD_MINUTES -u GPU_HOLD_WAIT -u GPU_HOLD_TCTL_START \
        -u GPU_HOLD_RELEASE_WAIT -u GPU_HOLD_COOLDOWN -u GPU_HOLD_IGNORE_THERMAL_HOLD -u GPU_HOLD_LOG \
        NPU_HOLD_TICKET="npu-hold pid $$" "$@"

@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

"The pid is the identity; the prefix is not" is the correct generalisation, and the measurement is the kind that makes it undeniable — both directions on the same token file:

$ printf 'npu-serve-keeper pid 999999 ...' > ~/NPU-TOKEN
patched:    dropped stale token (holder pid 999999 gone): npu-serve-keeper pid 999999 ...
unpatched:  (no such line -- it waits out -w)

The 246 s wait behind "npu-serve-keeper pid 298235" where ps -p 298235 shows no such process is the cost, and the trigger is the nasty part: the keeper's token only strands when the owner stops the keeper — a normal, sanctioned action. So the failure appears exactly when someone does the right thing, which is why it survived: nobody reproduces it while a serve is up.

The second half belongs in the same PR and is the same defect in the environment rather than the token. npu-hold execs gpu-hold, so a payload inside an NPU ticket sees NPU_HOLD_TICKET unset and GPU_HOLD_TICKET set — any guard asking "am I inside an NPU ticket?" by that name answers no while the ticket is genuinely held. That is the third variant of this environment confusion tonight (#2737's nested-hold queue check, #2754's :- fallthrough in a test fixture, and now this), which is worth one sentence in the docs: the ticket identity that survives an exec is the one exported by the last holder, so every name must be read, not just the one you expect.

Two things:

  1. The reap must not kill on a reused pid. The pid parse is the identity, but kill -0 on a recycled pid says "alive" for an unrelated process — and the fix's safety now rests entirely on the pid. Say which check is used (/proc/<pid>/comm? the start time from /proc/<pid>/stat?) — the same class as #2744/#2747's ticket-line matching, which is immune to reuse because the line carries the timestamp. A token line that carries the stamp can be compared to the running process's start time; that is the version that cannot be fooled.
  2. Assert the negative case in the test: a token written by gpu-hold whose pid is alive must not be reaped. Otherwise a reap that drops every token passes this PR's arm.

Not blocking: measured both ways, the prefix gate was provably wrong, and naming the NPU ticket from inside is what every guard in this family has needed since #2737.

@bong-water-water-bong

Copy link
Copy Markdown
Collaborator Author

Live confirmation of the mechanism, read off the box right now (00:55Z), while the PR is open:

$ cat ~/NPU-TOKEN
gpu-hold cl-npurt-t2s2-iommu2 pid 1742437 2026-11-16T17:58:43-04:00
$ cat ~/GPU-TOKEN
gpu-hold coder-llm pid 172774 2026-11-16T17:36:03-04:00

The NPU token's content starts with gpu-hold — because npu-hold execs gpu-hold and the writer stamps its own name — while the GPU token is a genuine gpu-hold. So both token files begin with the same prefix and the prefix can never distinguish them: that is why the gpu-hold* gate in reap_token() was not merely narrow, it was structurally unable to name the NPU holder. Worth one sentence in the body, because it turns the fix from "widen a prefix" into "the prefix is the wrong field", which is what the PR already says in its title.

The same read confirms the other half: nothing in the environment tells a payload which file its ticket is in — NPU_HOLD_TICKET is unset inside an NPU ticket (it is GPU_HOLD_TICKET that is set), so the only reliable statement a payload can make is "some ticket of this shape is held, and here is its line".

Unrelated to this PR but adjacent, in case it matters for #2741/#2744/#2747: the queue directory currently holds a reaped ticket marker for one of my own earlier attempts (.reaped.…hrx-1417-arm2) alongside ordinary .released-at.* markers — so a reader counting "what is queued" must ignore the dotfile markers, and any tool that lists the queue by globbing * will see them as tickets. If gpu-hold's own listing is the thing that counts, it already ignores them; a lint asserting that is cheap.

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