Skip to content

Comments

fix(stdio): handle EPIPE errors gracefully in StdioServerTransport#1566

Open
opspawn wants to merge 2 commits intomodelcontextprotocol:mainfrom
opspawn:fix/1564-epipe-crash-handler
Open

fix(stdio): handle EPIPE errors gracefully in StdioServerTransport#1566
opspawn wants to merge 2 commits intomodelcontextprotocol:mainfrom
opspawn:fix/1564-epipe-crash-handler

Conversation

@opspawn
Copy link

@opspawn opspawn commented Feb 21, 2026

Summary

Fixes #1564StdioServerTransport crashes with unhandled EPIPE when the client disconnects while the server is writing to stdout.

Changes

  • Add stdout error event listener in start() to catch EPIPE/write errors
  • Clean up the error listener in close() to prevent leaks
  • Reject the write promise in send() when backpressure drain fails due to a closed pipe

Test plan

  • Added 3 new tests covering: EPIPE during write, EPIPE during backpressure drain, and error listener cleanup
  • All 480 existing tests pass (40 server + 440 core)

🤖 Generated with Claude Code

…client disconnect

When an MCP client disconnects abruptly before the server finishes
writing its response, stdout.write() throws an unhandled EPIPE error
that crashes the entire server process.

This commit adds proper error handling for the stdout stream in
StdioServerTransport:

- Register an 'error' event listener on stdout during start(), matching
  the existing pattern for stdin error handling
- Clean up the stdout error listener in close()
- Reject the send() promise when a write error occurs during
  backpressure (when waiting for 'drain')

This ensures EPIPE and other stdout write errors are routed through the
transport's onerror callback instead of becoming unhandled exceptions.

Fixes modelcontextprotocol#1564

Signed-off-by: opspawn <sean@opspawn.com>
Signed-off-by: OpSpawn <opspawn@users.noreply.github.com>
@opspawn opspawn requested a review from a team as a code owner February 21, 2026 20:20
@changeset-bot
Copy link

changeset-bot bot commented Feb 21, 2026

🦋 Changeset detected

Latest commit: e1913ee

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 4 packages
Name Type
@modelcontextprotocol/server Patch
@modelcontextprotocol/express Patch
@modelcontextprotocol/hono Patch
@modelcontextprotocol/node Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@pkg-pr-new
Copy link

pkg-pr-new bot commented Feb 21, 2026

Open in StackBlitz

@modelcontextprotocol/client

npm i https://pkg.pr.new/modelcontextprotocol/typescript-sdk/@modelcontextprotocol/client@1566

@modelcontextprotocol/server

npm i https://pkg.pr.new/modelcontextprotocol/typescript-sdk/@modelcontextprotocol/server@1566

@modelcontextprotocol/express

npm i https://pkg.pr.new/modelcontextprotocol/typescript-sdk/@modelcontextprotocol/express@1566

@modelcontextprotocol/hono

npm i https://pkg.pr.new/modelcontextprotocol/typescript-sdk/@modelcontextprotocol/hono@1566

@modelcontextprotocol/node

npm i https://pkg.pr.new/modelcontextprotocol/typescript-sdk/@modelcontextprotocol/node@1566

commit: e1913ee

Signed-off-by: Sean <sean@opspawn.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Unhandled 'EPIPE' in 'StdioServerTransport' causes fatal process crash on client disconnect

1 participant