Repository navigation
fix(cli): stop quietly when the stdout reader closes early - #3781
Conversation
`rea ... | head` crashed once head closed the pipe. TOON and YAML output
raised an unhandled EPIPE 'error' event with a Node stack trace. JSON
output reported {"code":"UNKNOWN","message":"write EPIPE"} on stderr.
Both exited 1, as if the analysis had failed.
The CLI entry point owns process.stdout, so it now treats EPIPE as the
reader declining the rest of the output. It stops writing, suppresses
the failure report and exits 141 like a SIGPIPE-terminated writer, also
when Incur exits through its own failure path. Other stdout errors are
handled as before.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
|
Codex usage limits have been reached for code reviews. Please check with the admins of this repo to increase the limits by adding credits. |
[Medium impact] Handles early pipe closure in the CLI runner. Add the SQLite capability check before merging so the new test works across supported Node versions. Findings
Fix with agent prompt### Issue 1
tests/boundary/cli/closedStdout.test.ts:54-56
This test runs on supported Node versions where SQLite inspection is unavailable, including Node 22 and early supported Node 24 releases. Without `DatabaseSync.limits`, the command returns a small unavailable result instead of the large row output. Closing the reader after that result cannot reliably trigger another write, so the test fails its exit-status assertion.
Gate this case with `SQLITE_NATIVE_LIMITS_AVAILABLE`, as the existing SQLite tests do, or use a large-output command available on every supported Node version.
---
For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.SummaryThe PR makes closed CLI stdout pipes exit quietly with status 141.
Reviews (2) · Last reviewed commit: "fix(cli): guard mcp doctor output and co..." · Reviewed by Greptile |
The closed-reader guard was only installed for ordinary CLI commands. As a result `rea mcp doctor | head` still crashed on EPIPE. The guard now lives in one entry-point function shared by both CLI branches; MCP stdio keeps its own lifecycle. The tests now also close the reader after the first chunk of a 4 MiB streamed JSON result, so later writes in writeJsonOutput are covered. Co-Authored-By: Claude Opus 5.5 <[email protected]>
| cliTest( | ||
| "stops quietly when the reader closes during streamed JSON", | ||
| async ({ processes }) => { |
There was a problem hiding this comment.
This test runs on supported Node versions where SQLite inspection is unavailable, including Node 22 and early supported Node 24 releases. Without DatabaseSync.limits, the command returns a small unavailable result instead of the large row output. Closing the reader after that result cannot reliably trigger another write, so the test fails its exit-status assertion.
Gate this case with SQLITE_NATIVE_LIMITS_AVAILABLE, as the existing SQLite tests do, or use a large-output command available on every supported Node version.
Prompt To Fix With AI
This is a comment left during a code review.
Path: tests/boundary/cli/closedStdout.test.ts
Line: 54-56
Comment:
**Tests fail on older Node**
This test runs on supported Node versions where SQLite inspection is unavailable, including Node 22 and early supported Node 24 releases. Without `DatabaseSync.limits`, the command returns a small unavailable result instead of the large row output. Closing the reader after that result cannot reliably trigger another write, so the test fails its exit-status assertion.
Gate this case with `SQLITE_NATIVE_LIMITS_AVAILABLE`, as the existing SQLite tests do, or use a large-output command available on every supported Node version.
---
For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.
Summary
rea <command> | headcrashed as soon as the reader closed the pipe. Agents and scripts pipe output intoheadall the time, and each such run produced a failure that looked like an analysis error:'error'event with a Node stack trace on stderr, exit1.--format json:{"code":"UNKNOWN","message":"write EPIPE"}on stderr, exit1.Now the CLI stops writing, prints nothing to stderr and exits
141. That is the statusgit log | headandyes | headreport when their reader closes the pipe.Problem and expected behavior
When the reader closes early it declines the rest of the output; the command itself has not failed.
process.stdouthad no'error'listener, so for Incur-rendered formats the EPIPE became an uncaught exception. On the JSON streaming path,writeOutputTextrejected with EPIPE. Incur then reported that rejection as a command failure, and the entry point sent the report to stderr.Change and scope
process.stdoutisscripts/rea.mjs. A sharedguardClosedStdout(), called by both the ordinary CLI branch andmcp doctor, listens for EPIPE on stdout and records that the stream is closed. After that it skips further stdout writes, drops the failure report that the write rejection would otherwise send to stderr, and sets exit status141.process.exit(code)on its failure path.servenow receives anexitoverride that keeps141when the pipe was closed.docs/cli.md"Output and exit status" documents the new status.incur's own--token-offsetpast the end, which prints a reversed range (showing tokens 100000–1251 of 1251). It is an upstream rendering issue.Contract and boundary impact
scripts/rea.mjs).141for a reader that closes early; schemas unchanged. MCP is unaffected.Expected effects:
Evidence and regression coverage
tests/boundary/cli/closedStdout.test.tsspawns the realscripts/rea.mjs. It coverscapabilities(TOON and JSON) andmcp doctor --jsonwith the reader closed before the first write. It also covers a 4 MiB streamedinspect-sqlite-database --format jsonresult whose reader closes after the first chunk. Each case asserts exit141and empty stderr.scripts/rea.mjs, all 4 cases fail withcode: 1and a stderr containingnode:events ... code: 'EPIPE'.reaexits141.Validation performed
npm run test:focused -- tests/boundary/cli/jsonOutput.test.ts tests/boundary/cli/cliOutput.test.ts tests/boundary/cli/closedStdout.test.ts tests/boundary/cli/nativePlistCli.test.ts: 71 passednpm run check:fast: passed;npm run docs:check: passedtrace-dylib-resolution /Applications/Ghostty.app(143 KB of JSON) was piped into a reader that closes after 10 bytes, for TOON, JSON and YAML. Each run gave exit141and empty stderr; before the fix they gave exit1with a stack trace or anUNKNOWNerror. A closed-before-first-write reader was also tested withcapabilities(TOON/JSON),inspect-plist /nonexistent --format jsonand--llms-full: exit141and empty stderr in each case. Unchanged: a normal failure (inspect-plist /nonexistent --format jsonto a terminal) still prints its structured error and exits1; a normal success exits0.Compatibility, safety, and release
1after a closed pipe as an analysis failure now sees141, which matches standard Unix tools.Author readiness
Human owner:
Before merge
type(scope): outcome.🤖 Generated with Claude Code