Skip to content

tools: an ended gate run takes its binary's timeout with it - #201

Closed
juliankang4 wants to merge 31 commits into
midagedev:mainfrom
juliankang4:dgx-spark-gate-timeout-group
Closed

juliankang4 wants to merge 31 commits into
midagedev:mainfrom
juliankang4:dgx-spark-gate-timeout-group

Conversation

@juliankang4

@juliankang4 juliankang4 commented Oct 14, 2026 •

Copy link
Copy Markdown
Collaborator

What and why

Closes #171.

Release notes: internal: a gate or timing runner that is stopped with TERM, INT or HUP now ends the binary it started, so the binary's gate locks, output and timing lease go with the runner instead of staying held until the binary's own bound.

GNU timeout puts its command in a process group of its own, and the gate runners start their binary under timeout in the foreground. A TERM, INT or HUP sent to a runner's process group never reaches the binary or its timeout, and a TERM sent to the runner's pid ends the runner alone. Either way the binary runs on until its own bound and keeps what it holds:

  • tools/gpu-gate.sh: the gate lock descriptors and the writers of the run's output, up to 900 s when BLOOMERY_GATE_BOUND is unset.
  • tools/ref/time-gate.sh: the machine-wide lease, up to 600 s.
  • tools/host-gate.sh and tools/gate.sh: the binary, or cargo test, still writing to the run's output.
  • tools/ref/lease.sh's lease_bounded, which bounds the children the timing runners start under the lease: the lease (descriptor 9), up to LEASE_ARM_BOUND (900 s by default). Most timing runners also call it inside a command substitution or a pipeline (out=$(lease_bounded …)), where even a fix inside lease_bounded would not see a TERM sent to the runner's pid. The DS41 and Qwen3-MoE depth runners start their arms under a timeout of their own, piped into a fault marker. The DS41 and GLM5Next preheats and tools/ref/cstate-ab.sh's arms start theirs inside a command substitution (out=$(timeout … 2>&1)).
  • tools/ref/perf-decode.sh and tools/ref/load-groups.sh, which start the decoder or the load under a timeout of their own in the background and give the runner no way to stop it: the lease, up to that timeout's bound.

The gpu-gate self-test meets this when gate()'s bound ends a held run. The bound returns 124, but the stub binary and its timeout stay alive, and a later blocking flock on the same lock, or a relay() reading that output, waits for them.

