Skip to content

serve: a fair FIFO GPU queue (scripts/gpu-hold), used by the hardware gate - #501

Merged
bong-water-water-bong merged 2 commits into
mainfrom
1bit/gpu-hold
Oct 22, 2026
Merged

bong-water-water-bong merged 2 commits into
mainfrom
1bit/gpu-hold

Conversation

@bong-water-water-bong

Copy link
Copy Markdown
Collaborator

PR body — 1bit-MONSTER/engine, branch 1bit/gpu-hold, sha 7a65912

Title: serve: a fair FIFO GPU queue (scripts/gpu-hold), used by the hardware gate

Why

~/GPU-TOKEN has been a bare noclobber open for a year, and that has two failure modes that cost real time on
the gate box: waiters race (no order — an agent can be passed over for hours) and a holder that dies leaves the
file behind (nothing reaps it, so the box looks busy for ever). On 2026-10-21 both happened repeatedly: agents
lost the token for one to three hours, and the hardware gate's own wait gave up at 3600 s while other jobs
cycled through the token.

The change

New scripts/gpu-hold — one GPU job under a fair claim:

  1. Ticket: ~/GPU-QUEUE/<epoch-ns>.<label> written before waiting, pid and command inside it. Queue order
    is file-name order (fixed-width nanosecond stamps), so the oldest ticket runs first — arrival order.
  2. Claim: ~/GPU-TOKEN is created only when the ticket is the oldest live ticket, the token is absent, the
    die is at or below the start gate (75 C), and the label has waited its release window (180 s) since its own
    last release. The label goes into the token.
  3. Run: the command runs under that claim with a max-hold deadline (default 14 min) and a watchdog that
    aborts at 93 C. Exit is the command's, 124 if the deadline cut it short, 75 if the queue never came round.
  4. Release: token removed and ticket deleted however the holder exits (EXIT/INT/TERM/HUP).
  5. Reaping: tickets — and tokens — whose owner pid is gone are dropped by the next poller, so a SIGKILLed
    holder costs one poll interval instead of the rest of the day.
  6. Per-label release wait: the label that just released waits 180 s before holding again.

scripts/hw-gate.sh uses it when installed: the whole gate runs inside one claim (label
hw-gate run <id> pid <pid>, HW_GATE_HOLD_MINUTES default 45, queue wait HW_GATE_TOKEN_WAIT default
5400). HW_GATE_GPU_HOLD=0 keeps the old path, and a box without the helper behaves exactly as before. The
inner run gets GPU_HOLD=1 and HW_GATE_TOKEN=none, so it does not take the token twice.

The token file itself is unchanged — one file whose content names its holder — so the fleet can migrate one
wrapper at a time: gpu-hold users queue fairly among themselves and still wait for legacy holders; a legacy
wrapper does not see the queue and can still cut in front, it just is not fair. The protocol for the other
agents is written down in ~/amd-oss/GPU-TOKEN.md on the box, including the one-line migration of a wrapper.

Testing

Fake holders, isolated queue, no GPU work (/tmp/qtest.sh, /tmp/qtest2.sh, log /tmp/gpu-qtest*.log):

case result
three claims 1 s apart, 5 s each served A → B → C in arrival order, each waiting for the previous; queue and token empty afterwards
ticket whose owner pid is gone dropped, the live claim runs (dropped stale ticket …)
token left by a dead holder reaped (dropped stale token (holder pid 999999 gone))
label that just released waits its release window while another claim goes first
-t 0.05 (3 s) with sleep 30 exit 124 after 4 s, token released
fake sensor 80 C, gate 75 C never takes the token (holding off: die at 80C), exit 75 after its queue wait
fake sensor 94 C while holding exit 143 after 2 s (ABORT die at 94C), token released

That suite found two real defects in the first cut, both fixed: the watchdog was started before the command, so
it held an empty pid and neither the thermal abort nor the max hold stopped anything.

