Skip to content

fix(backend-pi): hand a refused turn-temp close to the reconciler only once its teardown ends (#3488) - #4148

Merged
schlessera merged 2 commits into
mainfrom
fix/3488-pi-duplicate-resume-close-race
Nov 17, 2026
Merged

schlessera merged 2 commits into
mainfrom
fix/3488-pi-duplicate-resume-close-race

Conversation

@schlessera

@schlessera schlessera commented Nov 17, 2026 •

Copy link
Copy Markdown
Owner

Closes #3488

What changed

A pi worker's refused turn-temp close now reaches the process cleanup reconciler only once the teardown that awaited it has ended. Two owners still wait for the bounded stdio release after their close: the inactive abort() that tears down the loser of a concurrent duplicate resume, and a prompted turn's teardown. Before, the refusal was handed over the moment it happened (inside closeStorage). If the reconciler's first retry came due (CLEANUP_RETRY_BASE_MS, 1 s) while the release was still in flight, it made a second closeAsync attempt before the busy refusal (or the turn) settled. That is PI_DUPLICATE_RESUME_LOSER_CLOSED_BEFORE_REFUSAL with Expected: 1, Received: 2. Supervision now exposes teardown(run), and both owner teardowns run inside it. A refusal inside it is held and handed off when the teardown ends. The order of exit, close and stdio release is unchanged.

Cause. After #3547 the exit-driven close is claimed away, so only two sources of a second closeAsync remained before the refusal settles: the abort itself, and the reconciler's timed retry. The abort calls closeTurnTemp() once. The reconciler's retry only fires if the remaining teardown (stdioReleased(), bounded at 5 s) takes longer than 1 s. On this host the loser's real release takes 0–8 ms even under 32 CPU spinners, and 30/30 loaded runs of the unchanged test passed on main. So the window is narrow, but it is real: a stall of the stdio release past 1 s (a loaded hosted runner) produces exactly the reported failure. Holding the loser's released() for 1.2 s with the real reconciler timer fails main every time and passes with this fix.

The 2026-11-03 comment on #3488 predates #3547 (merged 2026-11-04), which fixed the exit-driven double close. No failure of this assertion has been reported since. This PR closes the one remaining path to a second close.

Why this shape

Not taken: releasing stdio before the turn-temp close, which would make the close the teardown's last wait and remove the window without new state. early-cancel-lifecycle.test.ts pins the current order (PI_EARLY_CANCEL_CLEANUP_PRECEDES_STDIO_RELEASE), and #2880 kept the #2863 prompted/startup lifecycle out of scope. I don't think changing that order is mine to decide here. I tried it, and it hangs all six early-cancel cases. Also not taken: loosening the assertion, or making the reconciler skip a held cleanup. The reconciler still owns the refusal, with the same notice, reservation and retry; only the hand-off moves to the end of the teardown. The startup-failure path needs no change, because its close is already its last wait.

Proof

Loops use bun run test (--timeout 30000) with a private TMPDIR on a Linux host with bwrap (the duplicate test runs real workers).

Deterministic reproduction. Both tests replace the process reconciler with one whose schedule records retries. The loser's (or the prompted worker's) released() fires every retry already due while the release is in flight. This is the 1 s timer coming due during a slow release, made exact.

Failing first (product files from origin/main d97405216, new tests):

error: PI_DUPLICATE_RESUME_LOSER_CLOSED_BEFORE_REFUSAL
Expected: 1
Received: 2
      at duplicate-acquisition.test.ts:148:75
(fail) ... refuses only the loser after its refused teardown
error: PI_PROMPTED_TEARDOWN_CLOSES_ONLY_IN_FOREGROUND
Expected: 2
Received: 3
      at worker-session.test.ts:248:70
