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.
TypeScript adhdagent-orchestrationai-agentscanvasclaude-codecodexcoding-agentsdeveloper-toolselectrongeminilinuxmacosmultiplexeropencodeparallel-agentsterminalterminal-emulatorterminal-multiplexertmuxworkspace-manager
fix(server): terminate live WS clients on shutdown
enesb7a3f6fccaa0
diff --git a/src/server/index.ts b/src/server/index.tsindex b83705e52..698955f4b 100644--- a/src/server/index.ts+++ b/src/server/index.ts@@ -542,6 +542,10 @@ export async function startServer(// node env is torn down aborts the process. See SpeechService.shutdown.await speechService.shutdown()hookServer.stop()+ // No WS teardown counterpart to the serving branch's below, and none is owed: this branch+ // returns BEFORE `http.createServer`/`attachWsServer`, so there is no listener and no+ // upgraded socket that could hold a close open. `startServer` has two returns and a+ // shutdown step usually belongs in both — this one belongs in exactly one.}}}@@ -556,7 +560,7 @@ export async function startServer()// A closed browser tab is the NORMAL way to leave the Server Edition and sends no `pty:kill`,// so the WS close hook is what unsubscribes that client from the sessions it was watching.- attachWsServer(server, {+ const wsServer = attachWsServer(server, {platform,auth,onClientGone: (uiId) => {@@ -591,6 +595,11 @@ export async function startServer(await speechService.shutdown()// Close the loopback hook-server listener (it would otherwise die with the process anyway).hookServer.stop()+ // Upgraded WebSockets are not ordinary HTTP connections: server.close() waits for them but+ // does not end them. Own the WS lifecycle explicitly so a client close racing shutdown+ // cannot hang the Server Edition (or its tests) forever.+ for (const client of wsServer.clients) client.terminate()+ await new Promise<void>((resolve) => wsServer.close(() => resolve()))await new Promise<void>((resolve, reject) => {server.close((err) => (err ? reject(err) : resolve()))})diff --git a/src/server/ws.ts b/src/server/ws.tsindex a8fb4c70d..adbca3c62 100644--- a/src/server/ws.ts+++ b/src/server/ws.ts@@ -101,7 +101,7 @@ function upgradeAllowed(return true}-export function attachWsServer(server: http.Server, opts: WsServerOpts): void {+export function attachWsServer(server: http.Server, opts: WsServerOpts): WebSocketServer {const { platform, auth, onClientGone, heartbeatMs = WS_HEARTBEAT_MS, trustProxy } = optsconst wss = new WebSocketServer({ noServer: true, maxPayload: WS_MAX_PAYLOAD })@@ -211,4 +211,5 @@ export function attachWsServer(server: http.Server, opts: WsServerOpts): void {ws.on('close', () => teardown(uiId))})+ return wss}diff --git a/test/server/server-e2e.test.ts b/test/server/server-e2e.test.tsindex e8b51a2f7..7e1fc1734 100644--- a/test/server/server-e2e.test.ts+++ b/test/server/server-e2e.test.ts@@ -104,3 +104,46 @@ describe.skipIf(!hasTmux)('server e2e: login → ws → pty echo round-trip', ()ws.close()}, 30_000)})++describe('server shutdown with a live websocket', () => {+ it('terminates the upgraded client before closing the HTTP server', async () => {+ const dataDir = fs.mkdtempSync(path.join(os.tmpdir(), 'nt-e2e-shutdown-'))+ const srv = await startServer({+ port: 0, host: '127.0.0.1', dataDir,+ rendererDir: path.join(dataDir, 'no-renderer'), insecureHttp: false,+ passwordSeed: 'e2e-shutdown-password-123', installHooks: false+ })+ const res = await fetch(`http://127.0.0.1:${srv.port}/auth/login`, {+ method: 'POST',+ headers: { 'content-type': 'application/x-www-form-urlencoded' },+ body: 'password=e2e-shutdown-password-123',+ redirect: 'manual'+ })+ const cookie = res.headers.get('set-cookie')!.split(';')[0]+ const ws = new WebSocket(`ws://127.0.0.1:${srv.port}/ws`, { headers: { cookie } })+ await new Promise<void>((resolve, reject) => {+ ws.once('open', resolve)+ ws.once('error', reject)+ })+ const clientClosed = new Promise<void>((resolve) => ws.once('close', () => resolve()))+ let closeTimer: ReturnType<typeof setTimeout> | undefined++ try {+ await Promise.race([+ srv.close(),+ new Promise<never>((_resolve, reject) => {+ closeTimer = setTimeout(+ () => reject(new Error('server close hung on upgraded websocket')),+ 2_000+ )+ })+ ])+ await clientClosed+ expect(ws.readyState).toBe(WebSocket.CLOSED)+ } finally {+ if (closeTimer) clearTimeout(closeTimer)+ ws.terminate()+ fs.rmSync(dataDir, { recursive: true, force: true })+ }+ }, 10_000)+})