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: close websocket on handshake failure to prevent connection leaks
0xken15c4ce8e045dde
diff --git a/cli/src/lib/e2ee/relayLink.spec.ts b/cli/src/lib/e2ee/relayLink.spec.tsindex 71aaa79e2..5f3821246 100644--- a/cli/src/lib/e2ee/relayLink.spec.ts+++ b/cli/src/lib/e2ee/relayLink.spec.ts@@ -529,3 +529,77 @@ describe('RemoteRelayPool drops a peer the responder no longer trusts', () => {}})})++describe('RemoteRelayPool closes the upstream socket when the handshake fails', () => {+ // Regression for the leak measured on prod 2026-09-15: a handshake that rejected without closing+ // its socket left an OPEN, unreachable `/api/web-ws` connection behind on every retry — one user+ // reached 175 concurrent sockets on a single backend pod — and each one counted as a "connection"+ // in user_daily_presence. Any rejection path must take the socket down with it.+ const fakeAuth = { accessToken: async () => 'unused-in-this-fake' } as unknown as import('../authSession.js').AuthSessionManager++ /** Dials once against a fake backend and reports how it ended: the acquire() rejection and the close+ * code the SERVER saw on the socket (-1 = never closed within 2s, i.e. leaked). */+ async function dialAgainst(serverBehaviour: (ws: import('ws').WebSocket) => void): Promise<{ rejection: unknown; closeCode: number }> {+ const wss = new WebSocketServer({ port: 0 })+ let resolveClose!: (code: number) => void+ const serverSawClose = new Promise<number>((resolve) => { resolveClose = resolve })+ wss.on('connection', (ws) => {+ ws.on('close', (code) => resolveClose(code))+ serverBehaviour(ws)+ })+ try {+ const port = (wss.address() as AddressInfo).port+ const peers = new MachinePeerStore()+ peers.pin(MACHINE_ID, C.b64e(C.newIdentity().pub), 'harness link')+ const pool = new RemoteRelayPool(fakeAuth, `ws://127.0.0.1:${port}`, C.newIdentity(), peers)+ const sink = { sendFrame: () => true, sendBinary: () => true }+ let rejection: unknown = null+ try {+ await pool.acquire(MACHINE_ID, 'prod', { type: 'machine_select', payload: { machineId: MACHINE_ID } }, sink, () => {})+ } catch (err) { rejection = err }+ // Bounded wait: if the socket is leaked this never resolves, and the test must fail, not hang.+ const closeCode = await Promise.race([+ serverSawClose,+ new Promise<number>((resolve) => setTimeout(() => resolve(-1), 2_000)),+ ])+ return { rejection, closeCode }+ } finally {+ // A leaked client would otherwise keep wss.close() waiting until the test timeout — kill it so a+ // regression fails on the closeCode assertion instead.+ for (const client of wss.clients) client.terminate()+ await new Promise<void>((resolve) => wss.close(() => resolve()))+ }+ }++ it('machine_select_error rejects AND closes the socket', async () => {+ const { rejection, closeCode } = await dialAgainst((ws) => {+ ws.on('message', (raw) => {+ const frame = JSON.parse(raw.toString()) as Frame+ if (frame.type === 'machine_select') {+ ws.send(JSON.stringify({ type: 'machine_select_error', payload: { machineId: MACHINE_ID, error: 'NOT_YOUR_MACHINE' } }))+ }+ })+ })+ expect(rejection).toBeInstanceOf(Error)+ expect((rejection as Error).message).toBe('NOT_YOUR_MACHINE')+ expect(closeCode).toBe(1000)+ })++ it('an invalid e2e_welcome rejects AND closes the socket', async () => {+ // Stands in for the timeout path (peer never answers e2e_hello): same catch block, but without+ // waiting out CONNECT_TIMEOUT_MS. The select is acked so this socket is already bound to the+ // machine on the backend side — exactly the shape of the leaked sockets seen in prod.+ const { rejection, closeCode } = await dialAgainst((ws) => {+ ws.on('message', (raw) => {+ const frame = JSON.parse(raw.toString()) as Frame+ if (frame.type === 'machine_select') {+ ws.send(JSON.stringify({ type: 'connected', payload: { machineId: MACHINE_ID } }))+ } else if (frame.type === 'e2e_hello') {+ ws.send(JSON.stringify({ type: 'e2e_welcome', payload: { garbage: true } }))+ }+ })+ })+ expect((rejection as Error).message).toBe('E2EE_WELCOME_INVALID')+ expect(closeCode).toBe(1000)+ })+})diff --git a/cli/src/lib/remoteRelay.ts b/cli/src/lib/remoteRelay.tsindex bd32f6b61..7772ce5c2 100644--- a/cli/src/lib/remoteRelay.ts+++ b/cli/src/lib/remoteRelay.ts@@ -260,6 +260,11 @@ export class RemoteRelayPool {const token = await this.auth.accessToken({ force: forceRefresh })const url = `${this.backendWsBase}/api/web-ws?autonomousEnv=${encodeURIComponent(autonomousEnv)}`const ws = new WebSocket(url, [token])+ // An 'error' with no listener throws out of the emitter and takes the daemon down. The handshake's+ // once('error') below is consumed by its first emit, and nothing listens after the handshake at all;+ // this sink turns every later emit — including the one terminate() raises while CONNECTING — into a+ // plain 'close', which is what the owners actually clean up on.+ ws.on('error', () => { /* handled via 'close' */ })const crypto = new RelaySessionCrypto({ machineId, selfIdentity: this.selfIdentity, peerPub: b64d(peer.pub) })const entry: Entry = {ws,@@ -288,7 +293,7 @@ export class RemoteRelayPool {// e2e_hello/e2e_welcome as the "client" role — see lib/e2ee/relayClient.ts. Only once BOTH are done// does the app's onOutgoing/sink start receiving anything, so it never sees a half-encrypted stream.let selected = false- await new Promise<void>((resolve, reject) => {+ const handshake = new Promise<void>((resolve, reject) => {let settled = falseconst timeout = setTimeout(() => {if (!settled) { settled = true; reject(new RelayConnectError('relay connect timed out')) }@@ -446,6 +451,23 @@ export class RemoteRelayPool {if (!settled) { settled = true; clearTimeout(timeout); reject(err instanceof Error ? err : new Error(String(err))) }})})+ try {+ await handshake+ } catch (err) {+ // A rejected handshake MUST take the socket down with it. Only `e2e_denied` used to; the timeout+ // (peer never answered e2e_hello) and `machine_select_error` just dropped their reference, leaving+ // an OPEN socket nothing could reach — not in `entries`, no heartbeat, but still auto-answering+ // backend's pings — and the app's select retries leaked one more each time (175 concurrent+ // sockets from one user on a single backend pod, 2026-09-15). A clean close(1000) so backend logs+ // an ordinary disconnect; `ws` bounds the wait for the peer's close frame at 30s (closeTimeout)+ // and terminates on its own after that, so a mute peer cannot turn this back into a leak.+ if (ws.readyState === WebSocket.OPEN) {+ try { ws.close(1000, 'handshake failed') } catch { ws.terminate() }+ } else if (ws.readyState === WebSocket.CONNECTING) {+ ws.terminate()+ }+ throw err+ }// Handshake done — from here on, a close is the entry's real end-of-life, not a handshake failure.ws.on('close', (code, reasonBuf) => {entry.heartbeat?.stop()