Plan: a session that ends while it is being named starts no link

Closes: #848. Relates to: #839 (which introduced it), #840 (set aside until this is released). Branch: fix/no-link-after-shutdown. Status: Done (2026-11-17) — with the erratum below: the cause was another, and the fix that cured it is the second commit

Erratum (2026-11-17): the measurement this plan asked for refuted its cause. On the Windows box, 24 interleaved rounds left 9 (0.164.0), 8 (this plan’s check) and 7 (0.163.0) processes behind, and the check never fired: the fault is older than #839 and the quit does not fall in the naming wait there. It falls while the member’s attach is in flight: pi makes the ended session’s context throw, the attach’s own reads of it (memberClient’s `info, startMember’s log line) throw, and the client is left open. The fix added for that: the client keeps what it last read of the session, `startMember reads the directory before it waits and stops its client on any throw. The check of this plan stays, for its own window. Tests: two concurrency cases in hub-e2e.test.ts (a quit before the socket connects; a quit between the hub’s answer and the member’s reading of it).

Erratum (2026-11-17): measured on the Windows box with both commits, 24 interleaved rounds, 12 s bound: left behind 10 (0.164.0), 0 (the fix), 6 (0.163.0). Five of the fix’s runs attached after the quit and ended link_late, member_stopped; none logged restore_failed. This plan’s own check fired in none of the 48 runs that carried it, 24 in each measurement. Not run: an open-and-quit by hand, a one-shot that fails early.

Context

Found by the Windows seat on 0.164.0: with PI_STARTUP_BENCHMARK=1 pi quits right after starting, and in 4 of 22 such starts the process never exited. Its diagnostics for a hung run: no session_shutdown or member_stopped event, member_attached at 980-990 ms (after the quit), then pi_remote.restore_failed "This extension ctx is stale". A good run attaches at about 790 ms and shuts down at about 960 ms. Not established by a control beside it that 0.163.0 never did this; none was seen in five starts there.

By reading packages/pi-remote/index.ts and lifecycle.ts:

  • #839 made the session_start handler await nameAgent(…) before restoreLink. Before, restoreLink was called in the same tick as the start.

  • session_shutdown bumps state.generation ("a start in flight is late") and returns early when state.link is undefined.

  • startLinkOnce reads state.generation when it begins and drops a link that comes back after a bump. If the shutdown lands during the awaited naming, the restore begins afterwards, reads the generation already bumped, sees nothing late, attaches as a member and installs the link. Nothing then stops it: the session is over, and the member’s socket keeps node alive.

Design

D1. RemoteState gains shutdowns: number (state.ts; 0 in initialState), incremented by the session_shutdown handler beside its state.generation++ and by nothing else. state.generation cannot serve: stopLink bumps it too, with no link up (lifecycle.ts, the branch for a stop while nothing runs), so a /remote-control stop typed during the wait would read as the session’s end.

The session_start handler in sessionHooks reads state.shutdowns before await nameAgent(…) and, if it differs afterwards, logs state.log("pi_remote.start_abandoned", { cwd: ctx.cwd }) and returns: no restoreLink, no startMessaging. nameAgent has by then set state.agent; that is left, since the session is over and the next session_start sets it again.

D2. Nothing else changes. `restoreLink’s own guard for a link late by a socket round trip stays as it is.

Scope

In: packages/pi-remote/index.ts, packages/pi-remote/state.ts, packages/pi-remote/test/extension.test.ts, docs/modules/ROOT/pages/remote.adoc (one sentence where the name’s wait is described), CHANGELOG.adoc.

Out: restoreLink leaving a link up when its own notice throws on a stale context (the second half of what the diagnostics show; with D1 the restore does not begin, so it cannot be reached this way). Filed as #849, with a stop typed during the naming wait, which this change also leaves as it is.

Risks

Risk Mode Mitigation

A session that did not end is taken for one that did, and gets no link and no messages

fail-closed

Only the shutdown handler increments shutdowns. Tests: a start with no shutdown restores the link and hands over what waits; a /remote-control stop run while the name is awaited does not abandon the start (the session’s messages are still handed over).

The same race exists through another await and still leaves a process

silent

The Windows seat reruns its 22 quick starts on the fix, with the released build beside it; a hang on the fix is reported before merge.

A reload (shutdown then start in one process) is abandoned by the old start’s check and the new start is skipped

fail-closed

Each session_start reads shutdowns at its own beginning, after the reload’s shutdown; test: a shutdown followed by a new start names the agent and restores the link.

Tests

Concurrency (the race itself, extension.test.ts): a session start whose workspace does not answer mounts until released; session_shutdown fired while it waits; then released. Asserted: no socket was opened, state.link is undefined, pi_remote.start_abandoned is logged, and no message was handed to the session. The same case without the shutdown opens the link. The same case with /remote-control stop run in place of the shutdown: not abandoned, the waiting message is handed over, pi_remote.start_abandoned is not logged.

Regression: a shutdown followed by a fresh session_start on the same instance (a reload) restores the link.

Measured before merge, prediction on file first, by the Windows seat on panther: quick starts that quit at once, the released 0.164.0 and the fix interleaved, at least 22 each, counted as exited or left behind; and the diagnostics of any that hang.

Delivery

  1. One branch, one commit: the check, its tests, the sentence in remote.adoc.

  2. CHANGELOG, under Unreleased, Fixed: a pi that quit within about a second of starting could be left running (0.164.0).

  3. node tools/task.mjs validate, full consumer-sim, the Windows measurement; MR closing the issue, squash off, the merge SHA on the issue.

  4. Released at once as 0.164.1: @gadhs/pi-remote and @gadhs/pi move.

Edit this page · latest