Skip to content

Node-based terminal manager for AI coding agents — tmux-backed terminals and parallel agent sessions as draggable nodes on an infinite pan/zoom canvas. macOS, Linux, and a browser Server Edition.

fix(relay): handle the ws 'error' event so a relay 504/outage can't crash the app

enesb05dacf0f320
diff --git a/src/main/remote/relay-socket.test.ts b/src/main/remote/relay-socket.test.ts
index d1327b3fe..29cd56a8a 100644
--- a/src/main/remote/relay-socket.test.ts
+++ b/src/main/remote/relay-socket.test.ts
@@ -7,10 +7,54 @@
// session under its own keypair and then forge the second human's `trust:confirm` under the swapped
// key (see docs/remote-sessions.md, obligation (a)). This suite drives REAL relay sockets over an
// in-process transport that also models the relay's own injection capability.
-import { describe, expect, it } from 'vitest'
-import { connectRelay, type RelaySocket, type RelayTransport } from './relay-socket'
+import { describe, expect, it, vi } from 'vitest'
+import { connectRelay, wrapWebSocket, type RelaySocket, type RelayTransport } from './relay-socket'
import { genKeyPair, publicKeyToB64, randomSessionNonce } from './e2ee'
+// A minimal `ws`-shaped fake: a listener registry + an `emit` helper, enough for wrapWebSocket.
+function fakeWs() {
+ const listeners: Record<string, ((...a: unknown[]) => void)[]> = {}
+ return {
+ binaryType: '',
+ bufferedAmount: 0,
+ on(event: string, cb: (...a: unknown[]) => void) {
+ ;(listeners[event] ??= []).push(cb)
+ },
+ send() {},
+ close() {},
+ emit(event: string, ...args: unknown[]) {
+ for (const cb of listeners[event] ?? []) cb(...args)
+ }
+ }
+}
+
+describe('wrapWebSocket: a ws error must not be an uncaught crash', () => {
+ it('routes a ws `error` to onClose exactly once (never both, never a re-throw)', () => {
+ const ws = fakeWs()
+ const wrapped = wrapWebSocket(ws as unknown as import('ws').WebSocket)
+ const onClose = vi.fn()
+ wrapped.onClose(onClose)
+
+ // A 504 upgrade timeout / TLS failure surfaces as a ws 'error'. Without a listener Node would
+ // re-throw it as an uncaught exception and crash the app; wrapWebSocket must catch it here.
+ expect(() => ws.emit('error', new Error('Unexpected server response: 504'))).not.toThrow()
+ expect(onClose).toHaveBeenCalledTimes(1)
+
+ // A 'close' that follows the error must NOT fire onClose a second time (the once-guard).
+ ws.emit('close')
+ expect(onClose).toHaveBeenCalledTimes(1)
+ })
+
+ it('a plain close (no error) still fires onClose once', () => {
+ const ws = fakeWs()
+ const wrapped = wrapWebSocket(ws as unknown as import('ws').WebSocket)
+ const onClose = vi.fn()
+ wrapped.onClose(onClose)
+ ws.emit('close')
+ expect(onClose).toHaveBeenCalledTimes(1)
+ })
+})
+
// A pair of in-process transports wired host<->client, PLUS the relay's injection ports: the relay
// is untrusted and forwards opaque bytes, so it can also ORIGINATE traffic in either direction.
function makeTransportPair(): {
diff --git a/src/main/remote/relay-socket.ts b/src/main/remote/relay-socket.ts
index 3d4e3b05e..227be28f4 100644
--- a/src/main/remote/relay-socket.ts
+++ b/src/main/remote/relay-socket.ts
@@ -688,7 +688,21 @@ export function wrapWebSocket(ws: import('ws').WebSocket): RelayTransport {
ws.on('message', (data: Buffer, isBinary: boolean) =>
cb(isBinary ? data : data.toString('utf-8'))
),
- onClose: (cb) => ws.on('close', () => cb())
+ onClose: (cb) => {
+ // Fire `cb` on EITHER a close OR an error, exactly once. A ws `error` (a 504 upgrade timeout,
+ // a TLS/DNS failure, connection refused) is otherwise an UNHANDLED 'error' event, which Node
+ // re-throws as an uncaught exception that crashes the whole app. Treat it as a close so the
+ // caller tears the session down / reconnects gracefully instead. The caller registers this
+ // synchronously right after opening (relay-socket connectRelay), before any async error fires.
+ let fired = false
+ const fire = (): void => {
+ if (fired) return
+ fired = true
+ cb()
+ }
+ ws.on('close', fire)
+ ws.on('error', fire)
+ }
}
}