Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 33 additions & 1 deletion packages/core/test/effect/cross-spawn-spawner.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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* () {
Expand Down
13 changes: 9 additions & 4 deletions packages/util/src/cross-spawn-spawner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down
Loading