End to end on the real token, scripts/hw-gate.sh --models qwen3-0.6b --phases kat,placement: the gate
queued (gpu-hold[2127680] queued 'hw-gate pid 2127680' as 01792634292882393174.hw-gate_pid_2127680), saw the
token held by a legacy holder (held by: hw-gate run 39707855652 pid 1846734 …), waited 38 s, took it, ran
kat qwen3-0.6b PASS and placement qwen3-0.6b PASS (153 graphs), released, and left no ticket behind — which
is the backward-compatibility case the fleet actually needs while other agents still race.

bash -n on both scripts and python3 tools/copyright.py --check (exit 0) pass.

🤖 Generated with Claude Code

… gate

Agents on the gate box race each other for ~/GPU-TOKEN, and the race has starved work all day: the token is a
single file that whoever manages a noclobber open takes, so an agent can lose it for hours, a killed holder
leaves it behind, and nothing orders the waiters. The hardware gate's own wait gave up at 3600 s more than
once while other jobs cycled through the token.

scripts/gpu-hold runs one GPU job under a fair claim:

1. it enqueues a ticket ~/GPU-QUEUE/<epoch-ns>.<label> (pid and command inside the ticket), so queue order is
   file-name order and the oldest ticket runs first, whoever arrived first;
2. it creates ~/GPU-TOKEN only when its ticket is the oldest live ticket, the token is absent, the die is at
   or below the start gate (75 C), and its label has waited RELEASE_WAIT s (180) since it last released;
3. it runs the command under that hold with a max-hold deadline (default 14 min) and a watchdog that aborts the
   command at 93 C;
4. it always releases the token and removes its ticket, however it exits (EXIT/INT/TERM/HUP traps);
5. it drops tickets, and tokens, whose owner process is gone, so a killed agent cannot block the queue.

The token file is unchanged for everyone else: one file whose content names its holder. A wrapper that predates
this script simply makes the queue wait, and a queue holder makes a legacy wrapper wait, so the migration can
happen one agent at a time.

scripts/hw-gate.sh uses it when it is installed: the whole gate then runs inside one claim (label
"hw-gate run <id> pid <pid>", hold HW_GATE_HOLD_MINUTES default 45, queue wait HW_GATE_TOKEN_WAIT default
5400). HW_GATE_GPU_HOLD=0 keeps the old behaviour, and without the helper nothing changes. The inner run gets
GPU_HOLD=1 and HW_GATE_TOKEN=none, so it does not try to take the token a second time.

Tested with fake holders (sleeps) in an isolated queue, no GPU work: three claims made one second apart are
served A, B, C in arrival order, each waiting for the previous; a ticket whose owner pid is gone is dropped and
the queue moves on; a token left by a dead holder is reaped; a label that released waits its release window
before holding again while another claim goes first; the max hold cuts a long command short (exit 124); the
start gate refuses a hot die (no acquisition while the fake sensor reads 80 C against a 75 C gate); the abort
kills the command at 94 C (exit 143); and afterwards the queue and the token are empty. Two real defects were
found and fixed by that test: the watchdog was started before the command, so it held an empty pid and neither
the abort nor the max hold stopped anything.

The protocol is written down for the other agents in ~/amd-oss/GPU-TOKEN.md on the box.

Co-Authored-By: Claude Opus 5 <[email protected]>
@bong-water-water-bong bong-water-water-bong added the hw-gate Run the HRX hardware gate on Strix Halo (docs/hw-gate.md) label Oct 22, 2026
…from the shebang

The check keys the comment style off the file name patterns, so a new file with no extension (scripts/gpu-hold)
had no style at all and the copyright gate failed the build with "no comment style known":

  no comment style known (add one to LINE_STYLES/BLOCK_STYLES or EXEMPT): scripts/gpu-hold

A file whose name has no dot and whose first line starts with #! is a script, and a script takes the '#" line
style. Everything else keeps the old path: extensions match LINE_STYLES/BLOCK_STYLES first, and a file with no
extension and no shebang is still reported as unknown rather than guessed at.

