Skip to content

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

Description

@schlessera

Context

The #2918 spike found the losing branch of a concurrent duplicate resume reachable with two real workers (report: docs/investigations/duplicate-pi-worker-acquisition.md, added with #2918's PR).

Production pi backends never retain sessions (createSessionPool(runtime, Boolean(options.sessionFactory)), packages/ui-backend-pi/src/backend.ts:117), so every resume opens a worker. Two concurrent startTurn calls on the public createPiBackend() backend for the same non-resident sessionId both await runtime.openSession. The one that returns second finds the first registered, calls synchronous disposeSession(opened.session) and throws BackendBusyError (packages/ui-backend-pi/src/session-pool.ts:85-95). dispose() only ends stdin and requests SIGKILL (packages/ui-backend-pi/src/worker-session.ts:776). The turn-temp close then runs detached on exit, and its refusal is dropped (packages/ui-backend-pi/src/worker-session.ts:322). Nothing calls the launcher's released() for the losing worker.

Measured at main adf371ffa with the keyless real-worker test packages/ui-backend-pi/tests/duplicate-acquisition.test.ts: when the loser's BackendBusyError settled, its worker had not exited, its turn temp had not been closed (0 calls) and its stdio release had not been awaited (0 calls). Both workers' turn-temp reservations were still charged on the process ledger. The detached close freed it later.

The #2863 lifecycle ruling and its record ("Turn completion includes worker teardown", docs/decisions/governed-writable-workers.md) require completion to wait for worker exit, the turn-temp close and the bounded released() wait. #2880 applied this to cancelled acquisitions through cancelAcquiredSession (packages/ui-backend-pi/src/session-pool.ts:159-166), which awaits the session's shared inactive abort() (packages/ui-backend-pi/src/worker-session.ts:758-774). The duplicate-loser branch still uses the synchronous path.

The ui-server callers cannot reach this branch. TurnCoordinator admits one in-flight startTurn per session (see the report). The exposure is a direct consumer of the public backend.

Out of scope

  • The new-session duplicate branch (packages/ui-backend-pi/src/session-pool.ts:101-117). Production identities are randomUUID() (packages/ui-backend-pi/src/worker-session.ts:112), so only injected sessionFactory doubles reach it. It may share the helper, but it needs no production proof.
  • Public synchronous dispose(), worker protocols, the BackendBusyError and result shapes, and the five-second release bound.
  • Changing the ui-server coordinator, or redesigning the session pool or retention.

Scope

In the resumed duplicate branch, the pool owns the losing worker until its awaited inactive abort() settles, then disposes it. This matches cancelAcquiredSession. Take the busy-or-reuse decision, and claim a reused entry, synchronously before any await. After the wait, re-raise the original outcome: BackendBusyError for a running winner, or the reused entry. A loser cleanup refusal must not replace that outcome. Report it the way the backend reports other preserved cleanup refusals (backend.log warn); it must not be silently dropped.

Acceptance criteria

  • Failing first on main: at the moment its BackendBusyError settles, the loser in duplicate-acquisition.test.ts has exited, has closed its turn temp exactly once, and has had its stdio released() awaited exactly once. The ledger then charges only the winner's reservation.
  • The winner keeps its normal success result and is never signalled by the loser's teardown. The loser emits no frames and never prompts. The existing assertions in that test stay green.
  • If the loser's turn-temp close is refused, the caller still receives BackendBusyError, the reservation stays charged, and a retry through the shared abort() can release it (as in early-cancel-lifecycle.test.ts).
  • Mutation receipts: making the loser path synchronous again fails the new exit/close/release assertions. Dropping the busy re-raise fails PI_DUPLICATE_RESUME_LOSER_REFUSES_BUSY.

Verification

Run bun run test packages/ui-backend-pi/tests/duplicate-acquisition.test.ts packages/ui-backend-pi/tests/early-cancel-lifecycle.test.ts plus the packages/ui-backend-pi workspace tests on a Linux host with bwrap, keyless. Record the failing-first and mutation output in the PR.

Contract impact

None. Busy refusal, result shapes and synchronous disposal are unchanged. The refusal only settles later, within the existing bounds.

Activity

  1. added
    type: fix
    area: backends
    priority: p2
    agent-ready
    on Nov 3, 2026
  2. self-assigned this
    on Nov 3, 2026
  3. added this to the 0.45.0 milestone on Nov 3, 2026
  4. schlessera commented on Nov 3, 2026

    @schlessera
    OwnerAuthor

    Draft PR #3501 (head 2d80ed940) implements this. Local failing-first, mutation and check:pr receipts are in the PR body. Hosted candidate proof has not been requested yet. Milestone 0.45.0 is set on the issue and the PR. The claim stays in place while the PR is live.

  5. schlessera commented on Nov 3, 2026

    @schlessera
    OwnerAuthor

    The duplicate-resume test is still intermittent after #3501. packages/ui-backend-pi/tests/duplicate-acquisition.test.ts:135 fails PI_DUPLICATE_RESUME_LOSER_CLOSED_BEFORE_REFUSAL with Expected: 1, Received: 2 (the loser's worker is closed twice):

    • hosted CI run 41718017211 (unit (3)) on an epic candidate whose Pi changes only add a roster capability
    • locally on a clean main 29799ebcd: 1 of 6 runs failed with the same assertion (the other 5 passed)

    So the loser's teardown can run its close path twice before the refusal settles.

  6. schlessera commented on Nov 17, 2026

    @schlessera
    OwnerAuthor

    Ownership reconciliation (0.45.0 orchestration, 2026-11-17): this issue has been assigned with no linked activity since 2026-11-04. Releasing the stale assignment so a new worker can take it.

  7. schlessera commented on Nov 17, 2026

    @schlessera
    OwnerAuthor

    Claimed for implementation. Claim token: claim-45905ebb. Branch: fix/3488-pi-duplicate-resume-close-race.

    closure-only collisions (#3056, #3059, #3069, #3581, #3622, #4002, #4036); no path collisions

  8. schlessera commented on Nov 17, 2026

    @schlessera
    OwnerAuthor

    Merged in #4148 as 49b6e7c48 through the integration lease (orchestrator merge queue for 0.45.0). Candidate confirmation passed on the exact head; the squash's own main run carries the post-merge heavy categories.

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

Metadata

Metadata

Assignees

Labels

agent-readyarea: backendspriority: p2type: fix

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions