Skip to content

Commit a794c46

Browse files
fix(opencode): prompt-probe stops the run's whole process tree on macOS and Linux
On Windows the probe stopped `opencode run` with taskkill /T, which walks the tree, but elsewhere it sent a plain kill to the run alone: the MCP servers the run had started kept running, holding the temp directory and their pipes. The run is now spawned as the leader of its own process group on macOS and Linux, and stopping it signals the whole group (SIGTERM, then SIGKILL if the run has not exited after 5 s). As before, nothing is signalled once the run has exited, so a reused PID is never targeted. The logic lives in script/process-tree.ts with unit tests for the plan and a live test that stops a child and its grandchild on the platform it runs on. Ref anomalyco#47 Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01GHX6CpBR14wnFxJLDahwTr
1 parent 57946e8 commit a794c46

3 files changed

Lines changed: 129 additions & 7 deletions

File tree

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
// Stopping a child process and everything it started (#47, prompt-probe).
2+
//
3+
// `opencode run` starts MCP servers as its own children. Killing only the run
4+
// leaves them running, holding the temp directory and, for a stdio server, its
5+
// pipes. On Windows `taskkill /T` walks the tree. On macOS and Linux the child
6+
// is spawned as the leader of its own process group, and the whole group is
7+
// signalled. Either way only while the child still runs, so a PID the system
8+
// has since reused is never targeted.
9+
10+
import { spawn, type ChildProcess } from "node:child_process"
11+
12+
/** Spawn options that let stop() reach the child's whole tree. */
13+
export function spawnOptions(platform: NodeJS.Platform = process.platform) {
14+
// detached on Windows would open a console window for the child; taskkill does not need it
15+
return { detached: platform !== "win32" }
16+
}
17+
18+
export type Plan = { kind: "taskkill"; args: string[] } | { kind: "group"; pgid: number }
19+
20+
/** How to stop a child and its descendants, or undefined when there is nothing safe to target. */
21+
export function plan(input: {
22+
platform: NodeJS.Platform
23+
pid: number | undefined
24+
running: boolean
25+
}): Plan | undefined {
26+
if (!input.running || input.pid === undefined) return undefined
27+
if (input.platform === "win32") return { kind: "taskkill", args: ["/pid", String(input.pid), "/T", "/F"] }
28+
return { kind: "group", pgid: input.pid }
29+
}
30+
31+
/**
32+
* Stops a child spawned with spawnOptions() and waits for it to exit, at most
33+
* `graceMs`. On POSIX the group gets SIGTERM, and SIGKILL if the child has not
34+
* exited by then.
35+
*/
36+
export async function stop(child: ChildProcess, graceMs = 5000) {
37+
const exited =
38+
child.exitCode !== null || child.signalCode !== null
39+
? Promise.resolve()
40+
: new Promise<void>((done) => child.once("exit", () => done()))
41+
const step = plan({
42+
platform: process.platform,
43+
pid: child.pid,
44+
running: child.exitCode === null && child.signalCode === null,
45+
})
46+
if (step?.kind === "taskkill")
47+
await new Promise((done) => spawn("taskkill", step.args, { stdio: "ignore" }).once("exit", done))
48+
if (step?.kind === "group") signal(step.pgid, "SIGTERM")
49+
await Promise.race([exited, new Promise((done) => setTimeout(done, graceMs))])
50+
if (step?.kind === "group" && child.exitCode === null && child.signalCode === null) {
51+
signal(step.pgid, "SIGKILL")
52+
await Promise.race([exited, new Promise((done) => setTimeout(done, graceMs))])
53+
}
54+
}
55+
56+
function signal(pgid: number, name: NodeJS.Signals) {
57+
try {
58+
// a negative PID signals the whole process group
59+
process.kill(-pgid, name)
60+
} catch {
61+
// ESRCH: the group is already gone
62+
}
63+
}
64+
65+
export * as ProcessTree from "./process-tree"

‎packages/opencode/script/prompt-probe.ts‎

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import { mkdtempSync, rmSync } from "node:fs"
1818
import { tmpdir } from "node:os"
1919
import path from "node:path"
2020
import { parseArgs } from "node:util"
21+
import { ProcessTree } from "./process-tree"
2122

