Repository navigation
fix(backend-pi): hand a refused turn-temp close to the reconciler only once its teardown ends (#3488) - #4148
Merged
Conversation
…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>
3 of 5 tasks
…-resume-close-race
schlessera
marked this pull request as ready for review
November 17, 2026 07:55
This was referenced Nov 17, 2026
Closed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 (insidecloseStorage). If the reconciler's first retry came due (CLEANUP_RETRY_BASE_MS, 1 s) while the release was still in flight, it made a secondcloseAsyncattempt before the busy refusal (or the turn) settled. That isPI_DUPLICATE_RESUME_LOSER_CLOSED_BEFORE_REFUSALwithExpected: 1, Received: 2. Supervision now exposesteardown(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
closeAsyncremained before the refusal settles: the abort itself, and the reconciler's timed retry. The abort callscloseTurnTemp()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'sreleased()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.tspins 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 withbwrap(the duplicate test runs real workers).Deterministic reproduction. Both tests replace the process reconciler with one whose
schedulerecords 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/maind97405216, new tests):Mutations (each restored):
abort()runs outsideteardown()PI_DUPLICATE_RESUME_LOSER_CLOSED_BEFORE_REFUSAL(Expected 1, Received 2); prompted test passesteardown()PI_PROMPTED_TEARDOWN_CLOSES_ONLY_IN_FOREGROUND(Expected 2, Received 3); duplicate test passesteardown()drops the held refusal instead of handing it offPI_DUPLICATE_RESUME_REFUSED_LOSER_HAS_RETRY_OWNERandPI_PROMPTED_REFUSAL_HAS_RETRY_OWNERfail (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):
origin/mainproduct code, unchanged testorigin/mainproduct code, new testsPI_DUPLICATE_RESUME_LOSER_CLOSED_BEFORE_REFUSALandPI_PROMPTED_TEARDOWN_CLOSES_ONLY_IN_FOREGROUNDReal timer, no injected schedule (unchanged test, loser's
released()held 1.2 s):origin/mainfailsPI_DUPLICATE_RESUME_LOSER_CLOSED_BEFORE_REFUSALExpected 1 Received 2; this head passes.Workspace:
bun run test packages/ui-backend-pigives 593 pass / 25 fail on this head and 592 pass / 25 fail onorigin/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-sessionandworker-session-unitstogether: 49 pass.bun run typecheckandbun run lintare clean.{"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
packages/ui-react/,packages/ui-kit/,packages/ui-server/orpackages/core/carries a recorded independent review of its head (bun scripts/review-receipt.ts check <pr>), or it touches none of them. (Touches none.)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.)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 4148at head0d310bc29: nopathcollisions. Closure-only reports through shared workspace dependencies, with #3056, #3059, #3069, #3581, #3622, #4002, #4036 and #4145. None of them editsworker-launch.ts,worker-session.tsor the two test files.Anything left open
Nothing.
🤖 Generated with Claude Code