The issue offers two fixes. This PR takes the first: the runner passes the signal on to the inner timeout and its process group, and waits for them.

  • run_bounded now lives in tools/gate-bound.sh, which gpu-gate.sh, host-gate.sh and gate.sh already source. It sets its trap first, then starts the bounded command in the background and waits for it. A signal that comes before the child's pid is known is recorded and passed on as soon as the pid is, so no signal falls between the start and the trap. BIN_SENT records that the trap has passed the signal on already, so the child gets it once. The trap ignores nothing before the child starts, because a child started with signals ignored would keep them. On TERM or HUP it stops that child with lease_stop, ignores further signals and waits until the child ends; the runner then goes on with the child's code. Stopping a child that has already ended is no error, under set -e too (time-gate.sh and the timing runners run under it), and the pid is cleared once the child is reaped, so a late signal reaches no other process. INT works the same way unless the runner itself was started with & by a shell, because bash leaves INT ignored in that case. The child keeps the runner's stdin (<&0), because a background command in a non-interactive shell reads /dev/null otherwise.
  • lease_stop <pid>, also in tools/gate-bound.sh, sends TERM to timeout and to its process group, waits up to 10 s for the group's live members (a zombie counts as ended), then sends KILL and waits up to 10 s more, and reaps timeout. A group still alive after that is reported ([bound] <pid> or its process group still alive 10 s after KILL) and lease_stop returns 70, as it does when it cannot read the process table. The TERM goes to the group too because GNU timeout that receives a TERM before it has returned from fork exits without signalling its command, and the command then keeps the lease or the lock in timeout's group.
  • All the gate runners go through run_bounded: gpu-gate.sh's three start paths (plain, BLOOMERY_GATE_STACKS and BLOOMERY_GATE_GDB), host-gate.sh, gate.sh, time-gate.sh and lease.sh's lease_bounded. lease.sh now sources gate-bound.sh beside lease-probe.sh, and time-gate.sh gets run_bounded through lease.sh and keeps its own 600 s bound.
  • A gate runner ends right after its binary, but a timing runner has more arms to run. So after the child has ended, lease_bounded sends the signal it passed on to the runner again (kill -s <signal> $$), and the runner stops, by its own trap where it set one, instead of starting its next arm. For that, run_bounded records the signal it passed on in BIN_SIGNAL and puts back the caller's own TERM, INT and HUP traps when it returns, instead of resetting them. dump.sh and dump-mtp.sh set exit 143 and exit 130 traps before they call lease_bounded, and a reset would drop them. The gate runners other than gate.sh set no such traps and ignore BIN_SIGNAL, so for them nothing changes.
  • A timing runner that needs a bounded command's output now calls it in its own shell and reads the output from a file, so the signal reaches run_bounded. lease.sh has three helpers for this:
    • lease_capture <seconds> <command…> runs lease_bounded with stdout and stderr in a temp file and puts them in LEASE_OUT, as $(… 2>&1) gave them.
    • lease_filter <seconds> <stdout|stderr> <file> <filter…> -- <command…> runs a live filter (the depth runners' majflt_mark, dma-dram-share.sh's stamp) on the command's output through a FIFO: stdout filters the combined output, stderr filters stderr and leaves stdout in LEASE_OUT_FILE. The filter runs with descriptor 9 closed, so it never holds the lease. The bound and the arguments are checked before the filter starts. Like run_bounded, lease_filter sets its trap before it starts the filter, and a signal that comes before the filter's pid is known is acted on as soon as it is: the trap ends and reaps the filter, puts back the caller's traps and sends the signal on to the runner. Without that, a filter still waiting to open the FIFO would outlive the runner and hold the runner's stderr open. When the command succeeds and the filter fails, lease_filter returns the filter's code.
    • lease_out_init makes the temp dir with that file and FIFO, and adds lease_out_clean to the runner's EXIT trap without replacing what is there. lease_bounded also takes LEASE_BOUNDED_OUT and LEASE_BOUNDED_ERR, for a caller that names its own files.
  • The timing runners use them. ab-decode.sh, depth-decode.sh, depth-gpu.sh, decode-measure.sh's two sweeps, governor-ab.sh and depth-glm5next.sh's other arms take their output through lease_capture; perf-decode.sh's three readers and governor-ab.sh's ik arm, whose stderr was dropped, write to files through LEASE_BOUNDED_OUT. depth-glm5next.sh's ours arms, depth-ds41.sh's and depth-qwen3moe.sh's reference arms, depth-ds41.sh's ours arm and dma-dram-share.sh's bench go through lease_filter, and depth-qwen3moe.sh's ours arms through lease_capture. The DS41 and Qwen3-MoE arms keep their $BOUND. The DS41 and GLM5Next preheats and cstate-ab.sh's arms take their output through lease_capture with the bound they had ($BOUND, and 600 s in cstate-ab.sh). qdot-rate.sh runs its rounds in its own shell into the sitting file and prints the file after them, and expert-union-dump.sh reads its prompts from a process substitution instead of a pipe into its loop.
  • perf-decode.sh now owns its background decoder. An EXIT trap stops the decoder's timeout and its group with lease_stop, and a TERM, INT or HUP that comes before the decoder's pid is known is held and acted on as soon as it is (exit 143, 130 or 129). Before, a TERM during perf record ended the profiler and then the runner, and the decoder and its timeout kept the lease. After a normal run the runner reaps the decoder and clears its pid, so the later perf script, annotate and report calls keep their usual signal handling. A decoder that fails now fails the run with its code, a reason (cut off at its <n> s bound for 124) and the last 20 lines of its log. Its symbol table's awk now writes to ("sort -rn | head -n " top) in parentheses, which BSD awk on the Mac needs.
  • load-groups.sh's lg_process runs a load of arms under one timeout in the background. It now sets a TERM, INT and HUP trap before it starts that timeout: the trap stops the load with lease_stop, puts back the caller's traps and sends the signal on to the runner, and a signal that comes before the load's pid is known is acted on as soon as it is. Its own stops (no line within the bound, a line past the last arm) also go through lease_stop. depth-qwen3moe.sh's memory scope (MEM_SCOPE_SCRIPT) passes a TERM, INT or HUP on to its command once, ignores further ones and waits again, so it returns the command's code; it used to stop waiting at the first signal.
  • With this, a failed filter now fails the arm when the command itself succeeded; the old pipelines kept only PIPESTATUS[0]. expert-union-dump.sh now stops with code 1 at the first failed prompt; its exit 1 used to end only the pipeline's subshell, and the script went on. dma-dram-share.sh now refuses a value with a leading zero (0600) with 64, as lease_bounded refuses such a bound; it used to refuse only 0.
  • The card tests and the three depth-*-stub.sh tests build temp trees with a copy of lease.sh; those trees now get tools/gate-bound.sh too. The lease self-test reads depth-qwen3moe.sh's arms through a variable path, so check-recipes' walk of the timing scripts does not count lease.sh as a caller of that lease-taking runner.
  • gate.sh with a test filter used to pipe cargo test into tee. In a pipeline, run_bounded would run in a subshell that a TERM to the runner does not reach. The output now reaches tee through a FIFO beside the log, and the runner waits for tee before it counts the passed tests. A FIFO that cannot be made ends the runner with 70 before cargo starts. tee starts before run_bounded sets its trap, so gate.sh sets a TERM, INT and HUP trap of its own before it starts tee: once tee's pid is known the trap exits (143, 130 or 129), and a signal that comes before that pid is known is acted on as soon as it is. The EXIT trap then ignores further signals, sends tee a KILL and reaps it before it removes the log and the FIFO, because the reader may still be the shell opening the FIFO, before tee has its default signal handlers. Without this, a TERM in that window ended the runner, and tee, still waiting to open the FIFO, held the runner's output open.
  • tools/ref/stack-watch.sh passes a TERM or HUP on to its command and to a gdb dump under way. It now sources gate-bound.sh and runs both the command and the dump through run_bounded, so a cancelled watch waits for each timeout's group, and a lock either child inherited is free when the watch returns. The dump runs in the background and is waited for. Before, a TERM that came during a dump was handled only after the dump, and an outer KILL in that window left the inner timeout and the binary behind. Once it has passed a signal on, the trap ignores further ones, because under GNU timeout the watch gets the bound's TERM twice (from the outer timeout's group signal and from the runner). The first signal still cuts short the wait it arrives in, so the watch makes each wait, for the dump and for the command, twice, as run_bounded does: it goes on only after the child has ended, and returns the command's code. The first wait of each pair is written wait ... || true, and each TERM the watch sends to a child that may have ended is written || :, so a watch run under bash -e still reaches its last waits. The trap also sets $stop. A signal that comes outside a wait (during the loop's one-second sleep) ends the loop at its next turn, and the watch then waits for the command and starts no dump. Without it, a command that took longer than QUIET to end after its TERM was dumped with a gdb that no signal would reach. As in run_bounded, the trap is set before the command starts: a signal before the command's pid is known sets $stop, and the command gets its TERM as soon as the pid is known, unless the trap has sent it one already ($told). The trap also ignores nothing from the last $stop check before a dump until the dump's pid is known ($spawning), because a dump started with TERM ignored can lose the TERM the watch sends it before timeout sets its own handler. A signal in that window goes on to the command, and the dump gets its TERM as soon as its pid is known. The dump writes to a file beside the watch's log, and the watch passes that file to stderr, prefixed, once the dump has ended, so no child of the dump outlives it. The dump's timeout sends a TERM at 120 s and, with --kill-after=10, a KILL ten seconds later. After a dump the watch gives the quiet process ten seconds to end before a KILL. It reads the process state with ps -o stat= and counts a zombie as ended; kill -0 succeeds on a zombie, so a process whose parent, or the subreaper it was reparented to, does not reap it held the watch for the full ten seconds. An INT reaches the watch's trap only when the watch runs in the foreground; gpu-gate.sh starts it with & and passes its own INT on as a TERM.
  • tools/ref/stub-timeout.py stands in for timeout on hosts without coreutils (the Mac). It now keeps coreutils' timing and exit codes. It sends TERM at its bound or when it receives a TERM. It sends KILL --kill-after seconds after the first of the two, and --kill-after=0 means no KILL. It exits 124 when the bound ran out and 137 when the KILL ended the command. It sets its TERM handler before it starts the command, so a TERM that comes right after the runner starts it reaches the command. It still makes no process group: only its direct child is signalled. Before, it sent KILL at once at its bound, and no trap can catch KILL, so the regression below could not pass on the Mac with the old stand-in.

The second option in the issue keeps both bounds in one process group. The simplest way to do that is timeout --foreground on the inner bound, and in that mode timeout does not end the command's own children when the bound expires. So I did not use it.

The tests:

  • gpu-gate self-test, part 1. gate() takes GB=<s> for its own bound (the default stays 60 s). The stub writes its pid and its parent's pid (the binary's timeout) to STUB_PID. With STUB_TERM=7 it ends with code 7 one second after a TERM. The case holds the stub and lets gate()'s bound end the run with GB=10, once plain and once under the watch. Right after the bound returns 124 it checks that the stub and its timeout are gone and the card lock is free. Then it checks that the output pipe reached EOF and that the runner's own exit line says exit 7. The processes are checked before the wait for EOF, so a runner that passes the TERM on and exits without waiting does not pass. The exit line catches a runner that returns the status of its interrupted wait (143) instead of the child's.
  • stack-watch self-test, six cases. In the first, a stub gdb on PATH holds, so the watch (QUIET 1) stays in its dump, and the stub ends only six seconds after its TERM; it sets its TERM trap before it writes the pid the case waits for. The case sends a TERM to the watch and a second one 1.5 s later. The watch must end within 20 s with the command's code (7, two seconds after its TERM), and neither the gdb nor the command may be left. In the second, the command goes on after the watch has ended its quiet sleep, and the case sends a TERM while the watch waits for the command. The watch must end with the command's 7, not 143, and the command must be gone. In the third, the command prints every 0.2 s until its TERM and then takes five seconds to end, longer than the quiet time (2 s), and the case sends the TERM a second after the command's pid is there. Its printing, not the TERM's timing, keeps the quiet time from running out before the TERM. No dump may start (the stub gdb writes its pid when it starts), the watch must return 4 to 7 s after the TERM with the command's 7, and the command must be gone. In the fourth, the command sets its TERM trap, then writes its pid and holds, and the case sends the TERM as soon as the pid is there, right after the command has started. The watch must end with the command's 7 and the command must be gone. The fifth and sixth put a timeout stand-in on PATH that makes its own group and exits on a TERM without passing it on, as GNU timeout can before fork returns, once for the command and once for the dump, with a lock the held child inherited. The case waits until the child and its timeout are there with the lock held, sends the watch a TERM, and the child must be gone and the lock free. A failed case prints the last line of what it read (for example dumping: yes, ended in N s, left: none) on its FAIL line, so a log that keeps only FAIL lines shows why.
  • card tests. gate.sh with a test filter passes cargo's output through and ends 78 when no test passed, which covers the FIFO path. A call whose FIFO cannot be made, because a link already holds its path, must end 70 before cargo runs and leave the linked file as it was. Four more cases plant a TERM or a HUP right after tee starts, before and after its pid is recorded: the runner must end with 143 or 129, cargo must never start, and tee must end.
  • lease self-test, signal cases. A runner holds a temp lease, runs a stub through the call under test, then prints next arm. The stub sets a trap that ends it with 7 one second after a TERM, then writes its pid and its parent's pid (its timeout) and holds. The case waits until that pid file is there and the stub's timeout is asleep waiting for it (polled every 0.1 s), checks that it got there, and sends a TERM to the runner's pid, once with no runner trap and once with a runner trap that prints trap ran and exits 143. Each time the runner must end with 143 before the call's 20 s bound, the stub and its timeout must be gone, the lease must be free and next arm must not appear; with the trap, trap ran must appear. The call is lease_bounded in the runner's shell, lease_capture (whose temp dir must be gone too), a lease_bounded whose copy of run_bounded waits between starting the child and reading its pid, so the TERM lands in that window, a lease_bounded under the timeout stand-in that exits without passing the TERM on, lg_process with a stub load (also under that stand-in; skipped where bash is older than 4.1, as /bin/bash 3.2 on a Mac is), and nine arms taken from depth-ds41.sh, depth-glm5next.sh, depth-qwen3moe.sh and cstate-ab.sh: the DS41 and Qwen3-MoE ours and reference arms, the DS41 pp arm, the Qwen3-MoE memory-capped arm, the DS41 and GLM5Next preheats (with a held stub in place of the preheat script) and cstate-ab.sh's arm loop.
  • lease self-test, memory scope. The real MEM_SCOPE_SCRIPT runs the held stub and gets two TERMs 0.1 s apart; it must return the stub's 7, and the stub must be gone.
  • lease self-test, perf cancellation. A copy of perf-decode.sh runs with a stub decoder and a stub perf, and the case sends the runner a TERM during perf record (also under the timeout stand-in), before the decoder's pid is assigned, and during annotate and during the callers report. Each time the runner must end with 143 before the 20 s bound, with the decoder, the profiler and their timeouts gone and the lease free; before the pid, no perf command may run. A decoder that ends with 23, and one that ends with 124, must fail the run with that code, the decode exited <code>: line and the last 20 lines of its log.
  • lease self-test, filter start. A copy of lease_filter waits between starting the filter and reading its pid, and the case sends the runner a TERM there, with no runner trap, with one that exits and with one that returns. The filter must be gone and the runner's stderr must reach EOF before the case cleans up, the lease must be free, and a trap that returns must find its own trap back afterwards. The case never signals the filter itself.
  • lease self-test, the rest. lease_capture's output and code; the EXIT trap it extends (a quoted one and one with a trailing comment, both still run), read in the runner's own shell with its positional arguments untouched; a mktemp that fails (code 70, empty output); lease_filter's two streams; a failed filter's code; a refused bound that starts no filter; dma-dram-share.sh refusing 0600; expert-union-dump.sh stopping at a failed prompt; and a command that reads the runner's stdin.

Left open:

  • run_bounded, lease_filter, gate.sh and the watch each set their trap before they start their child. The lease self-test reaches the first two windows with a planted wait, and the card tests reach gate.sh's with a signal planted right after tee starts. The watch's window before a dump's pid is read is reached only by a mutant under "Fail first", and no mutant in this run reaches the watch's window before the command's pid is read.
  • A binary that ignores the TERM gets a KILL 10 s later. One that the KILL does not end within 10 s more, for example a process stuck in the driver, is reported, and the lease or lock it holds stays held until it ends.
  • The watch checks $stop in two places. The loop's condition ends the loop at its next turn, so no new round starts after the signal; the round under way when the signal came may still scan with ps and print "nothing dumped". The check before a dump keeps that round from starting a dump.
  • The dump's output reaches stderr only once the dump has ended, so a KILL to the watch during a dump, with no TERM before it, leaves no stacks in the log.
  • qdot-rate.sh prints its rounds only after they have ended, not while they run. A signal that stops it partway leaves none of the finished rounds on screen or in the log, because its EXIT trap removes the sitting file.

What I ran into

This does not block the PR.

  • On an x86_64 machine with Ubuntu 26.04.1, timeout is uutils coreutils 0.8.0. When timeout itself receives a TERM from outside, GNU coreutils returns the child's code and uutils returns 143: timeout 900 bash -c 'trap "sleep 1; exit 7" TERM; sleep 30 & wait' & p=$!; sleep 1; kill -TERM $p; wait $p; echo $? prints 143 there and 7 with GNU coreutils 9.4 on a DGX Spark. run_bounded returns timeout's code, so on such a machine an ended runner reports 143 instead of the binary's code, and the two gpu-gate self-test cases that look for exit 7 expect GNU's behavior. Options: take the binary's code from somewhere other than timeout's status; or require GNU timeout for the gate runners and say so in the tools' requirements.
  • In just contrib-check --tier t1 on the DGX Spark, the q38-3090-server self-test failed once at this head: FAIL second-turn-restored: no 'call stream end .*kept=0 restored=2100 end_us=420000' in the happy run's output. Main 55acee0b in the same container printed q38-3090-server.sh: self-test ok. This PR does not touch tools/ref/q38-3090-server.sh, and its self-test runs a stub server under a stub gpu-gate.sh in a temp tree, so none of the changed runners is in that path. The same self-test ran 20 times at this head on a Mac with no failure (Q38SRV runs=20 red=0). It looks like a timing flake of that stub run under the t1 load; I did not look further.
  • Red at main too, on the same DGX Spark: gate-batch's two budget cases in check-recipes, the DS41 and Qwen3-MoE stub tests, and the gates counted below as "same as base". On the Mac, shellcheck is not installed, at main too.

Code shape

  • No second copy. run_bounded and lease_stop have one owner, tools/gate-bound.sh, which the gate runners already source and lease.sh and stack-watch.sh now source too; the four gate runners, lease_bounded and stack-watch.sh run their bounded children through run_bounded, and perf-decode.sh and load-groups.sh stop their background timeout with lease_stop. The timing runners' bounded children go through lease_bounded, lease_capture or lease_filter. The DS41 and Qwen3-MoE arms, the two preheats and cstate-ab.sh's arms no longer start a timeout of their own. stack-watch.sh keeps its own trap for its loop and its dump.
  • Model code holds only architecture facts. No model code is touched.
  • Dead code is gone. Nothing became unused.
  • No silent failure. lease_filter refuses by name a stream other than stdout or stderr and a call without a filter or a -- command, and checks its bound as lease_bounded does, all with 64 and before the filter starts. A temp dir or FIFO it cannot make returns 70, and so does gate.sh when it cannot make its FIFO. lease_stop reports a group that outlives its KILL and returns 70. dma-dram-share.sh now refuses a leading zero. A runner that is ended still exits with a status, now the binary's, a failed filter fails the arm, and a failed decoder fails perf-decode.sh with its code and the end of its log.
  • Comments state what is true now. tools/check-comments.sh passes at the head on the Mac (mac-static: check-comments rc 0) and on the DGX Spark.
  • No relaxed gate. gate()'s default bound is still 60 s. One window is wider: the stack-watch dump case allowed [0-7] s and now allows 1?[0-9] s, because its stub gdb now ends six seconds after its TERM. Without the signal passed on, the stub holds until the dump's TERM at 120 s, so the window still tells the two apart. The case carries a PIN(2026-10-15): comment with this reason. No other case changed its expectation.
  • cargo fmt and cargo clippy are clean on what I touched; the lint count did not go up. No Rust file is touched.

Duplication found or removed (path:line, at main 55acee0b): the same foreground timeout start was in tools/host-gate.sh:25, tools/gate.sh:74, tools/gate.sh:79, tools/gpu-gate.sh:1779, :1794 and :1799, tools/ref/time-gate.sh:38 and tools/ref/lease.sh:461. All of them now go through run_bounded. The DS41 and Qwen3-MoE arms started their own at tools/ref/depth-ds41.sh:1619, :1621, :1730 and :1732 and tools/ref/depth-qwen3moe.sh:2223, :2372 and :2378, the preheats at tools/ref/depth-ds41.sh:1588 and tools/ref/depth-glm5next.sh:820, and cstate-ab.sh at tools/ref/cstate-ab.sh:65. lease_bounded ran inside a substitution or a pipeline in ab-decode.sh (for example tools/ref/ab-decode.sh:58), decode-measure.sh, depth-decode.sh, depth-glm5next.sh, depth-gpu.sh, dma-dram-share.sh, governor-ab.sh, perf-decode.sh, qdot-rate.sh (tools/ref/qdot-rate.sh:49) and expert-union-dump.sh (tools/ref/expert-union-dump.sh:16). All of these now run in the runner's shell through lease_bounded, lease_capture or lease_filter. The background timeouts at tools/ref/perf-decode.sh:31 and tools/ref/load-groups.sh:152 had no stop of their own and now stop through lease_stop. Other runners under tools/ref/ still start some commands under a foreground timeout of their own (for example tools/ref/depth-ds41.sh:1355); they are not changed here.

What I ran

Everything here ran at dd64d463, whose base is main 55acee0b. The branch also merges with upstream/main 97ff99c8 without a conflict; nothing was run again for that.

On a Mac

  • ./tools/mac-static.sh 55acee0b…, with a local edit to tools/check-recipes.sh that starts its blocks three at a time: mac-static: ok.
  • Inside it, check-recipes ran the tool self-tests, among them card-tests: 733 tests, 0 failed, gpu-gate self-test: 106 of 106 ok, stack-watch self-test: ok and the lease self-test, with check-recipes: 57 tool self-tests ok.
  • The shell self-tests, each under Homebrew bash 5.3.20 and under /bin/bash 3.2.57, with LC_ALL=C:
Command bash 5.3.20 /bin/bash 3.2.57
bash tools/ref/stack-watch.sh --self-test stack-watch self-test: ok stack-watch self-test: ok
bash tools/ref/lease.sh --self-test self-test: ok (0 failures) self-test: ok (0 failures), with skip bounded-ds41-load: the runner's bash has no exec {fd}<>
bash tools/gpu-gate.sh --self-test gpu-gate self-test: 106 of 106 ok gpu-gate self-test: 106 of 106 ok
bash tools/ref/card-tests/run.sh card-tests: 733 tests, 0 failed card-tests: 733 tests, 0 failed
bash tools/ref/card-tests/cpu-guard.sh cpu-guard: 9 tests, 0 failed cpu-guard: 9 tests, 0 failed
bash tools/ref/load-groups.sh --self-test self-test: ok (0 failures) self-test: ok (0 failures)
bash tools/check-comments.sh check-comments: ok (905 files, 132954 comment lines; …) check-comments: ok (905 files, 132954 comment lines; …)

shellcheck on the changed timing scripts did not run: shellcheck: command not found, the same at main 55acee0b on that Mac.

On a DGX Spark

All runs are in the bloomery-dev CUDA 13.3.1 container (aarch64, GNU bash 5.2.21, GNU timeout 9.4, LC_ALL=C).

The shell self-tests, from the tree's root:

Command Result
bash tools/ref/stack-watch.sh --self-test stack-watch self-test: ok
bash tools/ref/lease.sh --self-test self-test: ok (0 failures)
bash tools/gpu-gate.sh --self-test gpu-gate self-test: 107 of 107 ok
bash tools/ref/card-tests/run.sh card-tests: 733 tests, 0 failed, with ok gate.sh: a FIFO it cannot make ends 70
bash tools/ref/card-tests/cpu-guard.sh cpu-guard: 9 tests, 0 failed
bash tools/ref/load-groups.sh --self-test self-test: ok (0 failures)
bash tools/ref/depth-glm5next-stub.sh depth-glm5next-stub: 49 checks, 0 failed
bash tools/check-comments.sh check-comments: ok (905 files, 132954 comment lines; …)
bash tools/ref/depth-ds41-stub.sh depth-ds41-stub: 84 checks, 7 failed, the same line and failures as main 55acee0b in the same container, so not from this change
bash tools/ref/depth-qwen3moe-stub.sh depth-qwen3moe-stub: 86 checks, 59 failed, the same line and failures as main 55acee0b in the same container, so not from this change

The two stub tests' failure lines name random temp dirs, which differ between the two runs; with those names made equal, the head's failures are the main's. In this container the DS41 stub's runs print systemctl: command not found. The cause of the Qwen3-MoE stub's failures was not looked into.

Two repeats at the head: the lease self-test 20 times, lease-selftest-repeat: runs=20 red=0, and the card tests' four cases that signal right after tee starts, 50 times, TEE-HEAD runs=50 red=0.

just contrib-check --tier t1, which runs the t0 units and the host-nightly units: DONE tier=t1 total=104 red=1 wall=415s red: check-recipes. Main 55acee0b on the same machine prints DONE tier=t1 total=104 red=1 wall=413s red: check-recipes. Both fail gate-batch's FAIL budget: --over-budget-ok passes it: rc 70, want 0 and FAIL budget mutant: without the check the over-budget item runs: rc=70 …. The head also failed the q38-3090-server self-test once; see "What I ran into". t0 was not run on its own at this head, and t1 was not run on an x86_64 host.

The gates: tools/affected-gates.sh 55acee0b --no-box selects 166 of 166 gate-recipes. Each ran as BLOOMERY_BOX_LOCAL=1 just <recipe>. A gate that failed ran again on main 55acee0b on the same machine, and the two failure lists were compared: gates-affected: 175 gates, ok 32, same as base 143, differs 0.

  • The 175 are the 166 recipes and nine fixture variants. The variants were not run: the gate run passed them to just as <recipe>@BLOOMERY_FIXTURE_VARIANT=<variant>, a form just rejects with error: invalid override path. They are gate-gpu-glm5next-e2e with iq4xs, q2kxl and glmffnkq, gate-gpu-glm5next-mtp with iq4xs and glmffnkq, gate-gpu-glm5next-stagger with glmffnkq, gate-gpu-glm5next-nvtier with q6kdown, and gate-gpu-qwen35moe-e2e with unjoined and unjoinedq6.
  • The other gates counted as "same as base" fail at the head and at main with the same failure lines. They are failures on this machine, not passes.
  • The 32 that passed: gate-gpu-kquant, gate-gpu-cand, gate-gpu-hc-gated, gate-gpu-mamba2, gate-gpu-glm-router, gate-gpu-q6k-ids, gate-gpu-swap, gate-gpu-xstream, gate-gpu-qwen3moe-head, gate-gpu-linear, gate-gpu-gemma-embedding2-head512, gate-gpu-glm-mla, gate-gpu-qwen3moe-types, gate-r8, gate-threads, gate-levers, gate-sampler, gate-runtime, gate-models, gate-app, gate-serve, gate-jinja, gate-gpu-lib, gate-gpu-gates-lib, gate-hf, gate-gpu-ds41-lib, gate-ds41-bind, gate-q38-serve-rules, gate-ds41-place, gate-gpu-ple, gate-gpu-ds41-unpack and gate-gpu-vision-lib.

stack-watch's self-test under a child subreaper that never reaps orphans, three runs on CPUs 0 to 3: stack-watch self-test: ok each time, with the dump case dumping: yes, ended in 6 s, left: none, ended in 7 s and ended in 6 s.

Fail first

Each mutant was planted in a copy of the head's tree, on the same DGX Spark and in the same container, except the last two, which ran on the Mac. The check ran on the mutant, then on the restored copy, and the restored source was compared with the head: mutants: total=20 killed=19 restored=20 [derived from the 20 runs]; the one not caught is the readiness mutant below. Four use a probe outside this PR that sends the TERM inside a delay the mutant plants.

Mutant Check With the mutant Restored
run_bounded starts the child before it sets its trap lease self-test left: stub timeout, lease: held in bounded-startup-term ok
lease_capture takes the output with $(lease_bounded … 2>&1) lease self-test self-test: FAIL (12 failures), among them the preheat and cstate-ab.sh arms ok
lease_filter sets no trap of its own lease self-test reader=alive stderr=open in bounded-filter-open-term-none-reader ok
lease_filter's trap does not end and reap the filter lease self-test reader=alive stderr=open in bounded-filter-open-term-none-reader ok
lease_stop sends one TERM to timeout only lease self-test left: stub, lease: held in bounded-fork-term, left: decoder record, lease: held in bounded-perf-fork-term ok
lease_stop reads the ps rows without its own IFS lease self-test left: stub, lease: held in bounded-ds41-load-fork-term ok
lg_process sets no signal trap lease self-test left: stub timeout, lease: held in bounded-ds41-load-term ok
perf-decode.sh as it was before the decoder fix lease self-test left: decoder decoder-timeout, lease: held in bounded-perf-record-term ok
perf-decode.sh's failed decoder exits with its code alone lease self-test no line matches /^decode exited 23: / in bounded-perf-fail-reason ok
the readiness wait goes back to the stub's pid file alone, with timeout's fork delayed 0.5 s lease self-test not caught (see below) ok
the watch runs its command without run_bounded stack-watch self-test left: child, lock after: held in the command case ok
the watch runs its dump without run_bounded stack-watch self-test left: child, lock after: held in the dump case ok
the watch ignores signals while a dump starts probe, TERM before the dump watch still waiting 26 s after the TERM, gdb started: yes, left: gdb rc 7, ended in 6 s, gdb started: no, left: none
the TERM to an ended dump fails under bash -e probe, watch under bash -e rc 1, ended in 3 s, gdb started: yes, left: command rc 7, ended in 6 s, gdb started: yes, left: none
gate.sh sets no trap before tee probe, TERM before run_bounded runner gone: yes, output ended: no runner gone: yes, output ended: yes
gate.sh does not act on a signal from before tee's pid probe, TERM before the pid runner gone: no, output ended: no runner gone: yes, output ended: yes
gate.sh sets no trap before tee and acts on no held signal the four tee start cases tee ended without a FIFO writer: tee pid missing or still alive ok
gate.sh's EXIT trap sends tee one TERM and does not reap it the four tee start cases, 20 times runs 20 red 9 runs 20 red 0
perf-decode.sh's awk pipe without the parentheses (on the Mac) lease self-test bounded-perf-annotate-term-rc: got [2], want [143] ok
the lease self-test names depth-qwen3moe.sh by its literal path (on the Mac) just check-recipes self-test FAIL: smoke (a batch in a batch) or check-recipes has the wrong never-skip ok

The readiness mutant is not caught at this head because the defect it was made to show is fixed. On an earlier head it left the stub holding the lease, when a TERM came while GNU timeout was still in fork; at this head lease_stop sends the TERM to timeout's group, and every case ends with the lease free even with the delayed fork (race-sanity rc 0 seconds 0.511 clone delayed True). The lease_stop mutant above now guards that defect.

Two mutants ran a second time after a fix to the harness, with their first runs kept: the probe for gate.sh's trap before tee looked for the old one-line EXIT trap and never planted the mutant, and the first run of the four tee start cases ran inside the full card tests, where an unrelated depth-serve case failed in the copied tree on the restored run too.

Not run at this head: a mutant for the gate.sh case where the FIFO cannot be made, and one for the watch's window before the command's pid is read.

Hardware (if the change is for a machine)

Not a machine-specific change. The runs used a Mac and one DGX Spark (NVIDIA GB10, driver 595.84).

AI help

AI 코딩 에이전트가 코드를 작성한 후 설명을 썼고, 테스트를 마쳤습니다. diff 리뷰와 PR 초안 리뷰는 제가 직접 했습니다.

@github-actions github-actions Bot added the tools Tools, recipes, CI and the toolchain: tools/, justfile, .github/, Cargo label Oct 14, 2026

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Verdict: MUST_FIX: Cancellation can leave stack-watch children running.

Must fix

1. Wait again after a signal interrupts child cleanup

tools/ref/stack-watch.sh:58: TERM during the dump interrupts Bash's wait. The new trap forwards TERM, but the wait returns before delayed debugger cleanup finishes. Line 59 clears the dump PID, so the watcher can finish after the watched command exits while the dump's timeout and debugger remain alive.

tools/ref/stack-watch.sh:110: the quiet-command branch breaks out of the loop before waiting for the command. If TERM arrives during that final wait, the new trap also interrupts it: the watcher returns 143 while the command is still shutting down. The trap's signal ignore prevents later interruptions, but does not resume the interrupted wait.

These children inherit the gate's lock descriptors and output writers. Fix both waits in this cancellation change so a completed runner does not leave them held.

Replace the dump wait and PID reset with:

    wait "$dump"
    wait "$dump"
    dump=''

Replace the final command wait block with:

  rc=0
  wait "$pid" || true
  wait "$pid" || rc=$?

Extend the existing self-test with delayed debugger shutdown and TERM during the final wait. Its current debugger stub uses exec sleep 10, which exits immediately on TERM.

Checked

Evidence: read both waits, the new trap, the quiet-command branch, the cancellation self-test and the GPU caller's lock handling.
Tests: focused Bash probes using the dump function and final-wait block returned with a delayed child still alive. Both replacements waited for shutdown; the final wait preserved exit 7 instead of returning 143.
Gates: stack-watch's existing test expectations are unchanged.
Inline: line 58 is added by the diff; the replacement is valid Bash.
Escape catalog: C1–C4 do not match this finding. No ledger lessons are in force.

Notes

Full self-test suites were not rerun.

🔨 Shingler, bloomery's review bot: approves a clean pull request and requests changes on a must-fix (how it works)

Comment thread tools/ref/stack-watch.sh Outdated
for t in /proc/"$pid"/task/*; do
timeout 120 gdb -p "$target" -batch -ex 'thread apply all bt' > >(sed 's/^/stack-watch: /' >&2) 2>&1 &
dump=$!
wait "$dump"

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

TERM interrupts this wait before delayed debugger cleanup finishes. Wait again before clearing the dump PID so its timeout and debugger cannot outlive the watcher.

Suggested change
wait "$dump"
wait "$dump"
wait "$dump"

@juliankang4
juliankang4 force-pushed the dgx-spark-gate-timeout-group branch from 8d11239 to ddc585c Compare October 14, 2026 18:33
@juliankang4

Copy link
Copy Markdown
Collaborator Author

Fixed in ddc585ca, as suggested, and the branch is rebased onto main at 89d5ce01. Both waits in stack-watch.sh are now made twice, the way run_bounded already does: the trap ignores later signals, so only the first wait can be cut short, and the second one waits for the dump or the command to end and reads its status. The first wait of each pair is written wait ... || true, so a watch run under bash -e still reaches the second. $dump is cleared only after the second wait.

The same path had one more gap, fixed in the same commit. A signal that came outside a wait (during the loop's sleep) left the loop running, and when the command took longer than the quiet time to end after its TERM, the watch started a dump that no signal would reach. The trap now sets $stop: the loop ends on it and starts no dump, and a dump started just as the signal came gets a TERM right after it starts.

After a signal, the watch waits for the dump and the command only as long as their own bounds allow (the dump's 120 s and the command's own timeout); a child that does not end on its TERM still holds it until that bound or a KILL.

The self-test has the two cases the review asked for, and a third:

  • Delayed debugger shutdown: the stub gdb now holds in a loop and exits six seconds after its TERM, instead of exec sleep 10. The case's window is now 19 s instead of 7 s for that reason, with a dated PIN comment; a stub that never gets the TERM holds until the dump's 120 s bound.
  • TERM during the last wait: the command keeps running after the watch has ended its quiet sleep, and the case sends a TERM while the watch waits for it. The command ends with 7 two seconds after its TERM.
  • TERM before the quiet time: the command takes five seconds to end after its TERM, longer than the quiet time, and no dump may start after the TERM.

Results:

  • Against the single waits of 8d11239e, all three cases fail on the Mac under bash 3.2.57 and 5.3.20 and on a GB10 machine with GNU timeout 9.4: dumping: yes, ended in 2 s, left: gdb, rc 143, want 7 ... left: command and dumped: yes. The first two cases also failed the same way on x86_64 Linux with bash 5.3.9 and the uutils timeout 0.8.0.
  • With the waits made twice but no $stop, only the third case fails (Mac under both bash versions, GB10).
  • At ddc585ca, stack-watch self-test: ok on the Mac under both bash versions and on the GB10 machine. There gpu-gate.sh --self-test also gives 102 of 102 ok; its part 1 includes the two cases where gate()'s bound ends a held run, plain and under the watch.
  • Mutants (Mac bash 5 and GB10): a trap that no longer ignores later signals, or a single dump wait, fails the delayed-gdb case; a single last wait fails the last-wait case; removing both $stop checks fails the third case. Removing only one of the two checks does not fail any case.
  • By hand, under bash -e, the unguarded first waits ended the watch at once with 143 and left the gdb and the command; at ddc585ca both cases return 7 with nothing left. A TERM placed between starting a dump and setting $dump left the gdb running past 20 s without $stop; at ddc585ca the gdb gets its TERM and the watch returns 7 after 8 s.
  • tools/check-comments.sh is clean, and shellcheck -x -S warning reports the same single finding as before (SC2034 for an unused out in the self-test, present on main).

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Verdict: MUST_FIX: both supervisors can exit between child launch and trap installation, leaving the bounded command alive.

Must fix

1. Install forwarding traps before launching either child

tools/gate-bound.sh:40 and tools/ref/stack-watch.sh:83: TERM after the background launch and before the trap kills the runner alone. Its timeout and binary retain inherited output descriptors and gate or lease locks until their own bound.
Focused launch-sequence probes delivered TERM before PID assignment in both supervisors: the runner terminated by TERM and its timeout stayed alive. The replacements below awaited the child before returning.
Cost: close this window while changing the signal owner; otherwise cancellation still leaves later runs waiting.

Replace run_bounded with:

run_bounded() {
  local traps s rc=0 BIN_PID=''
  traps=$(trap -p TERM INT HUP)
  BIN_SIGNAL=
  for s in TERM INT HUP; do
    # shellcheck disable=SC2064 # bind the signal now, read the pid in the trap
    trap "BIN_SIGNAL=$s
if [ -n \"\$BIN_PID\" ]; then
  trap '' TERM INT HUP
  kill -TERM \"\$BIN_PID\" 2> /dev/null
fi" "$s"
  done
  "$@" <&0 &
  BIN_PID=$!
  if [ -n "$BIN_SIGNAL" ]; then
    trap '' TERM INT HUP
    kill -TERM "$BIN_PID" 2> /dev/null
  fi
  wait "$BIN_PID" || true
  wait "$BIN_PID" || rc=$?
  trap - TERM INT HUP
  eval "$traps"
  return "$rc"
}

In watch, replace the child launch and current trap at lines 77–83 with:

  pid=
  trap 'stop=1; if [ -n "$pid" ]; then
    trap "" TERM INT HUP
    kill -TERM "$pid" ${dump:+"$dump"} 2> /dev/null
  fi' TERM INT HUP
  "$@" > "$log" 2>&1 &
  pid=$!
  if [ "$stop" = 1 ]; then
    trap '' TERM INT HUP
    kill -TERM "$pid" 2> /dev/null
  fi

Before the PID exists, these traps record the signal without ignoring it. After assignment, they forward the pending signal. Add a regression case that sends TERM at that boundary and asserts that the child is gone and the lock is free.

Checked

Duplicates: one finding covers the launch window in both supervisors. The watch also owns dump cancellation.
Complexity: no Rust functions changed; shell launch, trap and wait transitions reviewed.
Silent failure: the second waits retain command status; lease_bounded replays BIN_SIGNAL after restoring the caller's traps.
Tests: focused validator probes reproduced both launch-window failures and checked the replacements. The run_bounded replacement preserved child codes 0, 7 and 101. The added self-tests send TERM after child startup, so they miss this boundary.
Gates: the dump-case time window has a dated PIN and reason; no engine gates moved.
Escape catalog: no C1–C4 pattern matched.

Notes

Full self-tests were not rerun by the validator.

🔨 Shingler, bloomery's review bot: approves a clean pull request and requests changes on a must-fix (how it works)

Filed outside this PR: #212

@juliankang4
juliankang4 force-pushed the dgx-spark-gate-timeout-group branch from ddc585c to 572ee7b Compare October 14, 2026 21:43
@juliankang4

Copy link
Copy Markdown
Collaborator Author

Fixed as suggested in b7c8cb86. The branch is now on main at 33a6b281, and the head is 572ee7b2. run_bounded and the watch set their trap before they start the child. Before the child's pid is known, the trap only records the signal (BIN_SIGNAL in run_bounded, $stop in the watch) and ignores nothing, so the child does not start with the signals ignored. Once the pid is read, a recorded signal goes on to the child as a TERM. BIN_PID is a local that starts empty, and so does the watch's pid. stub-timeout.py, the stand-in timeout on the Mac, sets its TERM handler before it starts its command, as GNU timeout does.

About the regression case: the window lies between two shell commands, the & that starts the child and the read of $!. A test outside the runner cannot place a TERM there, because by the time the test sees the child's pid, the runner has read it too. So the new cases (the lease self-test's two bounded-term cases and the stack-watch case "a TERM right after the command has started") send the TERM right after the child has started and check that the child is gone and the lease is free, and they pass with the old order too. Only a mutant that plants a 2 s delay in the window reaches it. At 4a282da2, an unpushed commit that holds b7c8cb86's trap code and comes before the fixes below (on the Mac under bash 5), the old order with the delay between the start and the trap failed both lease cases (left: stub timeout, lease: held) and the stack-watch case (rc 143, want 7; last line: command ran: yes, left: command). The new order with the delay between the start and the pid read passed all three (ok bounded-term, ok bounded-term-trap). At the head, on a DGX Spark, the same two mutants give the same split: the old order with the delay turns the two lease cases and the stack-watch case red, and the new order with the delay keeps them green. The table under "Fail first" in the PR body has every mutant.

A second look at the same paths found more of the same kind, fixed here:

  • 975066c2: gate.sh with a test filter starts tee on its FIFO before it calls run_bounded. A TERM in between ended the runner, and tee, still waiting to open the FIFO, held the runner's output open. gate.sh now sets its own TERM, INT and HUP trap before tee starts, and its EXIT trap ends tee.
  • 50e19cbe: the watch's trap no longer ignores signals from its last $stop check before a dump until the dump's pid is known. A dump started with TERM ignored could lose the TERM the watch sends it right after the start, and its gdb then ran on to the dump's bound. BIN_SENT and $told record that the trap has passed a signal on, so the check after the pid read does not send it again. Each TERM to a child that may have ended is written || :, so under set -e it does not end the caller before its last waits. Both clear the child's pid once it is reaped. The dump writes to a file that the watch passes to stderr after the dump, instead of > >(sed ...), whose sed no wait covered, and the dump's timeout gets --kill-after=10.
  • 572ee7b2: in the self-test, the stub gdb sets its TERM trap before it writes the pid the case waits for. The command of the case with a TERM before the quiet time prints every 0.2 s until its TERM, so the case no longer depends on when the TERM comes. The case once named "a TERM as the command starts" is now named for what it does.
  • 47c70929: after a dump the watch counts a zombie as ended, and a failed case prints the last line it read on its FAIL line.

The t0 failure on ddc585ca is described under "What I ran into" in the PR body; its cause is not confirmed.

Results at the head are under "What I ran" in the PR body: the self-tests pass on the Mac (bash 5 and bash 3.2) and on a DGX Spark, and of the 167 affected gate runs on a DGX Spark, 30 pass and the other 137 fail the same way on main.

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Verdict: MUST_FIX: TERM to a timing runner's PID still leaves its captured command and the machine lease alive.

Must fix

1. Close the cancellation defect in capture callers (kind A)

tools/ref/lease.sh:469 runs in a command-substitution subshell at tools/ref/ab-decode.sh:58 and tools/ref/depth-gpu.sh:132. TERM to the runner PID ends that shell without reaching the subshell's forwarding trap. The command and timeout retain descriptor 9, blocking subsequent timing runs until the arm bound (900 seconds by default). An independent reproduction confirmed both processes and the lease survive the runner.

Fixing the capture callers now avoids a second cancellation fix and another round of lease tests. The new termination tests call lease_bounded directly and miss this path.

Call lease_bounded in the runner shell and capture its output in a file. Apply this to substitution and pipeline callers. Replace ab-decode.sh:58 with:

arm_log=$(mktemp) || exit 70
trap 'rm -f "$arm_log"' EXIT
lease_bounded "$LEASE_ARM_BOUND" env ${e_args[@]+"${e_args[@]}"} \
  "$(decode_bin "$d")" -m "$MODEL" --tokens "$TOKENS" -n "$N" \
  >"$arm_log" 2>&1
rc=$?
out=$(cat "$arm_log") || exit 74
rm -f "$arm_log"
trap - EXIT
if [ "$rc" -ne 0 ]; then
  echo "r$r $label FAILED" >&2
  printf '%s\n' "$out" | tail -n 5 >&2
  exit 1
fi

Preserve existing cleanup traps in other runners. Extend the lease termination test to capture output and send TERM to the runner PID; assert that the command and timeout end and the lease is free.

Checked

Evidence: read both capture callers, descriptor 9's acquisition, and the changed signal handler.
Verification: with the changed functions, a PID-only TERM left the captured command, timeout and lock alive; the direct-call control ended both children and freed the lock. The proposed replacement passes bash -n.
Test coverage: the new termination cases use direct calls; the captured stdin case sends no signal.
Gates: the stack-watch dump timing window has a dated PIN and a reason. Escape catalog C1–C4 does not match this finding. No ledger lessons are in force.

Notes

The PR reports contrib-check t1 failures with uutils timeout; run_bounded returns the timeout child's status and documents GNU timeout behavior.

🔨 Shingler, bloomery's review bot: approves a clean pull request and requests changes on a must-fix (how it works)

Filed outside this PR: #221

@juliankang4

Copy link
Copy Markdown
Collaborator Author

Fixed in bbfa52b, the head now on the branch:

  1. No timing runner calls lease_bounded inside a command substitution or a pipeline any more. lease_capture in tools/ref/lease.sh runs it in the runner's own shell with stdout and stderr in a temp file and then sets LEASE_OUT. ab-decode.sh:58 and depth-gpu.sh:134 call it, and so do the other capture callers: decode-measure, depth-decode, depth-qwen3moe, depth-glm5next, governor-ab, cstate-ab and the DS41 and GLM-5.3-Flash preheats. A runner that filters the output live uses lease_filter, which reads the command through a FIFO with descriptor 9 closed in the filter.
  2. The lease self-test's TERM cases now include a capture form beside direct, plus the depth, preheat and C-state arms. Each case sends TERM to the runner's pid and checks that the stub and its timeout end, the next arm never starts and the lease is free.

The PR body has the results at this head.

AI help

AI 코딩 에이전트가 코드를 작성한 후 설명을 썼고, 테스트를 마쳤습니다. diff 리뷰와 PR 초안 리뷰는 제가 직접 했습니다.

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Verdict: MUST_FIX: Cancelling the profiling runner still leaves its decoder holding the timing lease.

Must fix

1. Stop the profiling runner's decoder on cancellation

tools/ref/perf-decode.sh:31 starts the decoder under a separate background timeout. TERM during perf record stops the profiling child, then tools/ref/lease.sh:488 signals the runner again and terminates it. The decoder and its timeout survive with lease descriptor 9 until their bound (900 seconds by default). A focused cancellation probe confirmed both survivors and the held lease. This is a kind A behavior defect.

This is a remaining sibling of the cancellation defect this PR fixes. Closing it now prevents the profiling runner from retaining the same lease leak.

Replace the decoder launch and bound_pid=$! with:

bound_pid='' decode_exit=''
trap 'trap "" TERM INT HUP; [ -z "$bound_pid" ] || { kill -TERM "$bound_pid" 2>/dev/null || :; wait "$bound_pid" 2>/dev/null || :; }' EXIT
for s in TERM:143 INT:130 HUP:129; do
  trap "decode_exit=${s#*:}; [ -z \"\$bound_pid\" ] || exit ${s#*:}" "${s%:*}"
done
timeout --kill-after=10 "$LEASE_ARM_BOUND" "$BIN" -m "$MODEL" --tokens "$TOKENS" -n "$N" > "$OUT/decode.log" 2>&1 &
bound_pid=$!
[ -z "$decode_exit" ] || exit "$decode_exit"

Replace the later wait "$bound_pid" with:

decode_rc=0
wait "$bound_pid" || decode_rc=$?
bound_pid=''
[ "$decode_rc" -eq 0 ] || exit "$decode_rc"

The cleanup passed the focused probe: exit 143, both children gone, lease free. Add a regression that cancels during profiling and checks both children and the lease.

Checked

Scope: the decoder launch is unchanged, but the fix-PR sibling-path exception applies to this cancellation defect.
Duplicates and complexity: no finding retained in either category.
Silent failure: traced cancellation through run_bounded, lease_bounded and inherited lease descriptor 9.
Tests: the focused probe reproduced the failure and verified the proposed cleanup with executable stubs.
Gates: no gate change requested by this finding.
Escape catalog: C1–C4 do not describe this finding.

Notes

Full shell self-tests and GPU gates were not rerun during validation.

🔨 Shingler, bloomery's review bot: approves a clean pull request and requests changes on a must-fix (how it works)

GNU timeout runs its command in a process group of its own, and the gate runners ran their binary
under timeout in the foreground. A TERM, INT or HUP sent to a runner's group never reached the
binary's timeout, and a TERM sent to the runner's pid ended the runner alone. Either way the binary
ran on until its own bound and kept what it held: gpu-gate.sh's gate locks and the run's output,
time-gate.sh's machine-wide lease, host-gate.sh's and gate.sh's output. The gpu-gate self-test met
it whenever gate()'s 60 s bound ended a held run: a later blocking flock and relay() waited on what
was left.

run_bounded, in gate-bound.sh beside the bound's parser, starts the bounded command as a child and
waits for it. A TERM or HUP (and an INT where the runner hears one) goes on to the child as a TERM;
the trap then ignores further signals, so the runner returns after the child, with its code.
gpu-gate.sh's three start paths, host-gate.sh, gate.sh and time-gate.sh all run through it. gate.sh
with a test filter feeds tee through a FIFO, since a pipeline would run run_bounded in a subshell a
TERM to the runner does not reach. lease.sh's lease_bounded, which bounds the children of the timing
runners under the lease, runs through it as well and then stops the runner on the signal it passed
on, by the runner's own trap where it set one; run_bounded gives the caller its traps back.

stack-watch.sh passes a TERM or HUP on to its command and to a gdb dump under way; the dump runs in
the background and is waited for, so a TERM during it is heard at once. The Mac's stand-in timeout
keeps coreutils' timing and codes: TERM at its bound or on a TERM it receives, KILL --kill-after
seconds after the first (none at 0), 124 when the bound ran out and 137 after the KILL.

The gpu-gate self-test ends a held run by gate()'s own bound (GB=<s>, 10 here), plain and under the
watch: right after the bound returns 124 the stub and its timeout are gone and the card lock is
free, then the output ends, and the runner's code is the stub's 7 (STUB_TERM). The stack-watch
self-test sends two TERMs during a dump by a stub gdb that holds; card-tests run gate.sh's filter
path. The lease self-test sends a TERM to a runner that holds a temp lease and runs a stub under
lease_bounded, with and without a TERM trap of its own: the stub and its timeout are gone, the lease
is free and the runner's next arm never starts.
…and starts no dump after it (review)

The watch's trap passes a TERM, INT or HUP on and then ignores further signals, but the signal itself
cuts short the wait it arrives in. During a dump, the watch went on while the debugger and its
timeout were still shutting down, and cleared $dump; in the last wait for the command, it returned
143 while the command was still ending. Both kept the gate's lock descriptors and output writers
open after the runner had gone on. Each wait is now made twice, as run_bounded does: the trap
ignores later signals, so the second wait is not cut short and reads the child's status. The first
waits take || true, so a watch run under errexit still reaches the second.

A signal that came outside a wait, during the loop's sleep, left the loop running: when the command
took longer than QUIET to end after its TERM, the watch dumped its quiet process with a gdb no signal
would reach, and waited for it up to the dump's 120 s bound. The trap now sets $stop; the loop ends
on it and starts no dump, and a dump started as the signal came gets a TERM as well.

The self-test's stub gdb now holds in a loop and ends six seconds after its TERM, so the dump case
allows 19 s (PIN). A second new case sends a TERM while the watch waits for a command that goes on
after its quiet process was ended, and a third sends a TERM before the quiet time to a command that
takes five seconds to end. With single waits the first two fail (the gdb left; 143 with the command
left); with the waits made twice but no $stop the third fails (a dump after the TERM). Now all three
end with the command's code 7, nothing left and no dump after the TERM.
…arts (review)

Both started the child with & and set the forwarding trap after reading its pid. A TERM that came
in between ended the runner by the default action, and the child (timeout and the binary under it)
ran on with the runner's lock descriptors and output writers until its own bound.

The trap is now set first. Before the pid is known it only records the signal (BIN_SIGNAL in
run_bounded, $stop in the watch) and ignores nothing, so the child does not start with the signals
ignored; once the pid is read, a recorded signal is passed on as a TERM, and from then on the trap
passes on and ignores later ones, as before. run_bounded's BIN_PID is a local set empty before the
trap, so a pid from an earlier call is never signalled, and the watch's pid starts empty, which its
trap reads under set -u.

stub-timeout.py, the stand-in timeout on hosts without one, sets its TERM handler before it starts
COMMAND, so a TERM forwarded right after the launch reaches COMMAND instead of ending the stand-in
alone. A second TERM sends COMMAND another TERM and moves no deadline, as GNU timeout does.

The lease_bounded self-test's stub now sets its trap before it writes its pid, and the test sends
the TERM as soon as the pid is there (polled every 0.1 s). stack-watch's self-test gains a case that
sends the TERM right after the command has started: the command sets its trap, writes its pid and
holds, and the watch returns with its code 7 and nothing left. Neither case reaches the window
between the start and the trap: it lies between two shell commands, and the runner has read the pid
by the time the test sees it, so both cases pass with the old order too. Only a delay planted in
that window tells the two orders apart.
…ase prints its last line

After the dump the watch TERMs the quiet process and gives it ten seconds before a KILL, polling
with kill -0. kill -0 succeeds on a zombie, so a process that had ended but was not yet reaped held
the watch for the full ten seconds and then took a KILL it could not feel. That happens when its
parent does not reap it, and when an orphaned one is reparented to a subreaper that does not reap.
The poll and the KILL now read the state from ps -o stat= (BSD and procps alike): gone, or Z, is
ended.

A failed self-test case now carries the last line of what it read on its FAIL line (the observed
summary, such as "dumping: yes, ended in N s, left: none"), so a log that keeps only FAIL lines
still shows why. No case's verdict changes.
…run_bounded's trap (review)

With a test filter gate.sh starts tee on the FIFO first and only then calls run_bounded, which sets
its trap. A TERM to the runner's pid in between ended the runner by the default action, and tee,
still waiting to open the FIFO, held the runner's output open, so a reader of that output never saw
its end.

gate.sh now sets a TERM, INT and HUP trap before tee starts: once tee's pid is known the trap exits
(143, 130, 129) and the EXIT trap ends tee and removes the log and the FIFO; a signal that comes
before tee's pid is known is held and acted on as soon as it is. run_bounded puts this trap back
when it returns. tee's pid is cleared once it is reaped, so the EXIT trap never signals a pid that
was reused.
…mp with it ignored, and go on when a TERM finds its child ended (review)

Once it has passed a signal on, the watch's trap ignores further ones. A signal that came after the
check before a dump and before the dump started set that ignore, and the dump (timeout and gdb)
started with TERM ignored: the TERM the watch then sent it could land before timeout set its own
handler and be lost, and the gdb ran on to the dump's bound. The trap now ignores nothing while a
child may still start ($spawning: from that check until the dump's pid is known, as before the
command's pid is known); a signal in that window is passed on to the command, and the dump gets its
TERM as soon as its pid is.

A signal that came after a child's pid was read and before the check of a held signal was passed on
twice, by the trap and by the check. The trap now records that it has passed it on (BIN_SENT in
run_bounded, $told in the watch, the pids it sent to), and the check sends only when it has not.

A cleanup TERM to a child that had already ended (bash reaps a background child as it ends) failed,
and under set -e that ended the caller before its last waits. Each such kill now ends `|| :`, and
the KILL after a dump is an if. run_bounded clears its pid and the watch its $pid once the child is
reaped, so a signal after that reaches no other process.

The dump's output went through `> >(sed ...)`, a child of its own that the two waits for the dump do
not wait for, and that could outlive the watch holding its output. The dump now writes to a file
beside the watch's log, and the watch passes the file to stderr, prefixed, once the dump has ended.
The dump's timeout gets --kill-after=10, so a gdb that ignores the TERM at 120 s is killed ten
seconds later instead of holding the watch.
…set, and its TERM-before-quiet case prints until the TERM (review)

The stub gdb wrote its pid, which the dump case waits for, before it set its TERM trap; a TERM sent
in between ended it at once, without the delayed exit the case is there to test. It now sets the
trap first, as the commands of the other cases do.

The case with a TERM before the quiet time slept 0.5 s after the command's pid and relied on the
TERM coming before the 2 s quiet time ran out; on a slow host the watch could dump first. Its
command now prints every 0.2 s until the TERM and stops printing then, so only the TERM, whenever
it comes, lets the quiet time run out.

The case named "a TERM as the command starts" sends its TERM once the command has written its pid,
when the watch has read the pid already; it is renamed "a TERM right after the command has
started", in the self-test and in check-recipes.sh's summary.
Run the DS41 and GLM5Next preheats and the C-state A/B arms through lease_capture. Keep their output and return-code handling while cancellation ends the child before the runner releases its lease.

Exercise both preheaters and the C-state arm loop under a TERM to the runner, with and without its own TERM trap.
A held command can publish its PID before GNU timeout returns from fork.
A TERM in that interval ends timeout without signalling the command, so
the runner is reaped while the stub still holds the lease.

Wait for the timeout parent to be sleeping with the held command alive
before sending TERM, and check readiness in each runner form. The bound,
return codes, caller traps and assertions on surviving processes and
lease release stay the same, including the runner's own startup gap.
… lease

Own the background decoder's timeout with an EXIT trap and defer signals
until its PID is known. Reap and clear it on normal completion, retaining
the decoder's status and the later profiling commands' signal handling.

The lease self-test cancels perf record once both timeout monitors are
waiting and checks that both command trees end and the lease is free.
Send TERM to timeout and its process group through lease_stop, wait for
live members and reap the monitor. Escalate to KILL after ten seconds so
a command left in timeout's fork window cannot keep the lease.

Load groups defer signals until their monitor is known. Memory scopes
ignore repeated signals and wait again for the command's status.
perf-decode reports decoder failures with the last twenty log lines.

Exercise load groups, the orphaned timeout group, the real memory scope
and perf cancellation before spawn and during annotate and callers.
Share lease_stop through gate-bound.sh without loading timing runners from gates. Parse process rows with an explicit IFS so cancelling a FIFO read waits for orphaned timeout children.
Run the watched command and stack dump through run_bounded so cancellation waits for the monitor group and releases inherited locks.

Cover fork-exit monitors in the watcher self-test and signals around tee PID publication in the card cases. Describe process-table failure cleanup and print perf stderr when readiness fails.
The FIFO reader can still be a shell setting up tee when cancellation arrives. A catchable TERM without a wait does not guarantee that reader has ended. Kill the remaining reader and reap it before cleanup returns, while ignoring further cancellation signals.
@juliankang4
juliankang4 force-pushed the dgx-spark-gate-timeout-group branch from bbfa52b to dd64d46 Compare October 15, 2026 20:01
@juliankang4

Copy link
Copy Markdown
Collaborator Author

Fixed in dd64d46, the head now on the branch:

  1. tools/ref/perf-decode.sh now owns its decoder. The EXIT trap stops the decoder's timeout and its process group with lease_stop, and a TERM, INT or HUP that comes before the decoder's pid is known is held and acted on as soon as it is (exit 143, 130 or 129). After a normal run the decoder is reaped and its pid cleared, so the later profiling commands keep their usual signal handling. A decoder that fails now fails the run with its code and the last 20 lines of its log. This follows the proposed cleanup, with lease_stop in place of the single TERM.
  2. lease_stop lives in tools/gate-bound.sh beside run_bounded. It sends TERM to timeout and to its group, waits for the group's live members, sends KILL after 10 s and reaps timeout. A single TERM to timeout is not enough: GNU timeout that gets the TERM before it has returned from fork exits without signalling its command, and the command keeps descriptor 9 in that group. run_bounded and the load groups in load-groups.sh stop through it too, and stack-watch.sh now runs its command and its gdb dump through run_bounded.
  3. The lease self-test runs a copy of perf-decode.sh with a stub decoder and a stub perf and cancels it during perf record, during perf record under a timeout that exits without passing the TERM on, before the decoder's pid is assigned, during annotate and during the callers report. Each run must end with 143, with both command trees and their timeouts gone and the lease free. Two more runs check that a decoder ending with 23 or 124 fails the run with that code and its last 20 log lines. With perf-decode.sh as it was at bbfa52b, the record case fails with left: decoder decoder-timeout, lease: held; with lease_stop cut back to one TERM, the fork case fails with left: decoder record, lease: held. Both pass once restored.

The review listed no other items. The PR body has the results at this head.

AI help

AI 코딩 에이전트가 코드를 작성한 후 설명을 썼고, 테스트를 마쳤습니다. diff 리뷰와 PR 초안 리뷰는 제가 직접 했습니다.

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Verdict: MUST_FIX: cleanup errors can produce a successful gate status, and the new cancellation fixtures duplicate one fact.

Must fix

1. Preserve cleanup errors in the gate status

tools/gate-bound.sh:93 Both cleanup calls discard lease_stop's failure. A TERM followed by a child exiting 0 therefore returns 0 even when process-table inspection fails with 70. A probe using ps() { sleep 0.1; return 1; } reproduces this.
Fixing the shared runner prevents every gate caller from accepting this cleanup failure. Preserve the error in rc; a later nonzero child status still fails the gate:

run_bounded() {
  local traps s rc=0 BIN_PID='' BIN_SENT=''
  traps=$(trap -p TERM INT HUP)
  # shellcheck disable=SC2034 # the caller reads it
  BIN_SIGNAL=
  for s in TERM INT HUP; do
    # shellcheck disable=SC2064 # the signal's name now, the pid when the trap runs
    trap "BIN_SIGNAL=$s; [ -z \"\$BIN_PID\" ] || { trap '' TERM INT HUP; BIN_SENT=1; lease_stop \"\$BIN_PID\" || rc=\$?; }" "$s"
  done
  "$@" <&0 &
  BIN_PID=$!
  if [ -n "$BIN_SIGNAL" ]; then
    trap '' TERM INT HUP
    [ -n "$BIN_SENT" ] || lease_stop "$BIN_PID" || rc=$?
  fi
  wait "$BIN_PID" || true
  wait "$BIN_PID" || rc=$?
  BIN_PID=''
  trap - TERM INT HUP
  eval "$traps"
  return "$rc"
}

2. Share the fork-race fixtures (R14)

tools/ref/lease.sh:1314 and tools/ref/stack-watch.sh:256 embed the identical Python timeout. Their bounded_ready and fork_ready helpers also encode the same readiness predicate.
Each change to the simulated fork race now needs two edits. Sharing these fixtures removes more code than it adds.
Put these helpers in tools/ref/cancel-stubs.sh:

cancel_timeout() {
  cat > "$1" <<'PY'
#!/usr/bin/env python3
import os
import signal
import subprocess
import sys

os.setpgid(0, 0)
signal.signal(signal.SIGTERM, lambda *_: sys.exit(143))
args = [a for a in sys.argv[1:] if not a.startswith('--kill-after=')]
child = subprocess.Popen(args[1:], close_fds=False)
try:
    rc = child.wait(timeout=float(args[0]))
except subprocess.TimeoutExpired:
    child.kill()
    child.wait()
    rc = 124
sys.exit(128 - rc if rc < 0 else rc)
PY
}
cancel_ready() {
  local sp ip state
  [ -s "$1" ] && read -r sp ip < "$1" || return 1
  state=$(ps -o stat= -p "$ip" 2> /dev/null) || return 1
  case $state in *S*) return 0 ;; esac
  return 1
}

Source that file inside each self-test: source "$LEASE_TREE/tools/ref/cancel-stubs.sh" in lease.sh and source "${self%/*}/cancel-stubs.sh" in stack-watch.sh. Replace both timeout heredocs with cancel_timeout "$t/fork-bin/timeout". Delete bounded_ready/fork_ready and rename their calls to cancel_ready. Keep the existing chmod calls.

Checked

  • Duplicates: the two timeout heredocs are identical; the readiness predicates are identical after excluding comments.
  • Complexity: no Rust functions changed; run_bounded's signal and cleanup transitions inspected.
  • Silent failure: both discarded lease_stop statuses reach run_bounded's result, which the gate runners use as their exit status.
  • Tests: 27 changed shell scripts and the Python timeout stub pass syntax checks. With a failed process read and a child exiting 0 after TERM, the cancellation probe returns 0 at the head and 70 with the fix. A child status of 7 remains 7. Both inline replacements match the full fix and target added lines.
  • Gates: the dump timing case carries a dated PIN.
  • Escape catalog: neither retained finding needs an exception to the review exclusions.

Notes

Dryer and crapper did not run because no Rust file changed. Full self-tests were not rerun here.

🔨 Shingler, bloomery's review bot: approves a clean pull request and requests changes on a must-fix (how it works)

Comment thread tools/gate-bound.sh
BIN_SIGNAL=
for s in TERM INT HUP; do
# shellcheck disable=SC2064 # the signal's name now, the pid when the trap runs
trap "BIN_SIGNAL=$s; [ -z \"\$BIN_PID\" ] || { trap '' TERM INT HUP; BIN_SENT=1; lease_stop \"\$BIN_PID\" || :; }" "$s"

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Preserve cleanup failure so a child exiting 0 cannot make the gate pass after lease_stop fails.

Suggested change
trap "BIN_SIGNAL=$s; [ -z \"\$BIN_PID\" ] || { trap '' TERM INT HUP; BIN_SENT=1; lease_stop \"\$BIN_PID\" || :; }" "$s"
trap "BIN_SIGNAL=$s; [ -z \"\$BIN_PID\" ] || { trap '' TERM INT HUP; BIN_SENT=1; lease_stop \"\$BIN_PID\" || rc=\$?; }" "$s"

Comment thread tools/gate-bound.sh
BIN_PID=$!
if [ -n "$BIN_SIGNAL" ]; then
trap '' TERM INT HUP
[ -n "$BIN_SENT" ] || lease_stop "$BIN_PID" || :

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Preserve cleanup failure when cancellation arrives before the child PID is recorded.

Suggested change
[ -n "$BIN_SENT" ] || lease_stop "$BIN_PID" || :
[ -n "$BIN_SENT" ] || lease_stop "$BIN_PID" || rc=$?

@juliankang4

Copy link
Copy Markdown
Collaborator Author

Superseded by a new pull request from the same branch; the link follows.

@juliankang4

Copy link
Copy Markdown
Collaborator Author

Superseded by #319.

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

Labels

tools Tools, recipes, CI and the toolchain: tools/, justfile, .github/, Cargo

Projects

None yet

Development

Successfully merging this pull request may close these issues.

gpu-gate self-test: the outer timeout leaves the inner gate alive

1 participant