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(server): attach ws error listeners + process crash guards

enes6a5d1d7e7703
diff --git a/src/server/main.ts b/src/server/main.ts
index 33adb14e9..b55aa1e6b 100644
--- a/src/server/main.ts
+++ b/src/server/main.ts
@@ -5,6 +5,11 @@ import { startServer } from './index'
* Script entry point for the headless server. Kept separate from index.ts so that
* `index.ts` stays side-effect-free (importable by tests without booting a server).
*/
+// Last-resort loggers: a stray throw/rejection anywhere in the process should be
+// logged, not silently exit the server (which would tear down every session's pty).
+process.on('uncaughtException', (e) => console.error('[nodeterm-server] uncaughtException', e))
+process.on('unhandledRejection', (e) => console.error('[nodeterm-server] unhandledRejection', e))
+
async function main(): Promise<void> {
const config = resolveConfig(process.env, process.argv.slice(2))
const { port, close } = await startServer(config)
diff --git a/src/server/ws.test.ts b/src/server/ws.test.ts
index 2cfdd601e..04a9db1a5 100644
--- a/src/server/ws.test.ts
+++ b/src/server/ws.test.ts
@@ -71,4 +71,32 @@ describe('ws endpoint', () => {
expect(bins.length).toBe(1)
ws.close()
})
+
+ it('survives a receiver protocol error on one connection; others keep working', async () => {
+ platform.handle('echo:x', (v: string) => `got:${v}`)
+ const ws1 = await connect({ cookie, origin: `http://127.0.0.1:${port}` })
+ const ws2 = await connect({ cookie, origin: `http://127.0.0.1:${port}` })
+ // Swallow the expected client-side error when the server closes ws1's bad frame.
+ ws1.on('error', () => {})
+
+ // Corrupt ws1: write a raw UNMASKED text frame straight to its TCP socket.
+ // RFC 6455 requires client→server frames to be masked, so the server's receiver
+ // emits 'error' — which, without our listener, would throw and exit the process.
+ // 0x81 = FIN+text, 0x00 = mask bit unset + length 0.
+ ;(ws1 as unknown as { _socket: NodeJS.WritableStream })._socket.write(
+ Buffer.from([0x81, 0x00])
+ )
+ await new Promise((r) => setTimeout(r, 100))
+
+ // The process is still alive and ws2 round-trips a request.
+ const messages: string[] = []
+ ws2.on('message', (d, isBinary) => { if (!isBinary) messages.push(d.toString()) })
+ ws2.send(JSON.stringify({ t: 'req', id: 9, method: 'echo:x', args: ['ok'] }))
+ await new Promise((r) => setTimeout(r, 200))
+ expect(messages.map((m) => JSON.parse(m))).toContainEqual({
+ t: 'res', id: 9, ok: true, result: 'got:ok'
+ })
+ ws1.close()
+ ws2.close()
+ })
})
diff --git a/src/server/ws.ts b/src/server/ws.ts
index 5c81aff53..22f5d76cf 100644
--- a/src/server/ws.ts
+++ b/src/server/ws.ts
@@ -58,6 +58,9 @@ export function attachWsServer(server: http.Server, opts: WsServerOpts): void {
if (pathname !== '/ws') return
if (!upgradeAllowed(req, auth)) {
+ // Guard the raw socket: writing to an already-dead peer emits 'error', and an
+ // unhandled 'error' on a socket (EventEmitter) throws → crashes the process.
+ socket.on('error', () => {})
socket.write('HTTP/1.1 401 Unauthorized\r\n\r\n')
socket.destroy()
return
@@ -92,6 +95,14 @@ export function attachWsServer(server: http.Server, opts: WsServerOpts): void {
// res/ev from a client are ignored.
})
+ // Without an 'error' listener, a receiver protocol error (malformed/unmasked
+ // frame, corrupted proxy frame) emits 'error' on the socket with no listener,
+ // which THROWS → uncaughtException → the whole server exits, tearing down every
+ // session's pty. Log and let 'close' fire for this one connection only.
+ ws.on('error', (e: NodeJS.ErrnoException) => {
+ console.warn('[nodeterm-server] ws socket error', (e && e.code) || e)
+ })
+
ws.on('close', () => {
platform.detach(uiId)
})