Fixes #39418.
The connection supervisor keeps a connection alive with one loop: try to connect, wait, try again. That loop ran as a single background fiber with nothing around it. A defect or a stray interruption anywhere in it ended the fiber silently. The app kept showing its last state, and Retry did nothing, because nothing was left to hear it.
I found this by reading the supervisor during an experiment on its retry loop, not from a user report. The code does have a route to it. rpc/session.ts fails a session's closed with whatever cause ended the config subscription, unless that cause holds interruptions only. A cause that holds a failure and an interruption gets through, and exitUnlessInterrupted turned any cause holding an interruption into an interruption of the loop.
What changes#
- An attempt reports its own unexpected failures.
runAttempt uses Effect.exit where it used exitUnlessInterrupted, and that helper is gone. A stray interruption becomes a failed attempt that keeps what the attempt knows: whether the session was live and whether it was stable. A typed error mixed with an interruption keeps its typed error, so a blocked host stays blocked. Effect does not run the continuation of a fiber that is itself interrupted, so a real shutdown still ends the loop.
- A defect elsewhere in an attempt is caught around the attempt. It is logged and counts as a failed attempt on the normal backoff ladder. If the session had gone live, it counts as a lost session. The catch does not know how long the session lived, so it never resets the ladder.
- Closing an attempt's scope is separate from the attempt. The scope is opened and closed with
Effect.acquireUseRelease. A defect from closing it is logged as a failed release, and the attempt's outcome stands. If it replaced the outcome, a release that throws after a requested disconnect would show backoff, and a Retry would wait out the ladder's current delay.
- The generation number advances where the session is published as connected. The counter lives outside the loop. No lost outcome and no restart gives two sessions the same number.
- A defect in the loop outside an attempt is the last resort. It comes from the loop's own bookkeeping, or from a logger that throws inside the handlers above. It is logged, the state shows backoff, offline or available, and the loop restarts after the longest retry delay or the next signal: Retry, connect, disconnect, a network change or a wakeup. The wait is the ladder's ceiling because a restarted loop has lost its retry counters, so a shorter wait would let a repeating defect reconnect with no backoff. This layer is coarse on purpose. The restarted loop has also lost the last failure, and it retries a host that was blocked.
- The shutdown finalizer is registered before the background fibers. Closing a supervisor then interrupts them before the signal queue shuts down. Otherwise the loop would see the closed queue as an unexpected interruption and log an error on every shutdown.
The issue proposed only the restart where the loop is forked. That alone would send every attempt defect, the issue's own example included, through the five-minute wait and reset the retry ladder. So attempts handle their own failures, and the restart is the last resort.
New log lines#
One error line per unexpected failure: Connection <attempt | release | loop> failed with an unexpected <defect | interruption>. It carries the environment and a reason count. For a defect it also carries the first defect's name and stack, through the existing safe-attributes helper. It never carries the error's message.
Main had only the attempt-defect line, and only while connecting. New here: a defect in the connected phase, a defect beside a typed failure in the same cause, every interruption line, and the release and loop lines. A release defect while a supervisor shuts down is logged too.
Proof#
The real route, in the running app#
reconnect-survives-config-stream-failing-while-interrupted.e2e.ts is part of this PR. It opens a thread, then hands the app one server frame that ends the config subscription with a typed failure and an interruption in the same cause. The frame is injected from the page. I found no way to make a real server send it. Everything after the frame is the app's own code.
$ vp run scenarios:prove scripts/scenarios/reconnect-survives-config-stream-failing-while-interrupted.e2e.ts --base origin/main
Proven: the scenario fails on base 57db750b8f and passes on head 89017c15a0.
base 57db750b8f
ASSERTION_FAILED: expect.toBeVisible failed
locator: getByText("The command printed one JSON line.").nth(1)
observed: no node (match count 0)
Five seconds after the frame, the scenario types a message and presses Enter. These are three seconds later:
Base 57db750b8f | Head 89017c15a0 |
|---|
 |  |