2223
const { values: args } = parseArgs({
2324
options: {
@@ -120,6 +121,8 @@ async function capture(dir: string) {
120121
OPENCODE_DB: ":memory:",
121122
},
122123
stdio: ["ignore", "ignore", "pipe"],
124+
// the run's MCP servers are its children: stop() reaches them through this
125+
...ProcessTree.spawnOptions(),
123126
},
124127
)
125128
let stderr = ""
@@ -137,13 +140,7 @@ async function capture(dir: string) {
137140
clearTimeout(timer)
138141
// The run starts MCP servers as its own children: stop the whole tree, and
139142
// only while the child still runs, so a reused PID is never targeted.
140-
const running = child.exitCode === null && child.signalCode === null
141-
if (running && process.platform === "win32" && child.pid)
142-
await new Promise((done) =>
143-
spawn("taskkill", ["/pid", String(child.pid), "/T", "/F"], { stdio: "ignore" }).once("exit", done),
144-
)
145-
if (running && process.platform !== "win32") child.kill()
146-
await Promise.race([exited, new Promise((done) => setTimeout(done, 5000))])
143+
await ProcessTree.stop(child)
147144
server.stop(true)
148145
try {
149146
rmSync(temp, { recursive: true, force: true, maxRetries: 10, retryDelay: 200 })
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import { spawn } from "node:child_process"
2+
import { describe, expect, test } from "bun:test"
3+
import { ProcessTree } from "../../script/process-tree"
4+
5+
describe("ProcessTree.plan", () => {
6+
test("Windows: taskkill walks the tree from the child", () => {
7+
expect(ProcessTree.plan({ platform: "win32", pid: 42, running: true })).toEqual({
8+
kind: "taskkill",
9+
args: ["/pid", "42", "/T", "/F"],
10+
})
11+
})
12+
13+
test("macOS and Linux: the child's whole process group is signalled, not the child alone", () => {
14+
expect(ProcessTree.plan({ platform: "linux", pid: 42, running: true })).toEqual({ kind: "group", pgid: 42 })
15+
expect(ProcessTree.plan({ platform: "darwin", pid: 42, running: true })).toEqual({ kind: "group", pgid: 42 })
16+
})
17+
18+
test("a child that has exited is never targeted: its PID may be reused", () => {
19+
expect(ProcessTree.plan({ platform: "win32", pid: 42, running: false })).toBeUndefined()
20+
expect(ProcessTree.plan({ platform: "linux", pid: 42, running: false })).toBeUndefined()
21+
expect(ProcessTree.plan({ platform: "linux", pid: undefined, running: true })).toBeUndefined()
22+
})
23+
24+
test("on macOS and Linux the child leads its own group; Windows gets no console window", () => {
25+
expect(ProcessTree.spawnOptions("linux").detached).toBe(true)
26+
expect(ProcessTree.spawnOptions("darwin").detached).toBe(true)
27+
expect(ProcessTree.spawnOptions("win32").detached).toBe(false)
28+
})
29+
})
30+
31+
const alive = (pid: number) => {
32+
try {
33+
process.kill(pid, 0)
34+
return true
35+
} catch {
36+
return false
37+
}
38+
}
39+
40+
describe("ProcessTree.stop", () => {
41+
// Runs the real path for this platform: taskkill on Windows, the group signal elsewhere.
42+
test("stops the child and the process it started", async () => {
43+
const script = `const g = Bun.spawn([process.execPath, "-e", "setTimeout(() => {}, 60000)"]); console.log(g.pid); setTimeout(() => {}, 60000)`
44+
const child = spawn(process.execPath, ["-e", script], {
45+
...ProcessTree.spawnOptions(),
46+
stdio: ["ignore", "pipe", "ignore"],
47+
})
48+
const grandchild = await new Promise<number>((done) =>
49+
child.stdout!.once("data", (data) => done(Number(data.toString().trim()))),
50+
)
51+
expect(alive(grandchild)).toBe(true)
52+
53+
await ProcessTree.stop(child, 5000)
54+
55+
expect(child.exitCode !== null || child.signalCode !== null).toBe(true)
56+
const deadline = Date.now() + 5000
57+
while (alive(grandchild) && Date.now() < deadline) await Bun.sleep(100)
58+
expect(alive(grandchild)).toBe(false)
59+
}, 30_000)
60+
})

0 commit comments

Comments
 (0)