The comment formatting of a line style is now one helper (_line_block) used by both the pattern path and the
shebang path, so the two cannot drift.

Checked: python3 tools/copyright.py --check exits 0 on this branch (scripts/gpu-hold carries the notice and its
shebang is line 1); a self-test of notice_block on five cases - the tracked script, an extension-less file with
and without a shebang, a *.sh, a *.md - gives '#' for the two scripts and the HTML block style for the markdown,
none for the extension-less file with no shebang; and --fix on a '#!'-headed script places the notice on line 2,
after the shebang, with has_notice() true afterwards.

Co-Authored-By: Claude Opus 5 <[email protected]>
@bong-water-water-bong
bong-water-water-bong merged commit 4b645cb into main Oct 22, 2026
8 checks passed
@bong-water-water-bong
bong-water-water-bong deleted the 1bit/gpu-hold branch October 22, 2026 02:27
@github-actions

Copy link
Copy Markdown

PR Reviewer Guide 🔍

Here are some key observations to aid the review process:

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

Possible Race Condition in Token Acquisition

The token acquisition logic in gpu-hold may be susceptible to a race condition. Specifically, after checking that the current ticket is the oldest live ticket and that the token is absent, the script attempts to create the token with noclobber. However, if another process manages to create the token between the check and the noclobber write, the script may not detect this and proceed incorrectly. This could lead to multiple processes holding the token simultaneously, violating the fairness guarantee.

else
    mine="gpu-hold $LABEL pid $$ $(date -Is)"
    if ( set -o noclobber; printf '%s\n' "$mine" > "$TOKEN" ) 2> /dev/null; then
        state=holding
        log "took $TOKEN after ${waited}s (die $(tctl)C)"
        break
    fi
Inaccurate Temperature Reading Handling

The tctl function in gpu-hold reads from TCTL_FILE and converts the value to Celsius. If the file is unreadable or contains invalid data, it defaults to 0. This default value of 0 could be misinterpreted as a safe temperature, potentially allowing a job to start when it should be blocked due to high temperature. The function should handle invalid or missing temperature data more robustly.

tctl() {
    local v
    v=$(cat "$TCTL_FILE" 2> /dev/null) || v=""
    if [ -n "$v" ]; then echo $((v / 1000)); else echo 0; fi
}
Potential Stale Ticket Reaping Issue

The oldest_ticket function in gpu-hold reaps stale tickets by checking if the ticket's process is alive. However, if the process ID is not found or is invalid, the ticket is considered stale and removed. This could lead to premature removal of valid tickets if the process ID parsing fails or if the process has already exited but the ticket file still exists.

stale() {
    local pid
    pid=$(pid_of "$1")
    if [ -z "$pid" ]; then return 0; fi          # unreadable or half-written: stale
    kill -0 "$pid" 2> /dev/null && return 1
    return 0
}

oldest_ticket() {
    local f pid
    for f in $(ls -1 "$QUEUE" 2> /dev/null | grep -v '^\.' | sort); do
        [ -f "$QUEUE/$f" ] || continue
        if stale "$QUEUE/$f"; then
            rm -f "$QUEUE/$f"
            log "dropped stale ticket $f (owner pid gone)"
            continue
        fi
        printf '%s' "$f"
        return
    done
}

bong-water-water-bong added a commit that referenced this pull request Nov 23, 2026
…e writeback case (llama.cpp#500, #501; engine#3408, engine#3402) (#3424)

- #500 (engine#3408): test-hrx-ops structural rows re-baselined or known-fail; the stale-expectation check reads both
  suites.
- #501 (engine#3402): mutable_host_input_after_writeback tests engine#1251's publish-before-return guarantee again.

Co-authored-by: bong-water-water-bong <[email protected]>
Co-authored-by: Claude Opus 5.5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

hw-gate Run the HRX hardware gate on Strix Halo (docs/hw-gate.md) Review effort 4/5

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant