Skip to content

fix(cli): exit quietly when --completion's reader closes early - #2700

Merged
cliffhall merged 1 commit into
v2/mainfrom
v2/fix/2646-completion-epipe
Oct 12, 2026
Merged

cliffhall merged 1 commit into
v2/mainfrom
v2/fix/2646-completion-epipe

Conversation

@cliffhall

Copy link
Copy Markdown
Member

Closes #2646

What

mcp-inspector --cli --completion <shell> wrote its script through awaitableLog, which attaches no stdout 'error' listener. When the reader closed the pipe first, Node raised an unhandled 'error' event (write EPIPE) and printed a stack trace. Both | head and bash 3.2's source <(…) close the pipe early.

Change

  • clients/cli/src/completion.ts: the script is now written by writeCompletion, which attaches a once('error') listener for the duration of the write. EPIPE resolves as a normal end. Any other stdout error rejects into the CLI's error path. The listener stays attached after a failed write because Node calls the write callback first and emits 'error' afterwards; since it is a once, that emit removes it.
  • clients/cli/src/handlers/consume-outcome.ts: exports the existing isBrokenPipe so both paths use the same EPIPE predicate.

Tests

  • completion.test.ts: EPIPE resolves and leaves no listener behind; a non-EPIPE error rejects; a successful write detaches its listener.
  • e2e.test.ts: spawns the built binary with its stdout read end already closed and asserts exit 0 with empty stderr. Before the fix this test failed with Error: write EPIPE; after the fix it passes.
  • The issue's repro (node clients/cli/build/index.js --completion bash | head -1) now prints the first line and nothing else.
  • npm run local:gate is green.

🤖 Generated with Claude Code

`mcp-inspector --cli --completion <shell>` wrote its script through
awaitableLog with no stdout 'error' listener, so a reader that closed the
pipe first (`| head`, bash 3.2's `source <(…)`) crashed the CLI with an
unhandled `write EPIPE`. The script write now treats EPIPE as a normal end
and rejects any other stdout error into the CLI's error path, reusing the
consume-outcome stream's isBrokenPipe.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: cliffhall <[email protected]>
@cliffhall cliffhall added the v2 Issues and PRs for v2 label Oct 12, 2026
@cliffhall
cliffhall requested a balanced review from Copilot October 12, 2026 19:16

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

The focused fix correctly handles write errors and includes comprehensive regression coverage.

0 open findings

What changed in this PR

Fixes CLI completion output so closed pipes exit quietly while other stdout failures follow the normal error path.

Changes:

  • Adds EPIPE-aware completion output handling.
  • Reuses the existing broken-pipe predicate.
  • Adds unit and subprocess regression coverage.
File Description
clients/​cli/​src/​handlers/​consume-outcome.ts Exports the shared EPIPE predicate.
clients/​cli/​src/​completion.ts Handles stdout errors during completion writes.
clients/​cli/​__tests__/​completion.test.ts Tests EPIPE, other errors, and listener cleanup.
clients/​cli/​__tests__/​e2e.test.ts Verifies quiet exit with closed stdout.

🧠 Review effort: Balanced


💡 Add a code-review agent skill for context-aware, tailored reviews. Learn more in the docs.

@cliffhall

Copy link
Copy Markdown
Member Author

Copilot review loop closed: round 1 came back clean (0 open findings, no inline comments, nothing suppressed), so no further round was requested.

@cliffhall
cliffhall merged commit cbf3f52 into v2/main Oct 12, 2026
8 checks passed
@cliffhall
cliffhall deleted the v2/fix/2646-completion-epipe branch October 12, 2026 20:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

v2 Issues and PRs for v2

Projects

None yet

Development

Successfully merging this pull request may close these issues.

CLI --completion crashes on an unhandled EPIPE when its reader closes early

2 participants