Skip to content

Commit 13c9c38

Browse files
committed
fix(shell): drain stdout before reading output to avoid "(no output)" on exit 0
The output reader was forked with forkScoped but only handle.exitCode was awaited, so the scope could close and interrupt the reader before buffered stdout/stderr drained to EOF, yielding "(no output)" on a clean exit 0 (anomalyco#35511). Capture the reader fiber and, on a clean exit, join it so the merged stream drains before the scope closes. Bound the best-effort drain with the abort/timeout races so a backgrounded child holding the pipe open cannot hang the tool; a clean exit is never relabelled aborted/terminated if the drain is cut short.
1 parent 3a149ba commit 13c9c38

2 files changed

Lines changed: 40 additions & 2 deletions

File tree

‎packages/opencode/src/tool/shell.ts‎

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Effect, Stream } from "effect"
1+
import { Effect, Fiber, Stream } from "effect"
22
import os from "os"
33
import { createWriteStream } from "node:fs"
44
import * as Tool from "./tool"
@@ -483,7 +483,7 @@ export const ShellTool = Tool.define(
483483
yield* Effect.addFinalizer(closeSink)
484484
const handle = yield* spawner.spawn(cmd(input.shell, input.command, input.cwd, input.env))
485485

486-
yield* Effect.forkScoped(
486+
const reader = yield* Effect.forkScoped(
487487
Stream.runForEach(Stream.decodeText(handle.all), (chunk) => {
488488
const size = Buffer.byteLength(chunk, "utf-8")
489489
list.push({ text: chunk, size })
@@ -545,6 +545,21 @@ export const ShellTool = Tool.define(
545545
timeout.pipe(Effect.map(() => ({ kind: "timeout" as const, code: null }))),
546546
])
547547

548+
if (exit.kind === "exit") {
549+
// On a clean exit, join the reader fiber so the merged stdout/stderr
550+
// stream drains to EOF before the scope closes and interrupts it.
551+
// Without this, buffered output can be lost, yielding "(no output)"
552+
// on exit 0 (#35511).
553+
//
554+
// A backgrounded child can keep the stdout/stderr write end open past
555+
// the parent's exit, so the stream may never reach EOF. Bound the
556+
// best-effort drain with abort/timeout so it can't hang forever (the
557+
// reader fiber is interrupted on scope close). The process already
558+
// exited cleanly here, so we do NOT set aborted/expired if the drain
559+
// is cut short — that would mislabel a successful command as
560+
// terminated/aborted despite its clean exit code.
561+
yield* Fiber.join(reader).pipe(Effect.race(abort), Effect.race(timeout))
562+
}
548563
if (exit.kind === "abort") {
549564
aborted = true
550565
yield* handle.kill({ forceKillAfter: "3 seconds" }).pipe(Effect.orDie)

‎packages/opencode/test/tool/shell.test.ts‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,29 @@ describe("tool.shell", () => {
194194
),
195195
)
196196

197+
// Regression for #35511: the merged stdout/stderr reader must drain to EOF
198+
// before output is assembled, otherwise buffered output is lost and the tool
199+
// returns "(no output)" on exit 0. Loop to catch the race.
200+
each("drains stdout before reading output (#35511)", () =>
201+
runIn(
202+
projectRoot,
203+
Effect.gen(function* () {
204+
for (let i = 0; i < 10; i++) {
205+
const result = yield* run({
206+
command: "echo hi",
207+
})
208+
expect(result.metadata.exit).toBe(0)
209+
expect(result.output).not.toBe("(no output)")
210+
expect(result.output).toContain("hi")
211+
// metadata.output (the streaming `last` preview) is populated from the
212+
// same drained list, so it must also carry the output, not "(no output)".
213+
expect(result.metadata.output).not.toBe("(no output)")
214+
expect(result.metadata.output).toContain("hi")
215+
}
216+
}),
217+
),
218+
)
219+
197220
it.live("falls back from terminal-only configured shell", () =>
198221
Effect.gen(function* () {
199222
const tmp = yield* tmpdirScoped({ config: { shell: "fish" } })

0 commit comments

Comments
 (0)