Skip to content

The agent that grows with you

fix(desktop): bound the revalidateConnection() await too (#93454)

chelsealong5ef205d8558b
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 0bebb6eb4f6..4b4a044f469 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
@@ -515,6 +515,48 @@ describe('useGatewayBoot remote reconnect loop (real hook, fake socket)', () =>
expect(callCount).toBeGreaterThanOrEqual(3)
})
+ it('a revalidateConnection() that hangs on reconnect does not permanently latch the backoff loop (#93454)', async () => {
+ // Same failure mode as the getConnection() repro above, but for the OTHER
+ // unbounded IPC await in the same try block: a wedged revalidation after a
+ // liveness-probe trip (the PR's own named trigger) must also unlatch.
+ const desktop = fakeDesktop()
+ let revalidateCallCount = 0
+
+ desktop.revalidateConnection = vi.fn(() => {
+ revalidateCallCount += 1
+
+ // Every reconnect attempt after the drop hangs indefinitely; getConnection
+ // itself stays fast so this isolates the revalidate call specifically.
+ return new Promise(() => undefined)
+ })
+ ;(window as { hermesDesktop?: unknown }).hermesDesktop = desktop
+
+ render(<Harness />)
+ await flushAsync()
+ expect($gatewayState.get()).toBe('open')
+ const callsBeforeDrop = desktop.getConnection.mock.calls.length
+
+ act(() => FakeWebSocket.instances[0].drop())
+ await advanceBackoff()
+
+ expect(revalidateCallCount).toBe(1)
+ expect($gatewayState.get()).not.toBe('open')
+ // Still stuck behind the hung revalidate — execution never reached
+ // getConnection() at all.
+ expect(desktop.getConnection.mock.calls.length).toBe(callsBeforeDrop)
+
+ // Advance past the internal reconnect-attempt timeout (20s) — the stalled
+ // revalidate await must reject (swallowed, as it always was for a genuine
+ // rejection) so execution proceeds to getConnection() and the socket
+ // reopens, instead of latching on the still-pending revalidate forever.
+ await act(async () => {
+ await vi.advanceTimersByTimeAsync(20_000)
+ })
+
+ expect(desktop.getConnection.mock.calls.length).toBeGreaterThan(callsBeforeDrop)
+ expect($gatewayState.get()).toBe('open')
+ })
+
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 b970408c2a8..2baff83a962 100644
--- a/apps/desktop/src/app/gateway/hooks/use-gateway-boot.ts
+++ b/apps/desktop/src/app/gateway/hooks/use-gateway-boot.ts
@@ -96,16 +96,17 @@ 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.
+// desktop.revalidateConnection() / 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 any 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 all three 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> {
@@ -263,7 +264,13 @@ export function useGatewayBoot({
// whose 'exit' would clear the main process's cached descriptor — without
// this the renderer re-dials the same dead endpoint forever and stays on
// "Starting Hermes…". The probe is a no-op for a healthy or local backend.
- await desktop.revalidateConnection?.().catch(() => undefined)
+ // Bounded like the two awaits below: a wedged revalidation (#93454) is
+ // the specific hang this loop must survive, not just a rejection.
+ await withTimeout(
+ desktop.revalidateConnection?.() ?? Promise.resolve(),
+ RECONNECT_ATTEMPT_TIMEOUT_MS,
+ 'Timed out revalidating the gateway connection'
+ ).catch(() => undefined)
// Primary sleep/wake reconnect must dial the WINDOW-owned primary backend
// (same as boot/softSwitch). Passing $activeGatewayProfile would retarget