Skip to content

fix(check): serve runs waiting for a slot first come, first served - #1886

Merged
devswha merged 2 commits into
mainfrom
fix/check-admission-fifo
Oct 24, 2026
Merged

devswha merged 2 commits into
mainfrom
fix/check-admission-fifo

Conversation

@devswha

@devswha devswha commented Oct 24, 2026

Copy link
Copy Markdown
Owner

Fixes #1881.

What failed

A herdr-backed bun run check waiting for a slot could wait out all of CHECK_WAIT_SECONDS and be refused while newer runs got slots ahead of it. #1863's "Browser regression (other scripts)" (run 40096402408) waited the full 2400 s for one of the P620's 3 slots, held in turn by runners 2, 6, 7, 8 and 12, and exited 1 with no test failing.

Root cause

acquireWaiting asked acquire again every 2 s, and acquire gave a free slot to whichever run reached the admission port first, whether it had waited 40 minutes or arrived a moment ago. Nothing recorded who had been waiting, so under steady load a waiting run could lose every round.

Fix: a first-come-first-served queue on the shared loopback

  • A run refused as busy takes a ticket: a port above the admission port (QUEUE_TICKETS = 24 of them) that answers when the run first found the PC busy. A ticket is a listening port, so a run that dies leaves no place behind, and the runner containers of a host already share that loopback for their slots (confirmed with lane ci-p620-max: one loopback network per host, one queue per host).
  • acquire refuses a run that wants a slot while a run with an older ticket waits; a run with no ticket comes after every ticket.
  • A waiting run (acquireWaiting's retries) with an older run ahead of it is refused before it takes the admission port, and asked again under it. One that held the port only to find the queue could keep the head out of it round after round (a livelock Codex found in review, also for waiters left without a ticket when all 24 are held). A run's first try meets its checkout and resource claims first, so a second run in the same checkout is still refused as same-checkout at once.
  • The ticket goes when the run is admitted, refused for good or out of time. Older checks ignore tickets; nothing breaks while both run. MAX_LOCK_PORT drops by QUEUE_TICKETS. Messages say when queued runs, not the run limit, caused a refusal.
  • Known limit: runs asking for different slot counts (only while a host changes CHECK_HOST_MAX_RUNS) still queue in one line, so a head that does not fit yet holds back a later run that would.

Proof (deterministic, no sleeps)

acquireWaiting takes the clock and the wait between rounds as seams; each test drives every round explicitly:

  • a freed slot goes to the run that waited, not to one that arrives after it;
  • two waiters are served in the order they first found the PC busy, whichever asks first (the earlier one holds the higher ticket port, and a probe between them is told only the earlier one is ahead);
  • a run behind the head never takes the admission port, with or without a ticket;
  • a second run in the same checkout is refused same-checkout at once while runs wait;
  • a run gives its place up when it stops waiting, and still waits when every ticket is held.

Mutations, each failing a named assertion: order by port only; the early check dropped; the early check for every run; retry ignored; the queue disabled. The slots tests: 32 runs, 4 at a time, 0 failures. The existing "stops waiting at its deadline" test now runs on the seams instead of a real 60 ms, and the tests' free port ranges cover the ticket ports.

Reviews

  • omo-native-code-reviewer: APPROVE (three rounds; its findings — test port ranges, ordering by time not port, same-checkout refusals hidden behind the queue, messages — are fixed).
  • Codex (Sol 6.1, codex exec, four rounds): the livelock (P2) and the overflow-waiter starvation (P2) are fixed; final round: no findings.

bun run check fast: passed (6398 pass, 0 fail). CI behaviour change in scripts/check.ts: a real review is needed.

@coderabbitai

coderabbitai Bot commented Oct 24, 2026 •

Copy link
Copy Markdown

Warning

Review limit reached

You've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository.

Next included review available in 23 minutes.

Check out review usage here.

View limit details

Limit details: You’ve used all 4 included reviews currently available.

Learn how review limits work.

Review configuration:

Run configuration
Commits

Reviewing files that changed from the base of the PR and between 3346b03 and ce89824.

Files selected for processing: 2
  • scripts/check.test.ts
  • scripts/check.ts
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@devswha

devswha commented Oct 24, 2026

Copy link
Copy Markdown
Owner Author

CodeRabbit's review was rate-limited ("Review limit reached"), so no CodeRabbit review ran on this PR. The reviews below stand in for it; this PR changes CI admission behaviour, so it also waits for the maintainer's real review.

omo-native-code-reviewer (three rounds): final APPROVE, no blockers. Resolved findings: test free port ranges now cover the ticket ports; ordering is proven by time, not port (the earlier waiter holds the higher port; a probe between them sees only the earlier one ahead); the early queue check no longer hides same-checkout and resource refusals (a run's first try meets its claims first); messages say when queued runs caused a refusal; CHECK_LOCK_PORT doc mentions the ticket ports; the newcomer assertion names the admission port as holder. Not changed: more than 24 simultaneous waiters (12 runners per host) and head-of-line blocking while hosts change slot counts (noted in a comment).
Codex (Sol 6.1, four rounds): P2 livelock (a later waiter holding the admission port while it scanned tickets kept the head out) — fixed by refusing waiting runs before the admission port; P2 overflow starvation (a waiter left without a ticket bypassed that check) — fixed with an explicit retry signal from acquireWaiting; final round: no findings. It confirmed containers on one kernel share the wall clock used for queue times.

@devswha

devswha commented Oct 24, 2026

Copy link
Copy Markdown
Owner Author

Merge pass, before review: this PR conflicts with main in scripts/check.test.ts after #1884 (03bd5e5), which changed the check CLI fixture's environment. Please rebase onto main. #1872 (drop HERDR_CONFIG_PATH in isolate()) also changes scripts/check.ts and scripts/check.test.ts. It is older and already approved, so it merges first. The merge pass starts its review (QA plus a code review) on the rebased head. Since this changes how every bun run check run is admitted, the review will include a contention probe on a private CHECK_LOCK_PORT.

@devswha

devswha commented Oct 24, 2026

Copy link
Copy Markdown
Owner Author

Will do: I'll rebase onto main once #1872 has merged (watching it), so this PR is pushed once on top of both #1884 and #1872, then run bun run check fast before the push.

@devswha

devswha commented Oct 24, 2026

Copy link
Copy Markdown
Owner Author

Plan change under merge focus: instead of waiting for #1872, I'll push this branch rebased on current main (it already includes #1884 and #1885) as soon as its bun run check fast finishes, so the conflict is gone and your review can start. If #1872 later conflicts with it, I'll rebase once more.

Also in the rebased head: the first CI run here failed the two "check CLI teardown" tests at 90 s, and that was a real interaction with the queue, not only the missing #1884. The fixture's throwaway checks used the shared default lock range, so on a busy runner host they waited behind every real run holding a ticket. They now use a private lock base (reproduced with a stand-in ticket on a private CHECK_LOCK_PORT: the old fixture fails at 90.3 s, the new one passes in 0.7 s). The native reviewer approved that delta.

@devswha
devswha force-pushed the fix/check-admission-fifo branch from 2394210 to 5d02701 Compare October 24, 2026 15:40
@devswha

devswha commented Oct 24, 2026

Copy link
Copy Markdown
Owner Author

Merge pass review of 5d02701. Under the maintainer's rule this is reviewed by Codex Sol, the omo code reviewer, and the merge pass's QA. The code review is still running; any findings it adds will follow. Codex Sol fails it on three P2 defects:

1. A ticket lookup can block admission indefinitely (scripts/check.ts:391)

  • The lookup uses an inactivity timeout, then resolves only when the socket closes. An unrelated service on a ticket port that sends now and then keeps the lookup alive.
  • The lookup runs while the run holds the admission port, so it blocks every other admission too.
  • Evidence: with 100 ms heartbeats and waitMs: 1000, admission was still pending after 3.3 s and still holding the admission port. CHECK_WAIT_SECONDS can't end this wait.
  • Fix: give each lookup an absolute deadline, not an inactivity timeout, and treat a port that doesn't answer like a ticket as "not ours".

2. A retry can hide a same-checkout conflict until the timeout (scripts/check.ts:512)

  • An older ticket is honoured before checkout and resource ownership is checked. A run already refused once as busy then waits behind unrelated jobs, even though its own checkout is in use.
  • Evidence: a controlled 5 s wait ran out as busy without reading the checkout marker once. Main's logic returns same-checkout for the same state.
  • Fix: check same-checkout and resource ownership before the queue order.

3. A backward clock step reverses FIFO order (scripts/check.ts:608, :422)

  • Arrival order is wall-clock time. A ticket at 1000, then a later ticket after a clock correction to 900, admitted the later run first.
  • Fix: order by a counter the admission holder hands out, or by performance.timeOrigin + performance.now() taken from one process. In any case, use nothing that a clock correction can move.

Holds

  • The FIFO tests pass, and three queue assertions fail against main's acquire.
  • Ticket exhaustion, concurrent claims, equal-time ties, cleanup and duplicate admission all pass.
  • Older runs keep slot safety but bypass FIFO. That is acceptable during a rollout.
  • 71 check and conventions tests pass, and the typecheck passes.

@devswha

devswha commented Oct 24, 2026

Copy link
Copy Markdown
Owner Author

Merge pass, code review result (omo-native-code-reviewer): PASS. It probed real check runs through main() on private ports.

  • 4 waiters were admitted in arrival order, twice. The old check.ts admitted the same probe out of order.
  • A killed ticket holder freed its port within 0.5 s.
  • CHECK_WAIT_SECONDS=5 ended the wait at 5.03 s with a clear error.
  • A port held by something unrelated was skipped.
  • With all 24 tickets taken, runs fell back to the old race and every run was still admitted.
  • Old and new check.ts runs mixed without failures.

A peer that accepts and never answers cost about 2 s per round. That is bounded, unlike Codex finding 1, where the peer keeps sending. The three Codex P2 findings above still need a fix before merge.

A herdr-backed check waiting for a slot could wait out all of CHECK_WAIT_SECONDS and be
refused while newer runs got slots ahead of it: #1863's "Browser regression (other scripts)"
waited the full 2400 s for one of the P620's 3 slots and exited 1 with no test failing.
acquireWaiting asked acquire again every 2 s, and acquire gave a free slot to whichever run
reached the admission port first.

A run refused as busy now takes a ticket, its place in the queue: a port above the admission
port (QUEUE_TICKETS of them) that answers when the run first found the PC busy. Under the
admission port, acquire refuses a run that wants a slot while a run with an older ticket waits;
a run without a ticket comes after every ticket. A ticket is a listening port, so a run that
dies leaves no place behind, and the runner containers of a host already share that loopback for
the slots. The ticket goes when the run is admitted, refused for good or out of time; a run that
finds every ticket taken waits without one and tries again each round. Older checks ignore
tickets; nothing breaks while both run. MAX_LOCK_PORT drops by QUEUE_TICKETS. A waiting run
(acquireWaiting's retries, with a ticket or without one when all were held) with an older run
ahead of it is refused before it takes the admission port, and asked again under it (a run's
first try meets its checkout and resource claims first, so a second run in its checkout is
still refused as such at once): one that held the port only to find the queue would keep the head out of it, round
after round (a livelock found in review).

acquireWaiting takes the clock and the wait between rounds as seams, so the tests drive every
round: a freed slot goes to the run that waited, not to one that arrives after it; two waiters
are served in the order they first found the PC busy, whichever asks first; the place goes when
the run stops waiting; a run still waits when every ticket is held; a run behind the head
never takes the admission port. The earlier waiter holds the higher ticket port, so only the
time can order them. Ordering by port only, dropping the check before the admission port,
making it apply to every run, or the queue itself each fail a named assertion. The tests' free port ranges now cover the ticket ports, so a
waiting run elsewhere on the loopback cannot come ahead of a test's run. A refusal by queued
runs says so instead of blaming the run limit.

Merge-pass review fixes: a ticket lookup has an absolute 1.5 s deadline and drops a listener that
does not answer like a ticket (one that kept talking held the admission port indefinitely); a
waiting run reads its checkout and resource markers before the queue order, so a same-checkout or
resource conflict is refused at once; a new ticket ranks after every live ticket whatever the clock
says (a clock stepped back reversed the order), and tickets are taken one run at a time on a ticket
desk port above the admission port, so two runs taking places at once cannot miss each other; a run that finds the desk taken waits
for it (up to 3 s) instead of a whole round, in which a later run could take a place first.

The check CLI tests' throwaway runs now use a lock range of their own: on the shared one they
waited in line behind every real run waiting for a slot (with a stand-in run waiting on a private
lock base, the old fixture fails after 90 s as in this PR's first CI run; the new one passes in
0.7 s).

Fixes #1881
@devswha
devswha force-pushed the fix/check-admission-fifo branch from 5d02701 to 1e5bd74 Compare October 24, 2026 16:16
@devswha

devswha commented Oct 24, 2026

Copy link
Copy Markdown
Owner Author

Pushed 1e5bd74 (was 5d02701). It fixes the three Codex P2 findings the merge pass raised:

  1. Ticket lookup: an absolute 1.5 s deadline. A listener that isn't a ticket is dropped at once, so a peer that keeps sending can't hold the lookup.
  2. Marker order: a waiting run reads its checkout and resource markers before the queue order. A second run on the same checkout is refused at once.
  3. Clock step: a new ticket now ranks after every live one. It is taken under a "ticket desk" port (admission+1; tickets start at admission+2 and run up to MAX_LOCK_PORT-1). A run that loses the desk waits for it, polling every 10 ms with a 3 s cap, timed with performance.now().

Known limit, documented in a code comment: a ~10 ms window at the desk handoff, where runs that reach the desk together may swap order. Closing it would need a second queue. Sol accepted this as the remaining P2, and it closed findings 1, 2 and the clock bound.

Reviews of this delta: omo-native-code-reviewer APPROVE, with all LOWs applied except an optional test hook.

Tests:

  • slots and lock-port 37/37, conventions 10/10, the desk test 40/40, typecheck clean;
  • each mutation fails the test named for it;
  • bun run check fast passed on 1e5bd74 before the push.

The rebase onto main merges cleanly. #1872 also touches scripts/check.ts, so whichever of the two merges second may need a rebase.

@devswha

devswha commented Oct 24, 2026

Copy link
Copy Markdown
Owner Author

Merge pass, Codex Sol re-check of 1e5bd74: the three earlier P2s are fixed, but there is one new P2 regression.

Fixed

  • Lookup deadline (check.ts:406): a peer that kept sending used to hold admission past 3.3 s. Now the run finishes in 1.5 s and releases admission.
  • Ownership checked before the queue (check.ts:594): a same-checkout run is refused on its first retry. Resource and report conflicts also pass.
  • Backward clock step (check.ts:453): arrivals at 1000 and then 900 get ranks 1000 and 1001, so the later run stays behind.
  • Also passing: reverse-retry FIFO, reused lower ticket ports, a ticket holder dying, 24 concurrent ticket claims, recovery when every ticket is taken, mixed versions against main's check.ts, and the 65 check tests.

New P2: the ticket-desk wait can run past CHECK_WAIT_SECONDS (scripts/check.ts:443, callers at :685-687)

  • The desk wait has its own fixed 3 s bound. The caller neither passes the remaining budget nor checks whether the budget has run out before it tries again.
  • Evidence: with CHECK_WAIT_SECONDS=1, an occupied desk, and the slot freed at 1.5 s, this head admitted the run after 3,012 ms. The previous head returned busy after 1,002 ms.
  • Fix: pass min(3 s, remaining budget) to the desk wait, and check the deadline before every acquire. Please add a test where CHECK_WAIT_SECONDS is shorter than the desk wait.

The merge pass re-checks the fix, looking only at the changed range.

@devswha

devswha commented Oct 24, 2026

Copy link
Copy Markdown
Owner Author

Fixed the new P2 in ce89824. The ticket desk no longer waits past CHECK_WAIT_SECONDS.

  • Change: acquireWaiting passes deskWaitMs: min(DESK_WAIT_MS, remaining budget) to takeTicket. It also checks the deadline right after the ticket attempt, before it sleeps or acquires again, so a run out of time returns its busy refusal at once.
  • Test: scripts/check.test.ts, "waits for the ticket desk no longer than CHECK_WAIT_SECONDS leaves". With the slot and the desk both held and a 1 ms wait, the run is refused as busy well under half of DESK_WAIT_MS. Without the fix it fails after 3,008 ms, matching your 3,012 ms reproduction.
  • Results: scripts/check.test.ts passes 66, and bun run check fast passed on ce89824 before the push.

@devswha

devswha commented Oct 24, 2026

Copy link
Copy Markdown
Owner Author

Merge pass review-work report. Overall: PASSED. Under the maintainer's rule, a check-tooling behaviour change is reviewed by Codex Sol, the omo code reviewer and the merge pass's QA.

Lane Verdict
Code review (omo-native-code-reviewer, programming + remove-ai-slops) of 5d02701 PASS (WATCH)
Codex Sol of 5d02701 FAIL: three P2 (lookup could hang, same-checkout hidden, clock step reversed FIFO)
Codex Sol of 1e5bd74 the three fixed; one new P2 (desk wait overran CHECK_WAIT_SECONDS)
Codex Sol of ce89824 PASS

What was checked

  • Real contention: the code reviewer ran real check runs through main() on private ports. 4 waiters were admitted in arrival order, twice; the old check.ts admitted them out of order.
  • Failures and limits: a killed ticket holder freed its port within 0.5 s. CHECK_WAIT_SECONDS ended a wait with a clear error. With all 24 tickets taken, every run still got in.
  • Codex fixes:
    • A ticket lookup now has an absolute deadline.
    • Checkout and resource ownership are checked before queue order.
    • Ranks are monotonic, so a clock step can't reorder arrivals.
    • The desk wait stays within what is left of CHECK_WAIT_SECONDS.
  • Mixed versions: with main's check.ts, slots stay safe. Old runs bypass FIFO, as documented.
  • Tests: 66 check tests pass, and so does the typecheck. git merge-tree is clean against main and against fix(check): drop an inherited HERDR_CONFIG_PATH from isolated runs #1872, which shares check.ts and check.test.ts.

Follow-ups (LOW):

  • Runs without a ticket, when more than 24 are waiting, can race.
  • scripts/check.ts is far over the 250-line guideline; the queue would split into its own module.

All required checks are green and no review thread is open.

CodeRabbit did not review this PR (rate-limited or skipped). The reviews above stand in for it.

@devswha
devswha merged commit bf9762c into main Oct 24, 2026
10 checks passed
@devswha
devswha deleted the fix/check-admission-fifo branch October 24, 2026 20:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

check: a run waiting for a slot can be starved by newer runs (no queue)

1 participant