Skip to content

The agent that grows with you

fix(desktop): bound getConnection() on the boot and soft-switch paths (#93454)

nftpoetrist31f3de1f0605
diff --git a/apps/desktop/src/app/gateway/hooks/use-gateway-boot.test.tsx b/apps/desktop/src/app/gateway/hooks/use-gateway-boot.test.tsx
index c0a4d4701f6..4b7cc7bbf0d 100644
--- a/apps/desktop/src/app/gateway/hooks/use-gateway-boot.test.tsx
+++ b/apps/desktop/src/app/gateway/hooks/use-gateway-boot.test.tsx
@@ -1138,6 +1138,69 @@ describe('useGatewayBoot remote reconnect loop (real hook, fake socket)', () =>
expect($gatewayState.get()).toBe('open')
})
+ it('a getConnection() that hangs on INITIAL boot rejects on its own after the reconnect-attempt timeout, not only when main eventually gives up (#93454)', async () => {
+ // boot()'s getConnection() had no bound of its own — only main's own
+ // eventual timeout (e.g. waitForHermes, ~45s) ever settled it. A wedge
+ // that main never resolves (not even a rejection) must not hang
+ // "Starting Hermes…" forever; the renderer needs to own its own bound
+ // here too, same as attemptReconnect() and softSwitch().
+ const desktop = fakeDesktop()
+ desktop.getConnection = vi.fn(() => new Promise(() => undefined))
+ ;(window as { hermesDesktop?: unknown }).hermesDesktop = desktop
+
+ render(<Harness />)
+ await flushAsync()
+
+ expect($desktopBoot.get().error).toBeNull()
+
+ // Advance past the internal reconnect-attempt timeout (20s) — the
+ // stalled await must reject on its own so boot()'s catch runs instead of
+ // waiting indefinitely on main.
+ await act(async () => {
+ await vi.advanceTimersByTimeAsync(20_000)
+ })
+
+ expect($desktopBoot.get().error).toBeTruthy()
+ })
+
+ it('softSwitch(): a getConnection() that hangs on a connection-apply switch does not latch $gatewaySwitching forever (#93454)', async () => {
+ // Repro: main applies a new connection (onConnectionApplied), softSwitch()
+ // re-dials via getConnection(), and the IPC round-trip wedges. Without an
+ // internal timeout, the try block never settles, so the `finally` that
+ // clears $gatewaySwitching never runs — the switch UI stays frozen until
+ // the app is restarted.
+ const desktop = fakeDesktop()
+ const originalGetConnection = desktop.getConnection
+ let callCount = 0
+
+ desktop.getConnection = vi.fn((profile?: null | string) => {
+ callCount += 1
+
+ // Initial boot succeeds; the switch triggered below hangs indefinitely.
+ return callCount === 1 ? originalGetConnection(profile) : new Promise(() => undefined)
+ })
+ ;(window as { hermesDesktop?: unknown }).hermesDesktop = desktop
+
+ render(<Harness />)
+ await flushAsync()
+ expect($gatewayState.get()).toBe('open')
+ expect(connectionApplied).not.toBeNull()
+
+ act(() => connectionApplied?.())
+ await flushAsync()
+
+ expect($gatewaySwitching.get()).toBe(true)
+
+ // Advance past the internal reconnect-attempt timeout (20s) — the
+ // stalled await must reject so the `finally` clears $gatewaySwitching
+ // instead of latching the switch UI frozen forever.
+ await act(async () => {
+ await vi.advanceTimersByTimeAsync(20_000)
+ })
+
+ expect($gatewaySwitching.get()).toBe(false)
+ })
+
it('rebinds Bot tabs owned by the restarted primary without touching another gateway', async () => {
render(<Harness />)
await flushAsync()
diff --git a/apps/desktop/src/app/gateway/hooks/use-gateway-boot.ts b/apps/desktop/src/app/gateway/hooks/use-gateway-boot.ts
index 14061f4098c..1a7aa6edfcc 100644
--- a/apps/desktop/src/app/gateway/hooks/use-gateway-boot.ts
+++ b/apps/desktop/src/app/gateway/hooks/use-gateway-boot.ts
@@ -540,7 +540,14 @@ export function useGatewayBoot({
// Same override rule as boot(): a profile-pinned helper window stays
// on its pinned profile's backend across a soft switch.
- const conn = await desktop.getConnection(windowProfileOverride() ?? undefined)
+ // Bounded for the same reason as attemptReconnect() (#93454): a wedged
+ // main-process round-trip must not latch $gatewaySwitching stuck —
+ // the `finally` below only runs once this promise settles.
+ const conn = await withTimeout(
+ desktop.getConnection(windowProfileOverride() ?? undefined),
+ RECONNECT_ATTEMPT_TIMEOUT_MS,
+ 'Timed out reconnecting to Hermes backend'
+ )
if (!ownsSwitch()) {
return
@@ -887,7 +894,13 @@ export function useGatewayBoot({
// A profile-pinned helper window (the HUD) dials its target profile's
// backend directly — ensureBackend spawns/reuses it from the pool.
// Everything else keeps dialing the primary.
- const conn = await desktop.getConnection(windowProfileOverride() ?? undefined)
+ // Bounded like the reconnect path (#93454): a wedged main-process
+ // round-trip must not hang "Starting Hermes…" forever.
+ const conn = await withTimeout(
+ desktop.getConnection(windowProfileOverride() ?? undefined),
+ RECONNECT_ATTEMPT_TIMEOUT_MS,
+ 'Timed out connecting to Hermes backend'
+ )
if (cancelled) {
return