fix(server): kill launched commands when the server dies #45965
Mergedjuliusmarmingemerged 3 commits into
main fromrefs/pull/45965/headmerged by juliusmarminge View on GitHubtanstack-source added labelsbug
github-actions[bot](GitHub profile, opens in a new tab) added labelssize:L
- @@ -143,2 +155,21 @@143155 const command = this.commands.get(message.id);144156 if (command === undefined) return;157+158+159+160+161+162+ socket.write(header, (error) => error ? reject(error) : resolve(socket))163+ })164+ })165++ // Each spawned command, with the process or group that kills it166++ const running = new Map()167+ const spawn = async (socketPath , token , { args, command, id, options } ) => {168+ const stdio = await Promise.allSettled(169+ options.stdio.map((mode, fd) => mode === "pipe" ? connect(socketPath, token, id, fd) : mode)170+171+ if (errno) throw new Error(\`setBlocking failed: \${Util.getSystemErrorName(errno)}\`)172+ }173+ const child = ChildProcess.spawn(command, args, { ...options, stdio: values })174++ child.on("spawn", () => running.set(child, options.detached ? -child.pid : child.pid))175++ child.on("exit", () => running.delete(child))
- @@ -143,2 +155,21 @@143155 const command = this.commands.get(message.id);144156 if (command === undefined) return;157+158+159+160+161+162+ socket.write(header, (error) => error ? reject(error) : resolve(socket))163+ })164+ })165++ // Each spawned command, with the process or group that kills it166++ const running = new Map()167+ const spawn = async (socketPath , token , { args, command, id, options } ) => {168+ const stdio = await Promise.allSettled(169+ options.stdio.map((mode, fd) => mode === "pipe" ? connect(socketPath, token, id, fd) : mode)170+171+ if (errno) throw new Error(\`setBlocking failed: \${Util.getSystemErrorName(errno)}\`)172+ }173+ const child = ChildProcess.spawn(command, args, { ...options, stdio: values })174++ child.on("spawn", () => running.set(child, options.detached ? -child.pid : child.pid))175++ child.on("exit", () => running.delete(child))@@ -144,0 +173,21 @@173+ const child = ChildProcess.spawn(command, args, { ...options, stdio: values })174++ child.on("spawn", () => running.set(child, options.detached ? -child.pid : child.pid))175++ child.on("exit", () => running.delete(child))176+ child.on("error", (error) => send({ _tag: "Error", id, error: serializeError(error) }))177+ child.on("spawn", () => send({ _tag: "Spawned", id, pid: child.pid }))178+ child.on("exit", (code, signal) => send({ _tag: "Exit", id, code, signal }))179+180+ // the proxy process exits when its commands have exited.181+ process.on("SIGINT", () => {})182+ process.on("SIGTERM", () => {})183++ // An application that was killed never stops its commands, so they die with its channel.184++ // A deliberate close leaves them, so an unreferenced command can outlive the application.185++ let closing = false186++ process.on("disconnect", () => {187++ if (closing) return188++ for (const target of running.values()) {189++ try {190++ process.kill(target, "SIGKILL")191++ } catch {}192++ }193++ })
Sign in to join this conversation.


Thread transfer impact#
✅ Thread transfer remains within every enforced ceiling.
Other scenarios#
All 64 scenario measurements match
main.All 64 scenario measurements
short turn resumeWebSocket decodedshort turn resumeWebSocket messageslong turn running, cold openHTTP decodedlong turn running, cold openrowslong turn running, history pageHTTP decodedlong turn running, history pagerowslong turn finished, cold openHTTP decodedlong turn finished, cold openrowslong turn finished, socket cold openWebSocket decodedlong turn finished, socket cold openWebSocket messageslong turn finished, socket cold openrowslong turn finished, work pageHTTP decodedlong turn finished, work pagerowsidle thread resumeWebSocket decodedidle thread resumeWebSocket messagesfleet shell snapshotHTTP decodedfleet shell snapshotrowsfleet shell subscribeWebSocket decodedfleet shell subscribeWebSocket messagesfleet shell resume checkHTTP decodedfleet shell resume checkrowsstale shell reloadHTTP decodedstale shell reloadrowsstale shell resumeWebSocket decodedstale shell resumeWebSocket messagesstale todo list cold openHTTP decodedstale todo list cold openrowsstale todo list cold opennodesstale todo list cold opensubagentsstale todo list cold openplansstale todo list cold openproviderTurnsstale todo list cold opencheckpointsshort turn resumeWebSocket decodedshort turn resumeWebSocket messageslong turn running, cold openHTTP decodedlong turn running, cold openrowslong turn running, history pageHTTP decodedlong turn running, history pagerowslong turn finished, cold openHTTP decodedlong turn finished, cold openrowslong turn finished, socket cold openWebSocket decodedlong turn finished, socket cold openWebSocket messageslong turn finished, socket cold openrowslong turn finished, work pageHTTP decodedlong turn finished, work pagerowsidle thread resumeWebSocket decodedidle thread resumeWebSocket messagesfleet shell snapshotHTTP decodedfleet shell snapshotrowsfleet shell subscribeWebSocket decodedfleet shell subscribeWebSocket messagesfleet shell resume checkHTTP decodedfleet shell resume checkrowsstale shell reloadHTTP decodedstale shell reloadrowsstale shell resumeWebSocket decodedstale shell resumeWebSocket messagesstale todo list cold openHTTP decodedstale todo list cold openrowsstale todo list cold opennodesstale todo list cold opensubagentsstale todo list cold openplansstale todo list cold openproviderTurnsstale todo list cold opencheckpointsBaseline:
a911b26· PR result:ddcf7cc· Source CI: successScenario and decoded snapshot size
10 historical turns, 5 command tools per turn, 878.9 KiB retained MCP result per historical turn, and a 1.05 MiB retained result in the measured turn.
Updated in place by a trusted workflow. PR artifacts are strictly validated and never executed.