Skip to content

The agent that grows with you

fix(desktop): bound reconnect awaits so a stuck IPC round-trip can't latch the UI frozen

chelsealong17f8e24c8167
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 9949831bf68..0bebb6eb4f6 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
@@ -471,6 +471,50 @@ describe('useGatewayBoot remote reconnect loop (real hook, fake socket)', () =>
expect($desktopBoot.get().error).toBeNull()
})
+ it('a getConnection() that hangs on reconnect does not permanently latch the backoff loop (#93454)', async () => {
+ // Repro: a remote gateway drops, the backoff loop kicks off a reconnect,
+ // and the IPC round-trip into main (desktop.getConnection) never settles
+ // — e.g. a wedged revalidation after a liveness-probe trip, even though
+ // the backend itself answers fine. Without an internal timeout on that
+ // await, `reconnecting` never clears and every later
+ // scheduleReconnect()/attemptReconnect() early-returns forever, so the UI
+ // stays stuck until the app is restarted.
+ const desktop = fakeDesktop()
+ const originalGetConnection = desktop.getConnection
+ let callCount = 0
+
+ desktop.getConnection = vi.fn((profile?: null | string) => {
+ callCount += 1
+
+ // The initial boot call succeeds; every reconnect attempt after the
+ // drop 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(callCount).toBe(1)
+
+ act(() => FakeWebSocket.instances[0].drop())
+ await advanceBackoff()
+
+ expect(callCount).toBe(2)
+ expect($gatewayState.get()).not.toBe('open')
+
+ // Advance past the internal reconnect-attempt timeout (20s) — the stalled
+ // await must reject so the `reconnecting` guard clears and the backoff
+ // loop schedules another attempt, instead of latching forever on the
+ // still-pending first hang.
+ await act(async () => {
+ await vi.advanceTimersByTimeAsync(20_000)
+ })
+ await advanceBackoff()
+
+ expect(callCount).toBeGreaterThanOrEqual(3)
+ })
+
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 c4953469e66..b970408c2a8 100644
--- a/apps/desktop/src/app/gateway/hooks/use-gateway-boot.ts
+++ b/apps/desktop/src/app/gateway/hooks/use-gateway-boot.ts
@@ -96,6 +96,35 @@ const BOOT_RETRY_MAX_ATTEMPTS = 5
// loop's 300ms: each attempt may rebuild an SSH master + remote dashboard.
const BOOT_RETRY_BASE_DELAY_MS = 2_000
+// desktop.getConnection() / resolveGatewayWsUrl() are IPC round-trips into the
+// main process with no timeout of their own (#93454). A remote backend that
+// looks alive to a fresh probe but leaves the main-process reconnect path
+// stuck (e.g. a wedged revalidation after a liveness-probe trip) hangs these
+// awaits forever. While either is pending, `reconnecting` never clears, so
+// scheduleReconnect()/attemptReconnect() early-return permanently and the
+// backoff loop is latched — the UI stays "reconnecting" until the app is
+// restarted even though the gateway is reachable again. Bound both so a stall
+// rejects instead, letting the existing catch/finally clear the guard and
+// resume backoff. gateway.connect() already has its own connect timeout.
+const RECONNECT_ATTEMPT_TIMEOUT_MS = 20_000
+
+function withTimeout<T>(promise: Promise<T>, ms: number, message: string): Promise<T> {
+ return new Promise<T>((resolve, reject) => {
+ const timer = setTimeout(() => reject(new Error(message)), ms)
+
+ promise.then(
+ value => {
+ clearTimeout(timer)
+ resolve(value)
+ },
+ err => {
+ clearTimeout(timer)
+ reject(err)
+ }
+ )
+ })
+}
+
/** Registry identity whose runtimes died with the primary connection. */
export function primaryRuntimeConnectionId(connection: Pick<HermesConnection, 'connectionId' | 'mode'>): null | string {
const connectionId = connection.connectionId?.trim()
@@ -240,7 +269,11 @@ export function useGatewayBoot({
// (same as boot/softSwitch). Passing $activeGatewayProfile would retarget
// this primary socket at a secondary profile's backend after a live swap.
// Secondaries reconnect via reconnectSecondaryGateways().
- const conn = await desktop.getConnection()
+ const conn = await withTimeout(
+ desktop.getConnection(),
+ RECONNECT_ATTEMPT_TIMEOUT_MS,
+ 'Timed out reconnecting to Hermes backend'
+ )
if (cancelled) {
return
@@ -261,7 +294,12 @@ export function useGatewayBoot({
// explicit auth rejection asks for sign-in; transport failures stay in
// this reconnect loop. For local/token gateways the URL carries a
// long-lived token and the re-mint is a cheap no-op.
- const wsUrl = await resolveGatewayWsUrl(desktop, conn)
+ const wsUrl = await withTimeout(
+ resolveGatewayWsUrl(desktop, conn),
+ RECONNECT_ATTEMPT_TIMEOUT_MS,
+ 'Timed out re-minting the gateway WebSocket URL'
+ )
+
await gateway.connect(wsUrl)
if (cancelled) {