From 94c8c3788d7fc3834b9f80b306028a51f49fa41b Mon Sep 17 00:00:00 2001 From: Chris Scott <99081550+chriswritescode-dev@users.noreply.github.com> Date: Wed, 17 Jun 2026 17:13:18 -0400 Subject: [PATCH] fix: add connect timeout to event stream to recover from failed connections --- .../__tests__/openCodeEventStream.test.ts | 14 +++++++++++ .../openCodeEventStream.ts | 24 ++++++++++++++++++- shared/src/config/defaults.ts | 1 + 3 files changed, 38 insertions(+), 1 deletion(-) diff --git a/frontend/src/lib/opencode-event-stream/__tests__/openCodeEventStream.test.ts b/frontend/src/lib/opencode-event-stream/__tests__/openCodeEventStream.test.ts index 69848f46..4c7bf341 100644 --- a/frontend/src/lib/opencode-event-stream/__tests__/openCodeEventStream.test.ts +++ b/frontend/src/lib/opencode-event-stream/__tests__/openCodeEventStream.test.ts @@ -44,6 +44,20 @@ describe('OpenCodeEventStream', () => { expect(healthStates.at(-1)).toMatchObject({ isConnected: true, isHealthy: true, isStalled: false }) }) + it('recovers when a connection never opens', async () => { + const transport = new TestEventStreamTransport() + const stream = new OpenCodeEventStream({ transport }) + + stream.subscribeGlobalMonitor({ directories: [], onEvent: vi.fn() }) + expect(transport.openedUrls).toHaveLength(1) + + await vi.advanceTimersByTimeAsync(10_000) + expect(transport.closeCount).toBeGreaterThan(0) + + await vi.advanceTimersByTimeAsync(1_000) + expect(transport.openedUrls.length).toBeGreaterThan(1) + }) + it('reconnects when the watchdog detects a stall', async () => { const transport = new TestEventStreamTransport() const stream = new OpenCodeEventStream({ transport }) diff --git a/frontend/src/lib/opencode-event-stream/openCodeEventStream.ts b/frontend/src/lib/opencode-event-stream/openCodeEventStream.ts index 099f8d50..516192da 100644 --- a/frontend/src/lib/opencode-event-stream/openCodeEventStream.ts +++ b/frontend/src/lib/opencode-event-stream/openCodeEventStream.ts @@ -21,7 +21,7 @@ interface OpenCodeEventStreamOptions { transport?: EventStreamTransport } -const { RECONNECT_DELAY_MS, MAX_RECONNECT_DELAY_MS, STALL_THRESHOLD_MS, WATCHDOG_TICK_MS } = DEFAULTS.SSE +const { RECONNECT_DELAY_MS, MAX_RECONNECT_DELAY_MS, CONNECT_TIMEOUT_MS, STALL_THRESHOLD_MS, WATCHDOG_TICK_MS } = DEFAULTS.SSE export class OpenCodeEventStream { private connection: { close(): void } | null = null @@ -30,6 +30,7 @@ export class OpenCodeEventStream { private directoryRefCounts = new Map() private pendingDirectories = new Set() private reconnectTimeout: ReturnType | null = null + private connectTimeout: ReturnType | null = null private reconnectDelay: number = RECONNECT_DELAY_MS private connected = false private subscriberIdCounter = 0 @@ -194,9 +195,26 @@ export class OpenCodeEventStream { onConnected: (data) => this.handleConnected(data), onHeartbeat: () => this.markActivity(), }) + this.startConnectTimeout() + } + + private startConnectTimeout(): void { + this.clearConnectTimeout() + this.connectTimeout = setTimeout(() => { + this.connectTimeout = null + this.handleError() + }, CONNECT_TIMEOUT_MS) + } + + private clearConnectTimeout(): void { + if (this.connectTimeout) { + clearTimeout(this.connectTimeout) + this.connectTimeout = null + } } private handleOpen(): void { + this.clearConnectTimeout() this.connected = true this.reconnectDelay = RECONNECT_DELAY_MS this.startWatchdog() @@ -205,6 +223,7 @@ export class OpenCodeEventStream { } private handleError(): void { + this.clearConnectTimeout() this.connected = false this.clientId = null this.upstreamConnectedCount = null @@ -250,6 +269,7 @@ export class OpenCodeEventStream { this.clientId = null } + this.clearConnectTimeout() this.connected = true this.startWatchdog() this.markActivity() @@ -263,6 +283,7 @@ export class OpenCodeEventStream { this.reconnectTimeout = null } + this.clearConnectTimeout() this.stopWatchdog() this.connection?.close() this.connection = null @@ -289,6 +310,7 @@ export class OpenCodeEventStream { this.reconnectTimeout = null } + this.clearConnectTimeout() this.stopWatchdog() this.connection?.close() this.connection = null diff --git a/shared/src/config/defaults.ts b/shared/src/config/defaults.ts index d0a3a330..478a6472 100644 --- a/shared/src/config/defaults.ts +++ b/shared/src/config/defaults.ts @@ -50,6 +50,7 @@ export const DEFAULTS = { SSE: { RECONNECT_DELAY_MS: 1000, MAX_RECONNECT_DELAY_MS: 30000, + CONNECT_TIMEOUT_MS: 10000, IDLE_GRACE_PERIOD_MS: 5000, HEARTBEAT_INTERVAL_MS: 30000, STALL_THRESHOLD_MS: 90000,