fix(shell): preserve output from fast-exiting commands

Capture child stdout and stderr eagerly before lazy Effect readers attach. Preserve the bounded post-exit drain and process cleanup policies, with delayed-consumption and backpressure regressions.
This commit is contained in:
Kit Langton
2026-08-31 19:52:08 -04:00
committed by GitHub
Unverified
parent 5b2276666f
commit a6b49b3f74
2 changed files with 45 additions and 3 deletions
@@ -147,6 +147,20 @@ describe("cross-spawn spawner", () => {
})
describe("stderr", () => {
fx.live(
"captures both streams across backpressure",
Effect.gen(function* () {
const handle = yield* js(
'process.stdout.write("o".repeat(256 * 1024)); process.stderr.write("e".repeat(256 * 1024))',
)
const output = yield* Effect.all([decodeByteStream(handle.stdout), decodeByteStream(handle.stderr)], {
concurrency: "unbounded",
})
expect(output).toEqual(["o".repeat(256 * 1024), "e".repeat(256 * 1024)])
expect(yield* handle.exitCode).toBe(ChildProcessSpawner.ExitCode(0))
}),
)
fx.effect(
"captures stderr output",
Effect.gen(function* () {
@@ -199,6 +213,30 @@ describe("cross-spawn spawner", () => {
)
})
describe("delayed output consumption", () => {
for (const combined of [false, true]) {
fx.live(
`retains ${combined ? "combined" : "separate"} output after process completion`,
Effect.gen(function* () {
const handle = yield* js(
'require("node:fs").writeSync(1, "stdout\\n"); require("node:fs").writeSync(2, "stderr\\n")',
)
expect(yield* handle.exitCode).toBe(ChildProcessSpawner.ExitCode(0))
if (combined) {
const output = yield* decodeByteStream(handle.all)
expect(output).toContain("stdout")
expect(output).toContain("stderr")
return
}
const output = yield* Effect.all([decodeByteStream(handle.stdout), decodeByteStream(handle.stderr)], {
concurrency: "unbounded",
})
expect(output).toEqual(["stdout", "stderr"])
}),
)
}
})
describe("stdin", () => {
fx.effect(
"allows providing standard input to a command",
+7 -3
View File
@@ -240,10 +240,13 @@ const makeCrossSpawnSpawner = Effect.gen(function* () {
) => {
const capture = (readable: NodeChildProcess.ChildProcess["stdout"], name: string) => {
if (!readable) return Stream.empty
// Bun resumes stdio on exit; retain bytes before the lazy Effect reader attaches.
const buffer = new PassThrough()
readable.on("error", (cause) => buffer.destroy(toError(cause)))
readable.pipe(buffer)
return NodeStream.fromReadable({
evaluate: () => readable,
evaluate: () => buffer,
onError: (cause) => toPlatformError(`fromReadable(${name})`, toError(cause), command),
closeOnDone: false,
}).pipe(
Stream.interruptWhen(Deferred.await(stopOutput)),
Stream.ensuring(
@@ -318,7 +321,8 @@ const makeCrossSpawnSpawner = Effect.gen(function* () {
const discard = (readable: NodeChildProcess.ChildProcess["stdout"]) => {
if (!readable || readable.destroyed) return
// read() also drains while a backpressured Effect adapter still has a readable listener.
readable.unpipe()
// Discard descendant output without refilling a capture buffer that is no longer consumed.
const drain = () => {
while (readable.read() !== null) {}
}