(fail) a prompted Pi turn hands its refused turn-temp close to the reconciler only after its stdio release (#3488)

Mutations (each restored):

Mutation Result
inactive abort() runs outside teardown() duplicate test fails PI_DUPLICATE_RESUME_LOSER_CLOSED_BEFORE_REFUSAL (Expected 1, Received 2); prompted test passes
prompted teardown runs outside teardown() prompted test fails PI_PROMPTED_TEARDOWN_CLOSES_ONLY_IN_FOREGROUND (Expected 2, Received 3); duplicate test passes
teardown() drops the held refusal instead of handing it off PI_DUPLICATE_RESUME_REFUSED_LOSER_HAS_RETRY_OWNER and PI_PROMPTED_REFUSAL_HAS_RETRY_OWNER fail (Expected 1, Received 0)

CPU-load loops (32 busy-loop spinners on 16 cores; each run executes the duplicate test and the new prompted test):

Tree Runs Result
origin/main product code, unchanged test 30 (16 spinners) 30 pass: the natural window needs a stdio release over 1 s
origin/main product code, new tests 20 20 fail, every run on PI_DUPLICATE_RESUME_LOSER_CLOSED_BEFORE_REFUSAL and PI_PROMPTED_TEARDOWN_CLOSES_ONLY_IN_FOREGROUND
this head 50 50 pass / 0 fail

Real timer, no injected schedule (unchanged test, loser's released() held 1.2 s): origin/main fails PI_DUPLICATE_RESUME_LOSER_CLOSED_BEFORE_REFUSAL Expected 1 Received 2; this head passes.

Workspace: bun run test packages/ui-backend-pi gives 593 pass / 25 fail on this head and 592 pass / 25 fail on origin/main (same tree, product and test files swapped back). The 25 failing test names are identical (sandbox-environment failures). duplicate-acquisition, early-cancel-lifecycle, worker-session and worker-session-units together: 49 pass. bun run typecheck and bun run lint are clean.

bun run check:pr --base origin/main
{"version":1,"kind":"brain-check-pr-receipt","head":"0d310bc29749069313ffc2ef564ab6a179fd48d9","base":"origin/main","mode":"focused","startedAt":"2026-11-17T07:36:20.104Z","finishedAt":"2026-11-17T07:37:33.192Z","checks":[{"name":"all lint and leakage gates","categories":["changeset"],"outcome":"passed","elapsedMs":14602.217056000001},{"name":"environment documentation","categories":["changeset"],"outcome":"passed","elapsedMs":109.00419400000101},{"name":"pending changeset package names","categories":["changeset"],"outcome":"passed","elapsedMs":100.48994900000253},{"name":"contribution changeset","categories":["changeset"],"outcome":"passed","elapsedMs":20.93946599999981},{"name":"affected fast invariants","categories":["verify","unit"],"outcome":"passed","elapsedMs":58236.94838999999}]}

Checklist

  • Focused local preflight/behavioral receipts and complete selected hosted proof passed; actual head/base/job checkout evidence is in Proof. (Local receipts above; hosted proof is pending.)
  • Acceptance criteria from the issue are all met, or the gap is named below.
  • A PR touching packages/ui-react/, packages/ui-kit/, packages/ui-server/ or packages/core/ carries a recorded independent review of its head (bun scripts/review-receipt.ts check <pr>), or it touches none of them. (Touches none.)
  • The PR and completed/advanced issues have verified release milestones under docs/process/github.md.
  • A changeset is included, or this changes nothing a consumer can see.
  • No personal data anywhere in the diff — the leakage gate covers the whole tree.
  • The integration contract index or relevant component is updated in this same commit, or nothing in the machine surface moved.
  • Docs under docs/ reflect the change, or nothing documented moved. (governed-writable-workers.md, backends: a pi duplicate-resume busy refusal settles before the losing worker's teardown #3488 paragraph, plus its four drifted citations.)
  • A fail-closed guard added or changed answers the proportionate-containment checklist in this body, or no guard changed. (No guard changed; a refused close still keeps its reservation charged and retryable.)
  • Provider history stays append-only (docs/decisions/provider-history.md): the provider-history guard passes, or nothing on a turn's path changed. (Worker teardown only; nothing provider-facing.)

Collisions

bun scripts/collisions.ts --json --pr 4148 at head 0d310bc29: no path collisions. Closure-only reports through shared workspace dependencies, with #3056, #3059, #3069, #3581, #3622, #4002, #4036 and #4145. None of them edits worker-launch.ts, worker-session.ts or the two test files.

Anything left open

Nothing.

🤖 Generated with Claude Code

…y once its teardown ends (#3488)

The inactive abort that tears down a duplicate-resume loser, and a prompted
turn's teardown, await the bounded stdio release after the turn-temp close.
A refused close was handed to the process cleanup reconciler at once, so
its timed retry (first due after one second) could make a second close
attempt while the release was still in flight: the loser's busy refusal
then settled with two closes, failing
PI_DUPLICATE_RESUME_LOSER_CLOSED_BEFORE_REFUSAL (Expected 1, Received 2).

Supervision now runs both owner teardowns through teardown(), which holds
a refused close and hands it to the reconciler when the teardown ends. The
order of exit, close and stdio release is unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@schlessera schlessera added this to the 0.45.0 milestone Nov 17, 2026
@schlessera schlessera added type: fix Something is broken and must behave the way it is documented. area: backends AgentBackend implementations and the seam they share. labels Nov 17, 2026
@schlessera
schlessera marked this pull request as ready for review November 17, 2026 07:55
@schlessera
schlessera merged commit 49b6e7c into main Nov 17, 2026
26 of 27 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area: backends AgentBackend implementations and the seam they share. type: fix Something is broken and must behave the way it is documented.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

backends: a pi duplicate-resume busy refusal settles before the losing worker's teardown

1 participant