mirror of
https://github.com/anomalyco/opencode.git
synced 2026-11-19 00:03:13 +03:00
fix(shell): bound post-exit pipe draining on all platforms (#46085)
This commit is contained in:
committed by
GitHub
Unverified
parent
174d263890
commit
90fb6562ce
@@ -53,13 +53,12 @@ export const make = Effect.fnUntraced(function* (options: Options) {
|
||||
let closing: Promise<void> | undefined
|
||||
let trailingBytes = 0
|
||||
|
||||
const stop = (handle: ChildProcessHandle) =>
|
||||
Effect.gen(function* () {
|
||||
const exit = yield* Effect.timeoutOption(handle.exitCode, CLOSE_GRACE)
|
||||
if (exit._tag === "Some") return
|
||||
const terminated = yield* Effect.timeoutOption(handle.kill({ killSignal: "SIGTERM" }), FORCE_KILL_AFTER)
|
||||
if (terminated._tag === "None") yield* handle.kill({ killSignal: "SIGKILL" })
|
||||
}).pipe(Effect.ignore)
|
||||
const stop = Effect.fnUntraced(function* (handle: ChildProcessHandle) {
|
||||
// Exit completion can precede descendant cleanup after the capture deadline.
|
||||
yield* Effect.timeoutOption(handle.exitCode, CLOSE_GRACE).pipe(Effect.ignore)
|
||||
const terminated = yield* Effect.timeoutOption(handle.kill({ killSignal: "SIGTERM" }), FORCE_KILL_AFTER)
|
||||
if (terminated._tag === "None") yield* handle.kill({ killSignal: "SIGKILL" })
|
||||
}, Effect.ignore())
|
||||
|
||||
const close = () =>
|
||||
(closing ??= Effect.runPromise(
|
||||
|
||||
@@ -384,7 +384,11 @@ const layer = () =>
|
||||
command.timeoutFiber = runFork(
|
||||
Effect.sleep(Duration.millis(duration)).pipe(
|
||||
Effect.flatMap(() =>
|
||||
finish("timeout", undefined, handle.kill().pipe(Effect.catch(() => Effect.void))),
|
||||
finish(
|
||||
"timeout",
|
||||
undefined,
|
||||
handle.kill({ forceKillAfter: Duration.seconds(3) }).pipe(Effect.catch(() => Effect.void)),
|
||||
),
|
||||
),
|
||||
),
|
||||
)
|
||||
|
||||
@@ -2,7 +2,7 @@ import { describe, expect } from "bun:test"
|
||||
import fs from "node:fs/promises"
|
||||
import os from "node:os"
|
||||
import path from "node:path"
|
||||
import { Effect, Exit, PlatformError, Stream } from "effect"
|
||||
import { Deferred, Effect, Exit, PlatformError, Stream } from "effect"
|
||||
import { ChildProcess, ChildProcessSpawner } from "effect/unstable/process"
|
||||
import { CrossSpawnSpawner } from "@opencode-ai/util/cross-spawn-spawner"
|
||||
import { LayerNode } from "@opencode-ai/util/effect/layer-node"
|
||||
@@ -217,6 +217,52 @@ describe("cross-spawn spawner", () => {
|
||||
})
|
||||
|
||||
describe("process control", () => {
|
||||
for (const mode of ["exit", "SIGKILL"] as const) {
|
||||
const test = mode === "SIGKILL" && process.platform === "win32" ? fx.live.skip : fx.live
|
||||
test(
|
||||
`finishes capture after ${mode} while a grandchild holds stdio`,
|
||||
Effect.gen(function* () {
|
||||
const tmp = yield* Effect.acquireRelease(
|
||||
Effect.promise(() => tmpdir()),
|
||||
(tmp) => Effect.promise(() => tmp[Symbol.asyncDispose]()),
|
||||
)
|
||||
const pidFile = path.join(tmp.path, "child.pid")
|
||||
yield* Effect.addFinalizer(() =>
|
||||
Effect.tryPromise(async () => process.kill(Number(await fs.readFile(pidFile, "utf8")), "SIGKILL")).pipe(
|
||||
Effect.ignore,
|
||||
),
|
||||
)
|
||||
const ready = yield* Deferred.make<void>()
|
||||
// The grandchild keeps both pipes open independently of the foreground process.
|
||||
const handle = yield* ChildProcess.make(
|
||||
"node",
|
||||
[path.join(import.meta.dir, "../fixture/held-stdio.cjs"), mode, pidFile],
|
||||
{ stdin: "ignore", forceKillAfter: 100 },
|
||||
)
|
||||
const [exit, stdout, stderr] = yield* Effect.all(
|
||||
[
|
||||
Effect.exit(handle.exitCode),
|
||||
decodeByteStream(handle.stdout),
|
||||
decodeByteStream(handle.stderr.pipe(Stream.tap(() => Deferred.succeed(ready, undefined)))),
|
||||
mode === "SIGKILL"
|
||||
? Deferred.await(ready).pipe(Effect.andThen(handle.kill({ killSignal: "SIGKILL" })))
|
||||
: Effect.void,
|
||||
],
|
||||
{ concurrency: "unbounded" },
|
||||
).pipe(Effect.timeout("3 seconds"))
|
||||
|
||||
// Completion must retain foreground output without waiting for the inherited pipes to close.
|
||||
expect(stdout).toBe("foreground-out")
|
||||
expect(stderr).toBe("foreground-err")
|
||||
expect(Exit.isSuccess(exit)).toBe(mode === "exit")
|
||||
if (Exit.isSuccess(exit)) expect(exit.value).toBe(ChildProcessSpawner.ExitCode(0))
|
||||
expect(yield* handle.isRunning).toBe(false)
|
||||
expect(alive(Number(yield* Effect.promise(() => fs.readFile(pidFile, "utf8"))))).toBe(true)
|
||||
}),
|
||||
10_000,
|
||||
)
|
||||
}
|
||||
|
||||
fx.effect(
|
||||
"kills a running process",
|
||||
Effect.gen(function* () {
|
||||
@@ -245,7 +291,7 @@ describe("cross-spawn spawner", () => {
|
||||
}),
|
||||
)
|
||||
|
||||
fx.effect(
|
||||
fx.live(
|
||||
"forceKillAfter escalates for stubborn processes",
|
||||
Effect.gen(function* () {
|
||||
if (process.platform === "win32") return
|
||||
|
||||
@@ -0,0 +1,36 @@
|
||||
const { spawn } = require("node:child_process")
|
||||
const fs = require("node:fs")
|
||||
|
||||
const mode = process.argv[2]
|
||||
const child = spawn(
|
||||
process.execPath,
|
||||
[
|
||||
"-e",
|
||||
`
|
||||
process.on("SIGTERM", () => {})
|
||||
process.stdout.on("error", () => {})
|
||||
process.stderr.on("error", () => {})
|
||||
process.send("ready")
|
||||
setTimeout(() => process.exit(0), 15000)
|
||||
`,
|
||||
],
|
||||
{
|
||||
detached: mode !== "mcp",
|
||||
stdio: ["ignore", "inherit", "inherit", "ipc"],
|
||||
},
|
||||
)
|
||||
fs.writeFileSync(process.argv[3], String(child.pid))
|
||||
|
||||
child.once("message", () => {
|
||||
child.disconnect()
|
||||
child.unref()
|
||||
if (mode === "mcp") {
|
||||
fs.writeSync(1, JSON.stringify({ jsonrpc: "2.0", method: "ready" }) + "\n")
|
||||
process.stdin.resume().once("end", () => process.exit(0))
|
||||
return
|
||||
}
|
||||
fs.writeSync(1, "foreground-out\n")
|
||||
fs.writeSync(2, "foreground-err\n")
|
||||
if (mode === "exit") process.exit(0)
|
||||
setTimeout(() => process.exit(0), 15000)
|
||||
})
|
||||
@@ -1,4 +1,5 @@
|
||||
import path from "node:path"
|
||||
import fs from "node:fs/promises"
|
||||
import { describe, expect, test } from "bun:test"
|
||||
import { Client } from "@modelcontextprotocol/sdk/client/index.js"
|
||||
import { StreamableHTTPClientTransport } from "@modelcontextprotocol/sdk/client/streamableHttp.js"
|
||||
@@ -43,6 +44,7 @@ import { Image } from "@opencode-ai/core/image"
|
||||
import { testEffect } from "./lib/effect"
|
||||
import { imagePassthrough } from "./lib/image"
|
||||
import { location } from "./fixture/location"
|
||||
import { tmpdirScoped } from "./fixture/tmpdir"
|
||||
import { hostEnvironmentLayer, recordingEnvironmentLayer } from "./fixture/environment"
|
||||
import { executeTool, toolDefinitions, toolIdentity, waitForTool } from "./lib/tool"
|
||||
|
||||
@@ -652,6 +654,47 @@ test("joins concurrent stdio transport closes", async () => {
|
||||
)
|
||||
})
|
||||
|
||||
const testMcpDescendants =
|
||||
process.platform === "win32" ? testEffect(hostEnvironmentLayer).live.skip : testEffect(hostEnvironmentLayer).live
|
||||
testMcpDescendants(
|
||||
"terminates MCP descendants after the wrapper exits successfully",
|
||||
Effect.gen(function* () {
|
||||
const tmp = yield* tmpdirScoped()
|
||||
const pidFile = path.join(tmp.path, "child.pid")
|
||||
yield* Effect.addFinalizer(() =>
|
||||
Effect.tryPromise(async () => process.kill(Number(await fs.readFile(pidFile, "utf8")), "SIGKILL")).pipe(
|
||||
Effect.ignore,
|
||||
),
|
||||
)
|
||||
const ready = yield* Deferred.make<void>()
|
||||
// Stdin EOF exits the wrapper, but its ready descendant ignores SIGTERM and retains both pipes.
|
||||
const transport = yield* McpStdio.make({
|
||||
server: "held-stdio",
|
||||
command: "node",
|
||||
args: [path.join(import.meta.dir, "fixture/held-stdio.cjs"), "mcp", pidFile],
|
||||
cwd: tmp.path,
|
||||
environment: {},
|
||||
})
|
||||
transport.onmessage = () => {
|
||||
Deferred.doneUnsafe(ready, Exit.void)
|
||||
}
|
||||
yield* Effect.promise(() => transport.start())
|
||||
yield* Deferred.await(ready).pipe(Effect.timeout("3 seconds"))
|
||||
const pid = Number(yield* Effect.promise(() => fs.readFile(pidFile, "utf8")))
|
||||
|
||||
yield* Effect.promise(() => transport.close()).pipe(Effect.timeout("6 seconds"))
|
||||
|
||||
// close() must kill the descendant, not merely observe the wrapper's bounded exitCode.
|
||||
const stopped = yield* Effect.try(() => process.kill(pid, 0)).pipe(
|
||||
Effect.exit,
|
||||
Effect.repeat({ while: Exit.isSuccess, schedule: Schedule.spaced("25 millis") }),
|
||||
Effect.timeout("2 seconds"),
|
||||
)
|
||||
expect(Exit.isFailure(stopped)).toBe(true)
|
||||
}),
|
||||
15_000,
|
||||
)
|
||||
|
||||
test("closes a stdio process that finishes spawning after close", async () => {
|
||||
const spawning = Deferred.makeUnsafe<void>()
|
||||
const release = Deferred.makeUnsafe<void>()
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import fs from "fs/promises"
|
||||
import { realpathSync } from "node:fs"
|
||||
import { realpathSync, watch } from "node:fs"
|
||||
import os from "os"
|
||||
import path from "path"
|
||||
import { describe, expect } from "bun:test"
|
||||
@@ -39,7 +39,7 @@ import { Shell as ShellSchema } from "@opencode-ai/schema/shell"
|
||||
import { ShellTool } from "@opencode-ai/core/tool/plugin/shell"
|
||||
import { ToolOutput } from "@opencode-ai/core/tool-output"
|
||||
import { Tool } from "@opencode-ai/core/tool"
|
||||
import { tmpdir } from "./fixture/tmpdir"
|
||||
import { tmpdir, tmpdirScoped } from "./fixture/tmpdir"
|
||||
import { tempGlobalLayer } from "./fixture/global"
|
||||
import { testEffect } from "./lib/effect"
|
||||
import { permissionLayer } from "./lib/permission"
|
||||
@@ -1510,6 +1510,55 @@ describe("ShellTool", () => {
|
||||
{ timeout: 15_000 },
|
||||
)
|
||||
|
||||
const signalTest = isWindows ? it.live.skip : it.live
|
||||
signalTest(
|
||||
"escalates a shell timeout when the ready process ignores SIGTERM",
|
||||
() =>
|
||||
Effect.gen(function* () {
|
||||
const tmp = yield* tmpdirScoped()
|
||||
const ready = yield* Deferred.make<void>()
|
||||
yield* Effect.acquireRelease(
|
||||
Effect.sync(() =>
|
||||
watch(tmp.path, (_event, filename) => {
|
||||
if (filename === "ready") Deferred.doneUnsafe(ready, Exit.void)
|
||||
}),
|
||||
),
|
||||
(watcher) => Effect.sync(() => watcher.close()),
|
||||
)
|
||||
yield* withSession(tmp.path, () =>
|
||||
Effect.gen(function* () {
|
||||
const shell = yield* Shell.Service
|
||||
yield* Effect.acquireUseRelease(
|
||||
// Arm the timeout only after the child announces that SIGTERM is ignored.
|
||||
shell.create({
|
||||
shell: "/bin/sh",
|
||||
command: "trap '' TERM; : > ready; while :; do sleep 60; done",
|
||||
timeout: 0,
|
||||
}),
|
||||
(info) =>
|
||||
Effect.gen(function* () {
|
||||
yield* Deferred.await(ready).pipe(Effect.timeout("5 seconds"))
|
||||
yield* shell.timeout(info.id, 1)
|
||||
// Completion must reap the process, not only mark the command as timed out.
|
||||
expect((yield* shell.wait(info.id).pipe(Effect.timeout("10 seconds"))).status).toBe("timeout")
|
||||
const pid = info.pid
|
||||
if (pid === undefined) throw new Error("Expected shell PID")
|
||||
expect(() => process.kill(pid, 0)).toThrow()
|
||||
}),
|
||||
(info) =>
|
||||
Effect.try(() => {
|
||||
if (info.pid !== undefined) process.kill(-info.pid, "SIGKILL")
|
||||
}).pipe(
|
||||
Effect.catch(() => Effect.void),
|
||||
Effect.andThen(shell.remove(info.id)),
|
||||
),
|
||||
)
|
||||
}),
|
||||
)
|
||||
}),
|
||||
{ timeout: 30_000 },
|
||||
)
|
||||
|
||||
it.live("does not retain removed running shells in exit order", () =>
|
||||
Effect.acquireUseRelease(
|
||||
Effect.promise(() => tmpdir()),
|
||||
|
||||
@@ -86,6 +86,7 @@ const toPlatformError = (
|
||||
}
|
||||
|
||||
type ExitSignal = Deferred.Deferred<readonly [code: number | null, signal: NodeJS.Signals | null]>
|
||||
type Spawned = readonly [process: NodeChildProcess.ChildProcess, closed: ExitSignal, exited: ExitSignal]
|
||||
|
||||
const makeCrossSpawnSpawner = Effect.gen(function* () {
|
||||
const fs = yield* FileSystem.FileSystem
|
||||
@@ -235,20 +236,28 @@ const makeCrossSpawnSpawner = Effect.gen(function* () {
|
||||
proc: NodeChildProcess.ChildProcess,
|
||||
out: ChildProcess.StdoutConfig,
|
||||
err: ChildProcess.StderrConfig,
|
||||
stopOutput: Deferred.Deferred<void>,
|
||||
) => {
|
||||
let stdout = proc.stdout
|
||||
? NodeStream.fromReadable({
|
||||
evaluate: () => proc.stdout!,
|
||||
onError: (cause) => toPlatformError("fromReadable(stdout)", toError(cause), command),
|
||||
})
|
||||
: Stream.empty
|
||||
let stderr = proc.stderr
|
||||
? NodeStream.fromReadable({
|
||||
evaluate: () => proc.stderr!,
|
||||
onError: (cause) => toPlatformError("fromReadable(stderr)", toError(cause), command),
|
||||
})
|
||||
: Stream.empty
|
||||
const capture = (readable: NodeChildProcess.ChildProcess["stdout"], name: string) => {
|
||||
if (!readable) return Stream.empty
|
||||
return NodeStream.fromReadable({
|
||||
evaluate: () => readable,
|
||||
onError: (cause) => toPlatformError(`fromReadable(${name})`, toError(cause), command),
|
||||
closeOnDone: false,
|
||||
}).pipe(
|
||||
Stream.interruptWhen(Deferred.await(stopOutput)),
|
||||
Stream.ensuring(
|
||||
Effect.gen(function* () {
|
||||
// Only the capture deadline transfers the reader back to the process scope.
|
||||
if (yield* Deferred.isDone(stopOutput)) return
|
||||
readable.destroy()
|
||||
}),
|
||||
),
|
||||
)
|
||||
}
|
||||
|
||||
let stdout = capture(proc.stdout, "stdout")
|
||||
let stderr = capture(proc.stderr, "stderr")
|
||||
if (Sink.isSink(out.stream)) stdout = Stream.transduce(stdout, out.stream)
|
||||
if (Sink.isSink(err.stream)) stderr = Stream.transduce(stderr, err.stream)
|
||||
|
||||
@@ -256,8 +265,9 @@ const makeCrossSpawnSpawner = Effect.gen(function* () {
|
||||
}
|
||||
|
||||
const launchProcess = (command: ChildProcess.StandardCommand, opts: NodeChildProcess.SpawnOptions) =>
|
||||
Effect.callback<readonly [NodeChildProcess.ChildProcess, ExitSignal], PlatformError.PlatformError>((resume) => {
|
||||
const signal = Deferred.makeUnsafe<readonly [code: number | null, signal: NodeJS.Signals | null]>()
|
||||
Effect.callback<Spawned, PlatformError.PlatformError>((resume) => {
|
||||
const closed = Deferred.makeUnsafe<readonly [code: number | null, signal: NodeJS.Signals | null]>()
|
||||
const exited = Deferred.makeUnsafe<readonly [code: number | null, signal: NodeJS.Signals | null]>()
|
||||
const proc = launch(command.command, command.args, opts)
|
||||
let end = false
|
||||
let exit: readonly [code: number | null, signal: NodeJS.Signals | null] | undefined
|
||||
@@ -266,14 +276,16 @@ const makeCrossSpawnSpawner = Effect.gen(function* () {
|
||||
})
|
||||
proc.on("exit", (...args) => {
|
||||
exit = args
|
||||
Deferred.doneUnsafe(exited, Exit.succeed(args))
|
||||
})
|
||||
proc.on("close", (...args) => {
|
||||
if (end) return
|
||||
end = true
|
||||
Deferred.doneUnsafe(signal, Exit.succeed(exit ?? args))
|
||||
Deferred.doneUnsafe(exited, Exit.succeed(exit ?? args))
|
||||
Deferred.doneUnsafe(closed, Exit.succeed(exit ?? args))
|
||||
})
|
||||
proc.on("spawn", () => {
|
||||
resume(Effect.succeed([proc, signal]))
|
||||
resume(Effect.succeed([proc, closed, exited]))
|
||||
})
|
||||
return Effect.sync(() => {
|
||||
proc.kill("SIGTERM")
|
||||
@@ -285,9 +297,37 @@ const makeCrossSpawnSpawner = Effect.gen(function* () {
|
||||
opts: NodeChildProcess.SpawnOptions,
|
||||
) {
|
||||
yield* Effect.logInfo("spawning process", { command: command.command, args: command.args, cwd: opts.cwd })
|
||||
return yield* launchProcess(command, opts)
|
||||
const [proc, closed, exited] = yield* launchProcess(command, opts)
|
||||
const stopOutput = yield* Deferred.make<void>()
|
||||
// Register before process release so the deadline remains active during termination.
|
||||
yield* Effect.forkScoped(
|
||||
Effect.gen(function* () {
|
||||
yield* Deferred.await(exited)
|
||||
// Particularly on Windows, some shell calls will cause detached descendants. Inherited stdio can hang the call.
|
||||
// Almost every ecosystem has tried to fix this; there's no single "good" answer here.
|
||||
// Calls that trigger this are e.g. dotnet build with warmed MSBuild processes, agent-browser, etc.
|
||||
// One shared deadline covers stdout and stderr, then the output pump finishes its file.
|
||||
if ((yield* Effect.timeoutOption(Deferred.await(closed), "1 second"))._tag === "Some") return
|
||||
yield* Deferred.succeed(stopOutput, undefined)
|
||||
discard(proc.stdout)
|
||||
discard(proc.stderr)
|
||||
}),
|
||||
)
|
||||
return [proc, closed, exited, stopOutput] as const
|
||||
})
|
||||
|
||||
const discard = (readable: NodeChildProcess.ChildProcess["stdout"]) => {
|
||||
if (!readable || readable.destroyed) return
|
||||
// read() also drains while a backpressured Effect adapter still has a readable listener.
|
||||
const drain = () => {
|
||||
while (readable.read() !== null) {}
|
||||
}
|
||||
readable.on("readable", drain)
|
||||
readable.once("error", () => {})
|
||||
readable.once("close", () => readable.off("readable", drain))
|
||||
drain()
|
||||
}
|
||||
|
||||
const killGroup = (
|
||||
command: ChildProcess.StandardCommand,
|
||||
proc: NodeChildProcess.ChildProcess,
|
||||
@@ -323,40 +363,27 @@ const makeCrossSpawnSpawner = Effect.gen(function* () {
|
||||
const stop = (
|
||||
command: ChildProcess.StandardCommand,
|
||||
proc: NodeChildProcess.ChildProcess,
|
||||
exit: ExitSignal,
|
||||
closed: ExitSignal,
|
||||
stopOutput: Deferred.Deferred<void>,
|
||||
opts: ChildProcess.KillOptions | undefined,
|
||||
) => {
|
||||
const send = (signal: NodeJS.Signals) =>
|
||||
Effect.catch(killGroup(command, proc, signal), () => killOne(command, proc, signal))
|
||||
const attempt = send(opts?.killSignal ?? "SIGTERM").pipe(Effect.andThen(Deferred.await(exit)), Effect.asVoid)
|
||||
if (!opts?.forceKillAfter) return attempt
|
||||
const terminate = (signal: NodeJS.Signals) =>
|
||||
Effect.catch(killGroup(command, proc, signal), () => killOne(command, proc, signal)).pipe(
|
||||
Effect.andThen(
|
||||
signal === "SIGKILL"
|
||||
? Effect.raceFirst(Deferred.await(closed), Deferred.await(stopOutput))
|
||||
: Deferred.await(closed),
|
||||
),
|
||||
Effect.asVoid,
|
||||
)
|
||||
const attempt = terminate(opts?.killSignal ?? "SIGTERM")
|
||||
if (opts?.forceKillAfter === undefined) return attempt
|
||||
return Effect.timeoutOrElse(attempt, {
|
||||
duration: opts.forceKillAfter,
|
||||
orElse: () => send("SIGKILL").pipe(Effect.andThen(Deferred.await(exit)), Effect.asVoid),
|
||||
orElse: () => terminate("SIGKILL"),
|
||||
})
|
||||
}
|
||||
|
||||
const timeout =
|
||||
(
|
||||
proc: NodeChildProcess.ChildProcess,
|
||||
command: ChildProcess.StandardCommand,
|
||||
opts: ChildProcess.KillOptions | undefined,
|
||||
) =>
|
||||
<A, E, R>(
|
||||
f: (
|
||||
command: ChildProcess.StandardCommand,
|
||||
proc: NodeChildProcess.ChildProcess,
|
||||
signal: NodeJS.Signals,
|
||||
) => Effect.Effect<A, E, R>,
|
||||
) => {
|
||||
const signal = opts?.killSignal ?? "SIGTERM"
|
||||
if (Predicate.isUndefined(opts?.forceKillAfter)) return f(command, proc, signal)
|
||||
return Effect.timeoutOrElse(f(command, proc, signal), {
|
||||
duration: opts.forceKillAfter,
|
||||
orElse: () => f(command, proc, "SIGKILL"),
|
||||
})
|
||||
}
|
||||
|
||||
const source = (handle: ChildProcessHandle, from: ChildProcess.PipeFromOption | undefined) => {
|
||||
const opt = from ?? "stdout"
|
||||
switch (opt) {
|
||||
@@ -385,7 +412,7 @@ const makeCrossSpawnSpawner = Effect.gen(function* () {
|
||||
const extra = fds(command.options)
|
||||
const dir = yield* cwd(command.options)
|
||||
|
||||
const [proc, signal] = yield* Effect.acquireRelease(
|
||||
const [proc, closed, exited, stopOutput] = yield* Effect.acquireRelease(
|
||||
spawn(command, {
|
||||
cwd: dir,
|
||||
env: env(command.options),
|
||||
@@ -394,21 +421,38 @@ const makeCrossSpawnSpawner = Effect.gen(function* () {
|
||||
shell: command.options.shell,
|
||||
windowsHide: process.platform === "win32",
|
||||
}),
|
||||
Effect.fnUntraced(function* ([proc, signal]) {
|
||||
const done = yield* Deferred.isDone(signal)
|
||||
const kill = timeout(proc, command, command.options)
|
||||
if (done) {
|
||||
const [code] = yield* Deferred.await(signal)
|
||||
if (process.platform === "win32") return yield* Effect.void
|
||||
if (code !== 0 && Predicate.isNotNull(code)) return yield* Effect.ignore(kill(killGroup))
|
||||
return yield* Effect.void
|
||||
}
|
||||
return yield* Effect.ignore(stop(command, proc, signal, command.options))
|
||||
}),
|
||||
Effect.fnUntraced(
|
||||
function* ([proc, closed, exited, stopOutput]) {
|
||||
const done = (yield* Deferred.isDone(closed)) || (yield* Deferred.isDone(stopOutput))
|
||||
if (done) {
|
||||
const [code] = yield* Deferred.await(exited)
|
||||
if (process.platform === "win32") return
|
||||
if (code === 0 || Predicate.isNull(code)) return
|
||||
if (command.options.forceKillAfter === undefined) {
|
||||
return yield* Effect.ignore(killGroup(command, proc, command.options.killSignal ?? "SIGTERM"))
|
||||
}
|
||||
}
|
||||
yield* Effect.ignore(stop(command, proc, closed, stopOutput, command.options))
|
||||
},
|
||||
(effect, [proc, , , stopOutput]) =>
|
||||
effect.pipe(
|
||||
Effect.ensuring(
|
||||
Effect.gen(function* () {
|
||||
yield* Deferred.succeed(stopOutput, undefined)
|
||||
proc.stdout?.destroy()
|
||||
proc.stderr?.destroy()
|
||||
}),
|
||||
),
|
||||
),
|
||||
),
|
||||
)
|
||||
|
||||
const completion = Effect.raceFirst(
|
||||
Deferred.await(closed),
|
||||
Deferred.await(stopOutput).pipe(Effect.andThen(Deferred.await(exited))),
|
||||
)
|
||||
const fd = yield* setupFds(command, proc, extra)
|
||||
const out = setupOutput(command, proc, sout, serr)
|
||||
const out = setupOutput(command, proc, sout, serr, stopOutput)
|
||||
let ref = true
|
||||
return makeHandle({
|
||||
pid: ProcessId(proc.pid!),
|
||||
@@ -418,8 +462,10 @@ const makeCrossSpawnSpawner = Effect.gen(function* () {
|
||||
all: out.all,
|
||||
getInputFd: fd.getInputFd,
|
||||
getOutputFd: fd.getOutputFd,
|
||||
isRunning: Effect.map(Deferred.isDone(signal), (done) => !done),
|
||||
exitCode: Effect.flatMap(Deferred.await(signal), ([code, signal]) => {
|
||||
isRunning: Effect.gen(function* () {
|
||||
return !(yield* Deferred.isDone(closed)) && !(yield* Deferred.isDone(stopOutput))
|
||||
}),
|
||||
exitCode: Effect.flatMap(completion, ([code, signal]) => {
|
||||
if (Predicate.isNotNull(code)) return Effect.succeed(ExitCode(code))
|
||||
return Effect.fail(
|
||||
toPlatformError(
|
||||
@@ -429,7 +475,7 @@ const makeCrossSpawnSpawner = Effect.gen(function* () {
|
||||
),
|
||||
)
|
||||
}),
|
||||
kill: (opts?: ChildProcess.KillOptions) => stop(command, proc, signal, opts),
|
||||
kill: (opts?: ChildProcess.KillOptions) => stop(command, proc, closed, stopOutput, opts),
|
||||
unref: Effect.sync(() => {
|
||||
if (ref) {
|
||||
proc.unref()
|
||||
|
||||
Reference in New Issue
Block a user