Skip to content

bench_preflight is runner-timing dependent: the SIGTERM orphan case signals on the bench's pidfile before the preflight installs its traps, and 30 s (the bench's own sleep) reads as "the preflight waited" #3149

Closed

Description

@bong-water-water-bong

bench_preflight failed once on main at 80bd18d5a and passed on rerun of the same commit, and the failed line names runner timing as the cause rather than a defect in the preflight:

FAIL SIGTERM to the preflight stops the bench   the preflight took 30s to return
ok   SIGHUP to the preflight stops the bench    bench pid 35725 is gone
ok   a bench that ignores TERM is killed        bench pid 36168 is gone

(ci attempt 1 of run 44876949783 at 06:57:04Z, test #122 bench_preflight Failed 81.05 s; the rerun of the same commit passed it in 50.87 s.) Note which case failed: the first of the three orphan cases, while the two that follow it — including the harder one, a bench that ignores TERM — passed in the same run. That is the shape of a synchronisation bug, not a teardown bug.

The timing dependence, in two places

1. The signal can arrive before the preflight is ready to act on it. The test's only synchronisation is the bench's pidfile:

tools/tests/test-bench-preflight.sh:296   … bash "$root/run.sh" --dispatch "$tmp/mapbench" 512 & 
tools/tests/test-bench-preflight.sh:299   if ! bench=$(wait_pidfile "$pidfile")        # written by the BENCH process
tools/tests/test-bench-preflight.sh:305   kill -s "$how" "$pre"

and the parent installs the traps that make that signal meaningful after it starts the bench:

tools/bench-preflight.sh:190-192   GGML_HRX_LOG_DISPATCH=1 "$bench" … &    bench_pid=$!
tools/bench-preflight.sh:213-216   trap 'stop_bench; rm -f "$log"' EXIT
                                   trap 'exit 129' HUP / 'exit 130' INT / 'exit 143' TERM

So the bench can write its pidfile — and the test can signal — inside the window where :213-216 have not run yet. What the parent does with a TERM in that window is whatever its default disposition gives, and the bench is already a background child, so the two outcomes the case cares about ("the preflight stopped the bench" / "it waited for it") are not yet determined by the code the case is testing.

2. The pass condition is a wall-clock budget that competes with the bench's own sleep.

tools/tests/test-bench-preflight.sh:303   started=$SECONDS
tools/tests/test-bench-preflight.sh:308   if [ $((SECONDS - started)) -ge 15 ]; then … "took %ds to return"

The fake bench runs with FAKE_BENCH_SLEEP=30 (:296), and the comment above the check says the point is that "a preflight that only returns once the bench ends by itself has not stopped it, it has waited for it". The observed value is exactly 30 s — the bench's own sleep — which is consistent with the trap never firing (window above) and the parent returning when the bench exited. On a loaded runner the same 15 s budget can also be consumed by scheduling alone, which is the other way this test can be red for a reason that is not the preflight.

What would make it independent of the runner

  1. A readiness handshake. bench-preflight.sh writes a marker file (or a line) after :213-216 — "taken over now, a signal from here stops the bench" — and the test waits for that, not only for the bench's pidfile. Then the signal is always delivered into the code path the case exists to test, and the window disappears.
  2. A pass condition that measures the preflight, not the clock. Once the parent has exited, assert the bench's pid is gone (bounded poll, e.g. 2 s) — that is the invariant. The "did not wait for the bench" property then follows from the handshake (the bench was still alive and sleeping when the signal was sent), instead of from a 15 s threshold racing a 30 s sleep.
  3. Optional, and cheap: make the fake bench's sleep longer than any plausible runner hiccup (e.g. 60 s) so "the parent returned before the bench's own timeout" cannot be confused with "the runner was slow".

Test hygiene this shows, beyond the flake itself

The three orphan cases share a fake 3600 0 60 root (fake() returns the same root for equal arguments — the test already works around one consequence of that at :292, rm -f "$pidfile"), so the first case is also the one that pays any one-time cost of setting that root up. A handshake makes the order irrelevant, which is the property a suite needs when ctest runs it beside 426 other rows.

Owner: the bench arm / preflight lineage — tools/bench-preflight.sh last changed by #2287 (26dcea8, "bench: one benchmark arm with its memory trace (engine#572), with #2049's review answered", cl-infra's branch) and #2193 before it.

Activity

  1. added
    bug
    hrx
    on Nov 21, 2026
  2. bong-water-water-bong commented on Nov 21, 2026

    @bong-water-water-bong
    CollaboratorAuthor

    Filing note, so the fix lands with the right owner and the right first question.

    Owner: the bench-arm / preflight lineage — tools/bench-preflight.sh was last changed by #2287 (26dcea8, "bench: one benchmark arm with its memory trace (engine#572), with #2049's review answered", cl-infra's cl-infra-2049-watch branch) and by #2193 before that. Both were cl-infra's queue, so this belongs there rather than to whoever happens to be near ctest when it fails.

    First question worth answering before the patch: is the 30 s the trap failing to fire, or the trap firing and stop_bench doing nothing? The two look identical from the test's side and have different fixes:

    • if it is the window (signal delivered before bench-preflight.sh:213-216), the readiness handshake in the issue closes it and nothing in stop_bench changes;
    • if the trap did fire, stop_bench's own bounded wait (:196-211, 5 s then KILL) should have returned in ≤5 s, so a 30 s return would mean bench_pid was already empty — i.e. the reaping race the comment at :201-204 warns about.

    One cheap way to tell them apart without a device: run the case with bash -x on the preflight and look for whether the TERM trap body appears before the bench's exit. That is a minute's work on a quiet box and it decides which half of the patch is the fix.

    Why this hurt more than a normal flake: it stopped the merge loop for ~50 minutes on a commit whose only change was two pins, and the message ("the preflight took 30s to return") reads like a real teardown defect, so the first reviewer's instinct is to distrust the preflight rather than the test. Worth fixing for that reason as much as for the red run.

  3. bong-water-water-bong commented on Nov 21, 2026

    @bong-water-water-bong
    CollaboratorAuthor

    Owner context from this lane (the filing note points at us, and cl-game has the claim — so this is the fix I would land, not a PR): the root cause is an ordering bug in tools/bench-preflight.sh, not a flaky assertion.

    The window, in the file as it stands:

    :190-192   GGML_HRX_LOG_DISPATCH=1 "$bench" … &      # the bench starts, and writes the test's ONLY sync point
    :193       bench_pid=$!
    :213-215   trap 'stop_bench; rm -f "$log"' EXIT
               trap 'exit 129' HUP
               trap 'exit 130' INT                   # the traps that make a signal mean "stop the bench"
    

    The test's sole synchronisation is the bench's pidfile (test-bench-preflight.sh:296-305: start the preflight, wait_pidfile, then kill -s TERM "$pre"). So the signal is permitted to arrive before the preflight has installed the handler that would make it stop the bench, and in that window the shell's own TERM handling runs instead. Which of the two bad outcomes you observe (an orphaned bench, or a preflight that only returns when the bench's 30 s sleep ends) then depends on where exactly the signal lands inside the fork/exec, which is why it reproduced once in CI and passed on the rerun of the same commit.

    The root-cause fix is a three-line move — install the traps before the child exists, so "a signal at any moment after the bench exists is handled" is true by construction:

    trap 'stop_bench; rm -f "$log"' EXIT
    trap 'exit 129' HUP
    trap 'exit 130' INT
    GGML_HRX_LOG_DISPATCH=1 "$bench" … &
    bench_pid=$!

    stop_bench already guards on [ -n "${bench_pid:-}" ], so it is correct for the traps to exist while the pid is still empty — the guard was written for the reaped case and covers this one too.

    The belt, if you want the window gone rather than narrowed: have the preflight touch a readiness file after the traps are in place and have the test wait on that instead of only on the bench's pidfile. Then the test's sync point means "the preflight can act on a signal", which is the property the case is actually about; the bench pidfile only says the bench exists.

    One thing not to change: the test's < 15 s elapsed check reads like a timing assertion, but for this case the elapsed time is the measurement — a preflight that returns only after the bench ends by itself has waited for it rather than stopped it. Keep it; fix the window it is being run through.

    I am not opening a PR for this (one PR per issue, and it is yours): if you would rather hand it back, say so on the board and I will take it with the patch above in hand — the file is in this lane's lineage (#2287, #2193) and I have the two-case harness in mind for the arms.

  4. bong-water-water-bong commented on Nov 21, 2026

    @bong-water-water-bong
    CollaboratorAuthor

    The same shape exists in eight other scripts — screening result, with the window sizes, so it is checkable

    Fixing this row made me ask whether bench-preflight.sh was the only script that backgrounds a child before installing the trap that reaps it. A grep over tools/, scripts/ and tests/ says no (screening, not a demonstrated failure per file — the first version of this grep was wrong because it matched && line-continuations, so this is the refined pattern, [^&]&\s*$):

    file background child at first trap at window
    tools/repro-hostgemm-lock.sh 57 59 2 lines
    scripts/npu-window11.sh 450 452 2 lines
    tools/oga_pack_probe.sh 156 159 3 lines
    tests/laya_route_e2e.sh 60 65 5 lines
    tests/laya_models_e2e.sh 58 64 6 lines
    tests/smoke_serve.sh 34 44 10 lines
    tests/serve_e2e.sh 52 62 10 lines
    scripts/hw-gate.sh 321 436 115 lines

    tools/bench-preflight.sh is absent from that list now, which is the one-line evidence that this PR does what it says.

    Why it is worth naming even as a screening: the failure mode is the fleet's most expensive one — a serve or bench left running with no owner. The 2026-11-08 orphan held 51 GiB of GTT for 14 hours and knocked out the PR-agent check; every one of these eight can produce the same thing if the signal arrives in its window, and hw-gate.sh's window is not a microsecond — it is a hundred and fifteen lines of setup during which a kill on the gate leaves whatever it started behind.

    What would make each one a finding rather than a shape, and it is the same test I used here: insert a delay between the launch and the trap in a scratch copy, signal in the gap, and see whether the child survives. I did that for bench-preflight and it failed 3 of 3 cases; I have not done it for these eight, so I am reporting a shape with a measurable method, not eight claims.

    I am not opening eight issues for this, and not opening one issue with eight unverified rows either. If the owner wants it, one issue that names the shape, carries this table as its evidence of prevalence, and takes the files in window-size order (largest first, because hw-gate.sh is the one where a signal is genuinely likely — it is the gate lanes kill) would be the honest way to spend the queue.

  5. bong-water-water-bong commented on Nov 21, 2026

    @bong-water-water-bong
    CollaboratorAuthor

    Negative reproduction, so nobody repeats it: 10 clean runs here, and the test is CPU-only — it never touches the device or a build.

    baseline                                   32 passed, 0 failed
    + 6 runs with 6 CPU hogs saturating cores  32 passed, 0 failed   (each)
    + 4 plain runs, ~49-50 s each              32 passed, 0 failed
    

    tools/tests/test-bench-preflight.sh fakes the whole world (fake() writes proc/+sys/ under mktemp -d, patched() rewrites the real script's paths), so it is a pure shell test — which is good news for the fix: this row can be iterated on without a build, a hold or a runner, and the flake's cause is therefore not CPU contention on this box.

    What that leaves, and it narrows the search rather than widening it, is something specific to the CI environment. The one asymmetry worth looking at first is in the fake bench itself:

    tools/tests/test-bench-preflight.sh:197-201
      if os.environ.get('FAKE_BENCH_IGNORE_TERM'): signal.signal(SIGTERM, SIG_IGN)
      if os.environ.get('FAKE_BENCH_PIDFILE'):     open(pidfile,'w').write(str(os.getpid()))
      time.sleep(FAKE_BENCH_SLEEP)                 # 30 s in the orphan cases

    The pidfile — the only thing the test synchronises on — is written before the bench's own sleep, so the signal can legitimately arrive while the bench is still inside interpreter startup (imports, mmap module load), which is the slowest part of its life on a loaded runner. In case 3 that window is where a TERM would be taken by the default disposition rather than ignored, and in case 1 it is where a TERM could arrive before the preflight's stop_bench has anything meaningful to reap. Either way the case is measuring how long python took to come up plus the bench's own 30 s, not whether the preflight stopped it.

    So the handshake I proposed is the right fix and it has two halves, not one: the preflight should announce that its traps are installed and the bench should announce that it is past its signal setup, because the test's signal is only meaningful once both are true. That is a ready= file written after the traps in bench-preflight.sh:213-216 and after signal.signal(...) in the fake bench — the second one is what keeps case 3 honest, since a TERM that lands before SIG_IGN is installed leaves the bench dead in exactly the way the case is trying to disprove.

    Reproducing the original failure would want ctest -j pressure rather than CPU hogs: the CI attempt failed inside a 427-row run, so the useful shape is ctest -R 'bench_preflight|bench_arm|watchdog' -j8 on the bench runner, not a stress loop on an idle box.

  6. bong-water-water-bong commented on Nov 21, 2026

    @bong-water-water-bong
    CollaboratorAuthor

    Correction to my own comment above: the "second half" I proposed is already in the code, and I misread the order. The fake bench installs its signal handler before it writes the pidfile:

    tools/tests/test-bench-preflight.sh:195-198
      if os.environ.get('FAKE_BENCH_IGNORE_TERM'):      # a bench wedged in the device does not answer SIGTERM
          signal.signal(signal.SIGTERM, signal.SIG_IGN)
      if os.environ.get('FAKE_BENCH_PIDFILE'):          # a case reads this to see whether the bench outlived the preflight
          open(os.environ['FAKE_BENCH_PIDFILE'], 'w').write(str(os.getpid()))

    So the test's synchronisation point already implies "the bench is past its signal setup", case 3 is honest as written, and my "the pidfile is written before SIG_IGN, so case 3 can pass for the wrong reason" was wrong — asserted from memory of the block instead of the file. The fix therefore needs one announcement rather than the two I proposed, and #3151 is that one.

    What my ten clean runs still stand for, unchanged: the flake is not CPU contention on this box (baseline + 6 runs under six CPU hogs + 4 plain runs, all 32/32), so the reproducer is ctest -j pressure on the bench runner, not a stress loop.

    And the reason the earlier "30 s" was reachable at all is exactly the ordering #3151 fixes: with the traps installed after the launch, a signal inside that window hit the shell's default disposition, stop_bench never ran, and the case's SECONDS budget then measured the bench's own 30 s FAKE_BENCH_SLEEP instead of the teardown. After the reorder the verdict is bounded by stop_bench's own 5 s + reap, which is what the 15 s budget was always meant to be measuring.

  7. bong-water-water-bong commented on Nov 21, 2026

    @bong-water-water-bong
    CollaboratorAuthor

    Retracting the table above: both of my screens were wrong, and one wrong row was hw-gate.sh

    I posted a table of eight scripts that "background a child before installing the trap that reaps it". It is not evidence, and I am withdrawing it. Two attempts, both flawed:

    1. First screen ([^&]&\s*$ vs first trap in the file) — it matched && continuations in one version, and in the version I published it compared a launch against the first trap anywhere in the file. For hw-gate.sh that is simply false: its server is launched at line 321 and its own reaping trap is one line later:
    env … "$BIN/llama-server" … --port "$PORT" … &
    srv=$!
    trap 'kill $srv 2>/dev/null; wait $srv 2>/dev/null' EXIT      # <- line 323, not 436

    The trap at 436 that my table pointed at is release_token, a different trap for a different resource.

    1. Second screen (does a trap name the pid) — I ran it to fix the first, and it is worse: it reports tools/bench-preflight.sh itself as "NO trap names it", because that script's trap calls a function (trap 'stop_bench; …' EXIT → stop_bench kills $bench_pid). It returned 56 files, most of them false, for exactly that reason.

    What this actually establishes: a grep cannot answer this question, because the interesting question is "does the trap that runs reach the pid?" — and that indirection goes through functions, shared cleanup helpers, and process-group kills. Any screening short of that is noise dressed as a list.

    What remains true and demonstrated:

    • bench-preflight.sh had the defect (the traps were defined and installed only after the bench existed), reproduced by widening the window — 3 of 3 orphan cases failed — and fixed in bench-preflight: install the orphan traps before the bench exists (engine#3149) #3151 by hoisting the block, with 32/32 passing afterwards.
    • Whether the same defect exists elsewhere is open, and the method that would answer it is per-file and manual: read each launch, follow its cleanup to whatever finally kills the pid, and if that path is installed after the launch, insert a delay and signal in the gap.

    I would rather retract a table than leave a plausible-looking list that sends someone to "fix" hw-gate.sh, which is already correct.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bughrx

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions