The ultimate harness for coding agents and beyond. All your agents. All your machines. One command center. Start with code, then follow your curiosity and build across disciplines: CAD, circuits, robots, games and music.
TypeScript 3d-modelingagent-frameworkagent-harnessai-agentscadclaude-codecodexcoding-agentsdeveloper-toolsesp32game-developmentmcp-servermulti-agentopen-hardwareopencodeopenharnesspcbroboticsterminaltmux
fix: add handshake timeout to WebSocket connection to prevent hanging in connecting state
0xken15396f1621c64e
diff --git a/cli/src/backendSocket.spec.ts b/cli/src/backendSocket.spec.tsindex c349d6967..8210bfee4 100644--- a/cli/src/backendSocket.spec.ts+++ b/cli/src/backendSocket.spec.ts@@ -19,7 +19,7 @@ const wsMock = vi.hoisted(() => {failNextSend: Error | null = nullprivate handlers = new Map<string, Array<(...args: unknown[]) => void>>()- constructor(readonly url: string, readonly protocols: string[]) {+ constructor(readonly url: string, readonly protocols: string[], readonly options?: { handshakeTimeout?: number }) {instances.push(this)}@@ -59,6 +59,12 @@ const wsMock = vi.hoisted(() => {this.emit('close', 1006)}+ /** What `ws` does when `handshakeTimeout` elapses: abort the upgrade, then report the socket gone. */+ handshakeTimeout(): void {+ this.emit('error', new Error('Opening handshake has timed out'))+ this.close()+ }+terminate(): void {this.close()}@@ -103,6 +109,34 @@ describe('BackendSocket outbound queue', () => {await socket.stop()})+ it('bounds the opening handshake and reconnects when it times out', async () => {+ // A connect attempt whose TCP side came up but whose upgrade was never answered used to sit in+ // CONNECTING forever: no 'open', so no heartbeat to terminate it, and `this.ws` set, so every+ // later connect() returned early. The daemon then showed "cloud reconnecting…" until restarted.+ vi.useFakeTimers()+ const socket = new BackendSocket('token')+ socket.connect()+ const ws1 = wsMock.instances[0]+ expect(ws1.options?.handshakeTimeout).toBe(15_000)+ expect(ws1.readyState).toBe(wsMock.MockWebSocket.CONNECTING)++ // Still connecting: a second connect() must not open a competing socket …+ socket.connect()+ expect(wsMock.instances).toHaveLength(1)++ // … but once the handshake is abandoned, the ordinary backoff schedules a fresh attempt.+ ws1.handshakeTimeout()+ expect(socket.isConnected()).toBe(false)+ await vi.advanceTimersByTimeAsync(1_000)+ expect(wsMock.instances).toHaveLength(2)+ const ws2 = wsMock.instances[1]+ expect(ws2.options?.handshakeTimeout).toBe(15_000)+ ws2.open()+ expect(socket.isConnected()).toBe(true)++ await socket.stop()+ })+it('keeps a frame queued when ws.send reports an error and retries after reconnect', async () => {vi.useFakeTimers()const socket = new BackendSocket('token')diff --git a/cli/src/backendSocket.ts b/cli/src/backendSocket.tsindex 61a0e0d9f..9fb7c3422 100644--- a/cli/src/backendSocket.ts+++ b/cli/src/backendSocket.ts@@ -91,6 +91,12 @@ export type RecentProvider = (sessionId: string, n: number) => Array<{ kind: strconst HEARTBEAT_MS = 20_000const APP_PING_MS = 15_000+// How long the opening handshake may take before the attempt is abandoned and retried. `ws` waits+// forever by default, and the heartbeat below only starts on 'open' — so a TCP connection that came+// up while the network was flapping but never got its upgrade answered sat in CONNECTING for hours,+// `this.ws` set, every later connect() returning early, and the daemon reporting "cloud+// reconnecting…" until someone restarted it.+const HANDSHAKE_TIMEOUT_MS = 15_000const BASE_DELAY_MS = 1_000const MAX_DELAY_MS = 30_000const QUEUE_MAX = 2_000@@ -555,7 +561,9 @@ export class BackendSocket {return}if (this.closed) { this.connecting = false; return }- const ws = new WebSocket(this.url, [token])+ // On timeout `ws` emits 'error' ("Opening handshake has timed out") then 'close', which lands in+ // onGone below and re-enters the ordinary backoff — the same path a refused connection takes.+ const ws = new WebSocket(this.url, [token], { handshakeTimeout: HANDSHAKE_TIMEOUT_MS })this.ws = wsthis.connecting = false