GeezerVison is showing github.com — fix(desktop): serialize background service connects through one state machine by Hona · Pull Request #61748 · anomalyco/opencode
Conversation
Hona
commented
Nov 4, 2026
•
edited by opencodejr
Bot
Loading
Uh oh!
There was an error while loading. Please reload this page.
The desktop connection to the background service is now one explicit state machine. Windows that reconnect together share one Service.ensure, a probed service is replaced when its verification finds another, a failed first connect is retried instead of replayed, and every window moves to a new service origin when main adopts one.
Bugs
- Racing reconnects. Two windows that lost the service each called
AppReconnectService, which ran two Service.ensure calls that raced on SidecarCredentials.set. A window that had not reconnected kept its old origin; wireRendererHeaders adds Authorization only for the current origin, so its requests failed with HTTP 401.
- Discarded replacement. A cold launch adopted the probed service
A and ran Service.ensure in the background with void runEnsure().catch(() => undefined). If A had exited and ensure started B, B was dropped: credentials, plugins and every window stayed on dead A.
- Sticky first failure.
BackgroundServiceState latched the first connect's Exit.fail in a Ref. After a transient failure (a port race, a config error the user then fixed), every new window, reload and Dock activate received the same error without retrying.
Machine
packages/desktop/src/main/service/background-service-machine.ts
type State =
| { status: "connecting"; attempt: number; waiters: Reply[] }
| { status: "ready"; attempt: number; data: Data }
| { status: "reconnecting"; attempt: number; data: Data; waiters: Reply[]; joiners: Reply[] }
| { status: "failed"; attempt: number; error: unknown; last: Data | undefined }
| { status: "disposed" }
type Command =
| { type: "connect"; attempt: number; mode: "initial" | "reconnect" }
| { type: "verify"; attempt: number }
| { type: "publish"; data: Data }
| { type: "resolve" | "reject"; replies: Reply[]; ... }
stateDiagram-v2
[*] --> connecting: connect(1, initial)
connecting --> ready: probe.succeeded / publish, resolve, verify
connecting --> ready: connect.succeeded / publish, resolve
connecting --> failed: connect.failed / reject
ready --> ready: connection.requested / resolve
ready --> ready: verify.succeeded (other service) / publish
ready --> reconnecting: reconnect.requested / connect(n+1, reconnect)
reconnecting --> ready: connect.succeeded / publish if changed, resolve
reconnecting --> failed: connect.failed / resolve joiners with last, reject waiters
failed --> connecting: any request, no last / connect(n+1, initial)
failed --> reconnecting: any request, with last / connect(n+1, reconnect)
connecting --> disposed: disposed / reject waiters
reconnecting --> disposed: disposed / reject waiters
Loading
- Requests during
connecting or reconnecting join the attempt in flight (single-flight).
- During
reconnecting, waiters asked to reconnect and joiners asked for the connection (a window opened or reloaded). A failed reconnect answers joiners with the last service and rejects only the waiters; failed keeps that service as last.
- Every result carries its attempt; a result of a superseded attempt (a late verify after a reconnect started) is ignored.
waiter.cancelled removes one caller. The renderer's reconnectService(signal) is now cancellable, so an aborted reconnect or a closed window interrupts its wait in main and leaves the attempt running for the others.
- The driver (
make) forks each attempt into a scoped FiberSet and reports every exit, including defects from DesktopCli.resolve, as an event, so no caller can hang. It normalizes the squashed cause to an Error there, so the machine carries only Error values.
Every window follows the active origin
sequenceDiagram
participant W1 as Window 1
participant M as Main machine
participant W2 as Window 2
W1->>M: AppReconnectService
M->>M: Service.ensure -> B
M-->>W1: SidecarChanged(B) + reply B
M-->>W2: SidecarChanged(B)
W2->>W2: transport.update(B), resync stream
Loading
publish sets SidecarCredentials and broadcasts the new SidecarChanged desktop event to every main window.
desktop-app.tsx keeps one stable ServerConnection.Sidecar whose http getter follows the latest origin. client.tsx now moves a sidecar server's transport in place, as it already did for plugin servers.
Deleted
background-service-state.ts (Ref<Exit> latch and Deferred first-gate) and its test.
- The fire-and-forget
runEnsure() after a probe and the direct SidecarCredentials.set inside connect.
- The module-global
sidecarProbe() promise, replaced by one-shot takeSidecarProbe(), so a retry never adopts a service that may have exited since boot.
- The renderer's per-window
mutate: setSidecar path, which rebuilt the sidecar connection object.
Product calls
- A failed verification keeps the probed service. It answered the probe, and
dev kept it too; a window that then loses it reconnects.
- A failed first connect is retried by the next request, including the first window's own
AppAwaitInitialization if it arrives after the failure. A reload or a new window therefore recovers from a fixed config error.
- A window that opens or reloads during a reconnect waits for it, and keeps
dev's outcome if it fails. It gets the last service, as it would have on dev, and gets the error only when no service was ever reached. A window that opens after a failed reconnect starts a retry and is answered the same way.
- Stale CLI stages are cleaned only after
Service.ensure settles which server runs, not right after adopting a probed service it may replace.
Deviations from the design
ready has no verifying flag: attempt fencing already rejects a stale verify, and no consumer reads the flag.
- No
selectCredentials, isServiceReady or isServiceConnecting selectors and no SidecarCredentials.clear(): nothing reads them; SidecarCredentials mirrors publish.
verify.failed leaves ready unchanged (see product calls) instead of entering failed.
Visual
<svg xmlns="http://www.w3.org/2000/svg" width="240" height="72" viewBox="0 0 240 72" fill="none" stroke="currentColor">
<style>
.pulse{animation:pulse 3s infinite}
.w2{animation:move 3s infinite}
@keyframes pulse{0%,20%{opacity:.2}35%,100%{opacity:.9}}
@keyframes move{0%,45%{transform:translateY(0)}60%,100%{transform:translateY(-24px)}}
@media (prefers-reduced-motion:reduce){.pulse,.w2{animation:none}}
</style>
<rect x="12" y="10" width="44" height="20" stroke-opacity=".7"/>
<rect x="12" y="42" width="44" height="20" stroke-opacity=".7"/>
<rect x="98" y="26" width="44" height="20" stroke-opacity=".9"/>
<line x1="56" y1="20" x2="98" y2="36" stroke-opacity=".5"/>
<line class="pulse" x1="56" y1="52" x2="98" y2="36" stroke-opacity=".5"/>
<rect x="184" y="10" width="44" height="20" stroke-opacity=".25"/>
<rect x="184" y="42" width="44" height="20" stroke-opacity=".9"/>
<line x1="142" y1="36" x2="184" y2="52" stroke-opacity=".7"/>
<circle class="w2" cx="206" cy="52" r="3" fill="currentColor" stroke="none" fill-opacity=".8"/>
</svg>
Added by the opencode bot (AI-generated).
Related: #60865
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.
An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The change looks sound. I couldn't verify it end to end: the bugs need the Electron desktop app with several windows and a service being replaced, which this Linux sandbox can't run. The new machine and renderer tests pass (18/18). The desktop typecheck script fails with Script not found "check" because it calls bun check; tsc -b passes.
I checked the state transitions against the description. Results from a superseded attempt are ignored, every waiter is settled on disposal, the sidecar keeps the same key ("sidecar") when its origin moves, and auth still follows SidecarCredentials through publish. Dropping !isolated from the probe path is safe, because startSidecarProbe already skips unpackaged builds.
Public API: unchanged. SidecarChanged and the optional signal on reconnectService are internal to the desktop app's main↔renderer messaging.
One behaviour change worth confirming is intended: a window that opens or reloads during a reconnect now waits for that reconnect. If the reconnect fails, the window gets the startup error instead of the last service, which on dev it would still have received.
Related: #60865
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The new commit looks sound. Failures from the background service now become an Error at the point where the driver reports them, and the startup error check in initialization.ts behaves as before. The machine and initialization tests pass (18/18) and tsc -b passes. I still couldn't run the desktop app here. The public API is unchanged. Nothing new to raise.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
|
Kept dev's outcome for this case in 86270fa. A window that opens or reloads during a reconnect still joins it, but reconnecting now tracks it as a joiner: if the reconnect fails, joiners get the last service and only the windows that asked to reconnect get the error. With no previous service, every caller still gets the error. New transition tests: a window that opens during a reconnect that fails still gets the last service (which also covers a retry that fails again), without a previous service, every caller of a failed attempt gets the error and a window that closes while it waits for a reconnect is not answered. The PR body's product calls are updated.
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My earlier point is addressed. A window that opens or reloads during a reconnect now gets the last service if the reconnect fails, as it did on dev, and gets the error only when no service was ever reached. Disposal and cancelling a wait cover both kinds of waiting caller, and the new transition tests are small and clear. The machine tests pass (44/44) and tsc -b passes; I still couldn't run the desktop app here. The public API is unchanged. Nothing new to raise.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
This was referenced Nov 7, 2026
Uh oh!
There was an error while loading. Please reload this page.
Assignees
No one assigned
Milestone
No milestone
Development
Successfully merging this pull request may close these issues.
Uh oh!
There was an error while loading. Please reload this page.
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.
|