From 2b4e8d9e691598e6ceb8aa9e9718163e469ff844 Mon Sep 17 00:00:00 2001 From: Victor Vogel Date: Mon, 9 Nov 2026 02:20:55 +0000 Subject: [PATCH 1/2] fix(util): keep stopping a process group after its leader exits A stop with forceKillAfter sent SIGTERM to the process group and escalated to SIGKILL only while the leader was still running. When the leader exited on SIGTERM, any child that outlived the signal kept running, and could keep starting more processes, with nothing tracking the group. After the first signal, wait for the whole group to empty. When the grace period ends, send SIGKILL to the group until it has no members, for at most one second. --- .../test/effect/cross-spawn-spawner.test.ts | 28 ++++++++++++++++++- packages/util/src/cross-spawn-spawner.ts | 14 ++++++++-- 2 files changed, 39 insertions(+), 3 deletions(-) diff --git a/packages/core/test/effect/cross-spawn-spawner.test.ts b/packages/core/test/effect/cross-spawn-spawner.test.ts index 08abaff60272..b21dc208c2a1 100644 --- a/packages/core/test/effect/cross-spawn-spawner.test.ts +++ b/packages/core/test/effect/cross-spawn-spawner.test.ts @@ -3,7 +3,7 @@ import fs from "node:fs/promises" import os from "node:os" import path from "node:path" import { inspect } from "node:util" -import { Cause, Deferred, Effect, Exit, PlatformError, Redacted, Stream } from "effect" +import { Cause, Deferred, Effect, Exit, Option, PlatformError, Redacted, Stream } from "effect" import { ChildProcess, ChildProcessSpawner } from "effect/process" import { CrossSpawnSpawner } from "@opencode/util/cross-spawn-spawner" import { LayerNode } from "@opencode/util/effect/layer-node" @@ -383,6 +383,32 @@ describe("cross-spawn spawner", () => { }), ) + fx.live( + "forceKillAfter empties the process group after its leader exits", + Effect.gen(function* () { + if (process.platform === "win32") return + + // The leader exits on SIGTERM. Its child ignores SIGTERM and starts one short sleep after another for about + // 10 seconds, so nothing is left behind even if the stop fails. + const handle = yield* ChildProcess.make( + "sh", + [ + "-c", + '(trap "" TERM; i=0; while [ $i -lt 200 ]; do sleep 0.05; i=$((i + 1)); done) >/dev/null 2>&1 & echo $!; wait', + ], + { stdin: "ignore" }, + ) + const group = -Number(handle.pid) + yield* Effect.addFinalizer(() => Effect.try(() => process.kill(group, "SIGKILL")).pipe(Effect.ignore)) + const child = Number(new TextDecoder().decode(Option.getOrThrow(yield* Stream.runHead(handle.stdout)))) + expect(alive(child)).toBe(true) + + yield* handle.kill({ forceKillAfter: 100 }) + + expect(alive(group)).toBe(false) + }), + ) + fx.effect( "isRunning reflects process state", Effect.gen(function* () { diff --git a/packages/util/src/cross-spawn-spawner.ts b/packages/util/src/cross-spawn-spawner.ts index 723d246000ae..07caeb652035 100644 --- a/packages/util/src/cross-spawn-spawner.ts +++ b/packages/util/src/cross-spawn-spawner.ts @@ -11,6 +11,7 @@ import { PlatformError, Predicate, Redacted, + Schedule, Sink, Stream, } from "effect" @@ -514,9 +515,18 @@ const makeCrossSpawnSpawner = Effect.gen(function* () { ) const attempt = terminate(opts?.killSignal ?? "SIGTERM") if (opts?.forceKillAfter === undefined) return attempt - return Effect.timeoutOrElse(attempt, { + // Descendants can outlive the leader in its group. A group ID stays reserved until its last member is reaped, so + // signalling the group until ESRCH stays on this group. + const grouped = globalThis.process.platform !== "win32" && (command.options.detached ?? true) + const untilGroupGone = (signal: NodeJS.Signals | 0) => + Effect.try(() => grouped && globalThis.process.kill(-proc.pid!, signal)).pipe( + Effect.orElseSucceed(() => false), + Effect.repeat({ while: Boolean, schedule: Schedule.spaced("10 millis") }), + ) + return Effect.timeoutOrElse(Effect.andThen(attempt, untilGroupGone(0)), { duration: opts.forceKillAfter, - orElse: () => terminate("SIGKILL"), + orElse: () => + terminate("SIGKILL").pipe(Effect.andThen(untilGroupGone("SIGKILL").pipe(Effect.timeoutOption("1 second")))), }) } From ad0abe31772ba218119302e3ce544240d2602c00 Mon Sep 17 00:00:00 2001 From: Victor Vogel Date: Mon, 9 Nov 2026 04:19:55 +0000 Subject: [PATCH 2/2] fix(util): don't wait for zombies when stopping a process group Waiting for kill(-pgid, 0) to fail never ends where nothing reaps orphans, such as opencode as PID 1 in its Docker image: zombies keep the group alive, so every stop with forceKillAfter took the grace period plus the 1 s SIGKILL loop. Once the leader has closed, send one SIGKILL to its group instead and return. Members that outlived SIGTERM still die, and a stop where every member exited on SIGTERM returns in milliseconds again. After the grace period, the SIGKILL path is the same as before this PR. The test now waits until the child is gone or a zombie (state Z in /proc//stat), so it also passes under a PID 1 that never reaps. Co-Authored-By: Claude Opus 5.5 --- .../test/effect/cross-spawn-spawner.test.ts | 26 ++++++++++++------- packages/util/src/cross-spawn-spawner.ts | 23 +++++++--------- 2 files changed, 25 insertions(+), 24 deletions(-) diff --git a/packages/core/test/effect/cross-spawn-spawner.test.ts b/packages/core/test/effect/cross-spawn-spawner.test.ts index b21dc208c2a1..0c0f68ea3e5a 100644 --- a/packages/core/test/effect/cross-spawn-spawner.test.ts +++ b/packages/core/test/effect/cross-spawn-spawner.test.ts @@ -10,6 +10,7 @@ import { LayerNode } from "@opencode/util/effect/layer-node" import { Teardown } from "@opencode/util/teardown" import { appExecutionAlias } from "../fixture/app-execution-alias" import { testEffect } from "../lib/effect" +import { waitUntil } from "../lib/eventually" const live = LayerNode.compile(CrossSpawnSpawner.node) const fx = testEffect(live) @@ -384,28 +385,33 @@ describe("cross-spawn spawner", () => { ) fx.live( - "forceKillAfter empties the process group after its leader exits", + "forceKillAfter kills the rest of the process group after its leader exits", Effect.gen(function* () { if (process.platform === "win32") return - // The leader exits on SIGTERM. Its child ignores SIGTERM and starts one short sleep after another for about - // 10 seconds, so nothing is left behind even if the stop fails. + // The leader exits on SIGTERM. Its child ignores SIGTERM and lets go of the output, so "close" doesn't wait + // for it. const handle = yield* ChildProcess.make( "sh", - [ - "-c", - '(trap "" TERM; i=0; while [ $i -lt 200 ]; do sleep 0.05; i=$((i + 1)); done) >/dev/null 2>&1 & echo $!; wait', - ], + ["-c", '(trap "" TERM; sleep 30) >/dev/null 2>&1 & echo $!; wait'], { stdin: "ignore" }, ) - const group = -Number(handle.pid) - yield* Effect.addFinalizer(() => Effect.try(() => process.kill(group, "SIGKILL")).pipe(Effect.ignore)) + yield* Effect.addFinalizer(() => + Effect.try(() => process.kill(-Number(handle.pid), "SIGKILL")).pipe(Effect.ignore), + ) const child = Number(new TextDecoder().decode(Option.getOrThrow(yield* Stream.runHead(handle.stdout)))) expect(alive(child)).toBe(true) yield* handle.kill({ forceKillAfter: 100 }) - expect(alive(group)).toBe(false) + // With no reaping init the killed child stays a zombie, which alive() still reports, so "Z" in /proc counts as + // stopped. macOS has no /proc but always reaps orphans. + yield* waitUntil( + Effect.tryPromise(() => fs.readFile(`/proc/${child}/stat`, "utf8")).pipe( + Effect.map((stat) => stat.charAt(stat.lastIndexOf(")") + 2) === "Z"), + Effect.orElseSucceed(() => !alive(child)), + ), + ) }), ) diff --git a/packages/util/src/cross-spawn-spawner.ts b/packages/util/src/cross-spawn-spawner.ts index 07caeb652035..46a4f961c757 100644 --- a/packages/util/src/cross-spawn-spawner.ts +++ b/packages/util/src/cross-spawn-spawner.ts @@ -11,7 +11,6 @@ import { PlatformError, Predicate, Redacted, - Schedule, Sink, Stream, } from "effect" @@ -515,19 +514,15 @@ const makeCrossSpawnSpawner = Effect.gen(function* () { ) const attempt = terminate(opts?.killSignal ?? "SIGTERM") if (opts?.forceKillAfter === undefined) return attempt - // Descendants can outlive the leader in its group. A group ID stays reserved until its last member is reaped, so - // signalling the group until ESRCH stays on this group. - const grouped = globalThis.process.platform !== "win32" && (command.options.detached ?? true) - const untilGroupGone = (signal: NodeJS.Signals | 0) => - Effect.try(() => grouped && globalThis.process.kill(-proc.pid!, signal)).pipe( - Effect.orElseSucceed(() => false), - Effect.repeat({ while: Boolean, schedule: Schedule.spaced("10 millis") }), - ) - return Effect.timeoutOrElse(Effect.andThen(attempt, untilGroupGone(0)), { - duration: opts.forceKillAfter, - orElse: () => - terminate("SIGKILL").pipe(Effect.andThen(untilGroupGone("SIGKILL").pipe(Effect.timeoutOption("1 second")))), - }) + // A descendant that doesn't hold the output pipes can outlive the leader in its group, and "close" doesn't wait for + // it. Once the leader has closed, kill what is left of the group. Waiting for the group to empty instead never ends + // where nothing reaps orphans (opencode as PID 1 in a container), since zombies keep the group alive. + return Effect.timeoutOrElse( + globalThis.process.platform !== "win32" && (command.options.detached ?? true) + ? Effect.andThen(attempt, Effect.ignore(killGroup(command, proc, "SIGKILL"))) + : attempt, + { duration: opts.forceKillAfter, orElse: () => terminate("SIGKILL") }, + ) } const source = (handle: ChildProcessHandle, from: ChildProcess.PipeFromOption | undefined) => {