| No banner and no error. The message stays in the composer. | The app reconnected. The message was sent and answered. |
Both runs side by side:
https://github.com/user-attachments/assets/273927be-6775-4224-bb48-bca5c50882e1
The issue's own example, in the running app#
A second scenario does what the issue describes. It closes the app's socket from the page, as a network drop would, and makes the next read of navigator.onLine throw once. No real browser throws there, so this scenario is not part of the PR. Its code is in #39418.
scenarios:prove gives the same verdict: it fails on base 57db750b8f and passes on head 89017c15a0. Ten seconds after the drop, the scenario presses Retry now if the button is there:
Base 57db750b8f | Head 89017c15a0 |
|---|
 |  |
| The banner still says "is reconnecting" five seconds after Retry now. | Reconnected by itself. There was no Retry now button to press, and a new message was answered. |
https://github.com/user-attachments/assets/ea13bd87-c335-4d0a-bffd-5f12fc1bb673
On head the page logged one line: ERROR: Connection attempt failed with an unexpected defect. On base it logged nothing. A control run of the same scenario with no injected throw reconnected on main's supervisor, so the stuck state comes from the defect and not from the dropped socket.
Tests#
The new and changed cases, run with main's supervisor.ts and with this branch:
$ vp test run src/connection/supervisor.test.ts
base 57db750b8f supervisor.ts
× retries after the attempt's network-status read dies
× retries after an attempt interrupts itself
× reconnects soon after a stable session is interrupted
× reconnects soon after a stable session fails and dies while interrupted
× reconnects soon after a stable session fails while interrupted
× reconnects soon after a stable session closes while its release defects
× does not reuse a live session's generation when its attempt defects after its session went live
× does not reuse a live session's generation when logging its release defect dies
× keeps a released session's outcome
× restarts the loop after a defect outside an attempt
× keeps a blocked host that fails while interrupted idle until an external signal requests another attempt
Tests 11 failed | 79 passed (90)
89017c15a0
Tests 90 passed (90)
All eleven fail on base because the loop is dead and the state never changes. The two "stops attempting when its scope closes" cases pass on base on purpose. They guard the finalizer order, and they fail if the finalizer is registered after the fibers again.
vp test run src/connection passes, 214 of 214. vp run check:changed passes.
Not covered#
- The supervisor's three other background fibers still end silently on a defect: connectivity changes, wakeups and better-route checks. #39419 describes that, and #39431 fixes it.
- A failure inside the restart handler itself still ends the loop, after one log line.
- A cleanup defect in an establishment that lost its race is discarded with the cancelled fiber and not logged, as on main. A release that throws when Retry cancels a synchronizing attempt is one example.
Two open PRs, #30835 and #34963, edit the exitUnlessInterrupted call this PR replaces. Whichever lands second needs a small rebase there.
Implemented by Claude Fable 5.2 and Claude Opus 5.5 in Claude Code (T3 Code). Reviewed by Claude Opus 5.5 and GPT-6.1 Sol.
No actionable comments were generated in the recent review.
Walkthrough
The connection supervisor now captures failure causes, logs unexpected failures, retains connection generations across loop restarts, and restarts its loop after unexpected failures. Tests cover attempt, session, and release failures, blocked retries, and shutdown. An end-to-end scenario checks message delivery after a config-stream failure mixed with interruption.
Changes: Connection supervisor recovery
packages/client-runtime/src/connection/supervisor.ts,packages/client-runtime/src/connection/supervisor.test.tspackages/client-runtime/src/connection/supervisor.ts,packages/client-runtime/src/connection/supervisor.test.tspackages/client-runtime/src/connection/supervisor.ts,packages/client-runtime/src/connection/supervisor.test.ts,scripts/scenarios/reconnect-survives-config-stream-failing-while-interrupted.e2e.tsEstimated code review effort: 3 (Moderate) | ~25 minutes
Change: Bug fix · Severity of issue fixed: Low
Sequence Diagram(s)#
Possibly related issues
Possibly related PRs
Suggested labels:
bugfixSuggested reviewers:
t3dotgg,juliusmarmingeMerge Risk:
Low · up to
fda67This change keeps the client reconnecting after unexpected connection failures. A few minor open items remain: a throwing logger could still stop recovery, config error text could appear in telemetry spans, and one test has a cleanup gap and fixed waits. These are low-risk follow-ups for the owner to consider.
Pre-merge checks:
4
#39418has three coding requirements. [#39418] (1) Survive unexpected defects and interruptions in attempts: the whole-PR summary states that runAttempt uses Effect.exit in place…#39418or to a necessary part of its fix: Effec…Full details: Title check
Explanation
The title names the area (client-runtime) and the main behavior change: an unexpected failure no longer stops the reconnect loop permanently. The title follows conventional commit style and matches the supervisor, retry loop, and attempt-level handling described in the PR objectives and the raw summary.
Full details: Linked Issues check
Explanation
Directly linked issue
#39418has three coding requirements. [#39418] (1) Survive unexpected defects and interruptions in attempts: the whole-PR summary states that runAttempt uses Effect.exit in place of exitUnlessInterrupted. A stray interruption becomes a failed attempt, a typed error mixed with an interruption keeps its typed error, and a defect around the attempt is caught and counted as a failed attempt. Real fiber interruption still ends the loop. The read of supervisor.ts at head confirms failureFromCause and unexpectedFailure, but the runAttempt section was truncated and was not read directly. [#39418] (2) Keep reconnecting after these failures: the unit test cases named in the summary cover retries after a network-status read defect, after an attempt interruption, and after a stable session fails while interrupted. The e2e scenario reconnect-survives-config-stream-failing-while-interrupted.e2e.ts covers the config-stream case. The loop-level restart covers defects outside an attempt. [#39418] (3) Log unexpected defects: the summary describes one error line per unexpected failure for attempt, release, and loop, and the tests capture log messages. Limits: the incremental diff from the previous reviewed head 89017c15 to head fda679a0 could not be read (changed blobs missing), so changes between those commits are unknown. The test results in the PR description were reported for head 89017c15, not fda679a0, and were not re-run. The three other background fibers in#39419are excluded as contextual scope, not as requirements of#39418.Full details: Out of Scope Changes check
Explanation
The whole-PR summary shows changes limited to the connection supervisor (supervisor.ts and supervisor.test.ts) and one e2e scenario. Each change maps to
#39418or to a necessary part of its fix: Effect.exit handling and the attempt-level defect catch (requirement 1); the acquireUseRelease scope handling with release-failure logging, which keeps a requested disconnect outcome (fix support); the generation counter kept outside the loop (fix support across restarts); the loop-level restart (last-resort recovery); and moving the shutdown finalizer ahead of the background fibers (prevents a spurious unexpected-interruption log on shutdown). The e2e scenario and the test harness changes support verification. The three other background fibers are explicitly excluded in the PR and deferred to#39419. No unrelated change was found. Limit: the incremental diff from 89017c15 to fda679a0 could not be read, so the out-of-scope check covers the summary of the whole PR, not the exact changes since the previous review.Full details: Description check
Explanation
The description is thorough and follows the repository template. It has a Problem section that explains the defect and how the supervisor loop dies silently. It has a Change section that lists the changes and the reason for each. It has a Scope section that links issue
#39418and explains why the change fixes a bug and does not add a feature. It has a Verification section that gives focused test results on base and head, an e2e proof with before/after screenshots and a recording, and lists what was not covered. It also names the models that did the work, as the template requests. The template's Title guidance is not part of this check. The description is complete, so this check passes.Finishing Touches
Generate unit tests (beta)
37109565-15ad-4223-a1ac-5bd3f33115e1Reviewing files that changed from the base of the PR and between 89017c15a0af177645fee044226d9d794b028051 and fda679a036720f915395f0f69b1edd5b35e4d415.
packages/client-runtime/src/connection/supervisor.test.tsIncluded review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 8 remain after this review.