From 023ce02cefd181a0c987a7c0ff4d290c0f963a96 Mon Sep 17 00:00:00 2001 From: Neil <4138956+nwparker@users.noreply.github.com> Date: Sun, 31 May 2026 09:24:39 -0700 Subject: [PATCH] fix: close runtime sockets during stop (#4240) --- src/main/runtime/rpc/unix-socket-transport.ts | 13 +++++- src/main/runtime/runtime-rpc.test.ts | 40 +++++++++++++++++++ 2 files changed, 51 insertions(+), 2 deletions(-) diff --git a/src/main/runtime/rpc/unix-socket-transport.ts b/src/main/runtime/rpc/unix-socket-transport.ts index 07866b6ae..f74bab9fe 100644 --- a/src/main/runtime/rpc/unix-socket-transport.ts +++ b/src/main/runtime/rpc/unix-socket-transport.ts @@ -35,6 +35,7 @@ export class UnixSocketTransport implements RpcTransport { private readonly keepaliveIntervalMs: number private server: Server | null = null private messageHandler: MessageHandler | null = null + private readonly activeSockets = new Set() constructor({ endpoint, kind, keepaliveIntervalMs }: UnixSocketTransportOptions) { this.endpoint = endpoint @@ -81,7 +82,7 @@ export class UnixSocketTransport implements RpcTransport { if (!server) { return } - await new Promise((resolve, reject) => { + const closePromise = new Promise((resolve, reject) => { server.close((error) => { if (error) { reject(error) @@ -90,12 +91,19 @@ export class UnixSocketTransport implements RpcTransport { resolve() }) }) + // Why: server.close() stops accepting new connections but waits for + // existing sockets; long-poll keepalives can otherwise hold shutdown open. + for (const socket of Array.from(this.activeSockets)) { + socket.destroy() + } + await closePromise if (this.kind === 'unix' && existsSync(this.endpoint)) { rmSync(this.endpoint, { force: true }) } } private handleConnection(socket: Socket): void { + this.activeSockets.add(socket) let buffer = '' let oversized = false // Why: each in-flight dispatch registers its own AbortController here so @@ -114,11 +122,12 @@ export class UnixSocketTransport implements RpcTransport { socket.on('error', () => { socket.destroy() }) - socket.on('close', () => { + socket.once('close', () => { for (const cleanup of inflight) { cleanup() } inflight.clear() + this.activeSockets.delete(socket) }) socket.on('data', (chunk: string) => { if (oversized) { diff --git a/src/main/runtime/runtime-rpc.test.ts b/src/main/runtime/runtime-rpc.test.ts index 270ef54bd..554edb239 100644 --- a/src/main/runtime/runtime-rpc.test.ts +++ b/src/main/runtime/runtime-rpc.test.ts @@ -2530,6 +2530,46 @@ describe('OrcaRuntimeRpcServer', () => { } }) + it('destroys active Unix socket connections when the runtime stops', async () => { + const userDataPath = mkdtempSync(join(tmpdir(), 'orca-runtime-rpc-')) + const runtime = new OrcaRuntimeService() + const db = new OrchestrationDb(':memory:') + runtime.setOrchestrationDb(db) + const server = new OrcaRuntimeRpcServer({ + runtime, + userDataPath, + keepaliveIntervalMs: 1000, + longPollCap: 1 + }) + await server.start() + + try { + const metadata = readRuntimeMetadata(userDataPath) + const endpoint = metadata!.transports[0]!.endpoint + + const session = openFramedSession(endpoint, { + id: 'req_stop', + authToken: metadata!.authToken, + method: 'orchestration.check', + params: { terminal: 'term_stop', wait: true, timeoutMs: 10_000 } + }) + await waitFor(() => server['activeLongPolls'] === 1) + + const stopResult = await Promise.race([ + server.stop().then(() => 'stopped'), + sleep(500).then(() => 'timeout') + ]) + + expect(stopResult).toBe('stopped') + await session.done + await waitFor(() => server['activeLongPolls'] === 0) + expect(session.socket.destroyed).toBe(true) + } finally { + db.close() + await server.stop() + } + }) + it('responds runtime_busy once the long-poll cap is saturated', async () => { const userDataPath = mkdtempSync(join(tmpdir(), 'orca-runtime-rpc-')) const runtime = new OrcaRuntimeService()