Skip to content

Commit 64a33e3

Browse files
committed
fix(cli): exit cleanly on closed stdout (EPIPE) instead of spinning
When the reader on the far end of stdout/stderr closes the pipe (e.g. `opencode run --format json | head`), the CLI process kept generating output to a dead sink: the run event loop, SSE heartbeats and log writes continued firing, and the write failures surfaced as EPIPE stream errors with no listener (or were swallowed by the worker's no-op uncaughtException handler), so the process never exited and spun at 100% CPU. Install an `error` listener on process.stdout/stderr at the CLI entry point that exits 0 on EPIPE (closed pipe) and rethrows every other error, so real stream failures still surface and normal-exit behavior is unchanged. Fixes anomalyco#33653
1 parent 1af4d5d commit 64a33e3

1 file changed

Lines changed: 10 additions & 0 deletions

File tree

‎packages/opencode/src/index.ts‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,16 @@ import { errorMessage } from "./util/error"
3030
import { PluginCommand } from "./cli/cmd/plug"
3131
import { Heap } from "./cli/heap"
3232

33+
// Exit cleanly if the output pipe is closed (e.g. `opencode ... | head`)
34+
// instead of spinning by writing to a dead sink. Only EPIPE/closed-pipe is
35+
// handled; any other stream error is rethrown so real failures still surface.
36+
for (const stream of [process.stdout, process.stderr]) {
37+
stream.on("error", (err: NodeJS.ErrnoException) => {
38+
if (err.code === "EPIPE") process.exit(0)
39+
throw err
40+
})
41+
}
42+
3343
const args = hideBin(process.argv)
3444

3545
function show(out: string) {

0 commit comments

Comments
 (0)