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(bridge): a closed socket rejects its in-flight requests instead of hanging them

enes44918468280e
diff --git a/src/renderer/bridge/ws-bridge.test.ts b/src/renderer/bridge/ws-bridge.test.ts
index 17a09356c..adee0454c 100644
--- a/src/renderer/bridge/ws-bridge.test.ts
+++ b/src/renderer/bridge/ws-bridge.test.ts
@@ -2,7 +2,7 @@ import { describe, it, expect, beforeEach, afterEach } from 'vitest'
import http from 'http'
import { WebSocketServer } from 'ws'
import WebSocket from 'ws'
-import { encodePtyData } from '../../shared/rpc'
+import { encodePtyData, E_DISCONNECTED } from '../../shared/rpc'
// RpcClient is exported for tests
import { RpcClient } from './ws-bridge'
@@ -60,4 +60,32 @@ describe('RpcClient', () => {
await client.ready()
await expect(client.request('missing')).rejects.toMatchObject({ code: 'E_NO_HANDLER' })
})
+
+ // A response can only ever arrive over the socket that carried the request. When that socket
+ // closes, every in-flight request is unanswerable — and a promise that neither resolves nor
+ // rejects is the worst of the three outcomes: the caller's `finally`/`setBusy(false)` never runs,
+ // so a dialog sits on "Creating…" with its Cancel button disabled forever, with no error and no
+ // way out. Fail closed: reject them, so `await` throws and every caller's error path can run.
+ it('rejects every in-flight request when the socket closes (never hangs them forever)', async () => {
+ wss.on('connection', (sock) => {
+ // Take the request, answer nothing, and drop the connection — the WS-drop-mid-call case.
+ sock.on('message', () => sock.close())
+ })
+ const client = new RpcClient(`ws://127.0.0.1:${port}/`)
+ await client.ready()
+ const a = client.request('git:worktree-add', '/repo')
+ const b = client.request('git:worktree-remove', '/repo')
+ await expect(a).rejects.toMatchObject({ code: E_DISCONNECTED })
+ await expect(b).rejects.toMatchObject({ code: E_DISCONNECTED })
+ })
+
+ it('still notifies the onClose hooks (the reconnect overlay) when it fails the pending requests', async () => {
+ wss.on('connection', (sock) => sock.on('message', () => sock.close()))
+ const client = new RpcClient(`ws://127.0.0.1:${port}/`)
+ await client.ready()
+ let closed = 0
+ client.onClose(() => closed++)
+ await expect(client.request('anything')).rejects.toThrow()
+ expect(closed).toBe(1)
+ })
})
diff --git a/src/renderer/bridge/ws-bridge.ts b/src/renderer/bridge/ws-bridge.ts
index 3eb56010b..b2496b054 100644
--- a/src/renderer/bridge/ws-bridge.ts
+++ b/src/renderer/bridge/ws-bridge.ts
@@ -6,7 +6,7 @@
// namespaces (`pty`, `workspace`, `settings`) over that socket. Every other namespace comes from
// `buildStubApi()` (Task 7) so the renderer boots without a full Electron preload.
-import { parseRpcMessage, decodePtyData, type RpcMessage } from '../../shared/rpc'
+import { parseRpcMessage, decodePtyData, E_DISCONNECTED, type RpcMessage } from '../../shared/rpc'
import { IPC } from '../../shared/ipc'
import {
UNKNOWN_CLAUDE_CLI_CAPS,
@@ -57,7 +57,32 @@ export class RpcClient {
ws.addEventListener('error', () => reject(new Error('WebSocket error')))
})
ws.addEventListener('message', (ev: MessageEvent) => this.onMessage(ev.data))
- ws.addEventListener('close', () => this.closeCbs.forEach((cb) => cb()))
+ ws.addEventListener('close', () => {
+ // Fail the in-flight requests BEFORE the overlay hooks: a response can only arrive over the
+ // socket that carried the request, so once it is gone they are unanswerable.
+ this.failPending()
+ this.closeCbs.forEach((cb) => cb())
+ })
+ }
+
+ /**
+ * Reject every in-flight request, because the socket that could have answered them is gone.
+ *
+ * A promise that never settles is the worst of the three outcomes. The caller's cleanup —
+ * `setBusy(false)`, a `finally`, an error banner — is all downstream of the `await`, so it simply
+ * never runs: a dialog sits on "Creating…" with its own Cancel button disabled by `busy`, showing
+ * no error and offering no way out but Escape; a Merge or Remove looks like a silent no-op. Every
+ * caller that handles a rejection at all handles this correctly the moment we actually reject, so
+ * failing closed here protects features that have not been written yet, not just this one.
+ */
+ private failPending(): void {
+ if (this.pending.size === 0) return
+ const waiting = [...this.pending.values()]
+ this.pending.clear() // clear first: a reject handler that fires another request must not see stale ids
+ const err = Object.assign(new Error('The connection to the server was lost.'), {
+ code: E_DISCONNECTED
+ })
+ for (const p of waiting) p.reject(err)
}
/** Resolves once the socket is open; rejects if it errors before opening. */