diff --git a/packages/core/test/effect/cross-spawn-spawner.test.ts b/packages/core/test/effect/cross-spawn-spawner.test.ts index 08abaff60272..0c0f68ea3e5a 100644 --- a/packages/core/test/effect/cross-spawn-spawner.test.ts +++ b/packages/core/test/effect/cross-spawn-spawner.test.ts @@ -3,13 +3,14 @@ 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" 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) @@ -383,6 +384,37 @@ describe("cross-spawn spawner", () => { }), ) + fx.live( + "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 lets go of the output, so "close" doesn't wait + // for it. + const handle = yield* ChildProcess.make( + "sh", + ["-c", '(trap "" TERM; sleep 30) >/dev/null 2>&1 & echo $!; wait'], + { stdin: "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 }) + + // 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)), + ), + ) + }), + ) + 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..46a4f961c757 100644 --- a/packages/util/src/cross-spawn-spawner.ts +++ b/packages/util/src/cross-spawn-spawner.ts @@ -514,10 +514,15 @@ const makeCrossSpawnSpawner = Effect.gen(function* () { ) const attempt = terminate(opts?.killSignal ?? "SIGTERM") if (opts?.forceKillAfter === undefined) return attempt - return Effect.timeoutOrElse(attempt, { - duration: opts.forceKillAfter, - orElse: () => terminate("SIGKILL"), - }) + // 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) => {