fix: handle EPIPE errors in StdioServerTransport gracefully#1644
Open
MaxwellCalkin wants to merge 1 commit intomodelcontextprotocol:mainfrom
Open
fix: handle EPIPE errors in StdioServerTransport gracefully#1644MaxwellCalkin wants to merge 1 commit intomodelcontextprotocol:mainfrom
MaxwellCalkin wants to merge 1 commit intomodelcontextprotocol:mainfrom
Conversation
When a client disconnects abruptly (closes stdio pipes before the server can write a response), stdout.write() throws an unhandled EPIPE error that crashes the entire Node.js process. This commit: - Adds an error event listener on stdout that forwards errors to onerror and triggers a clean close() - Wraps stdout.write() in try/catch in send() to catch synchronous write errors and reject the promise instead of crashing - Removes the stdout error listener in close() to prevent leaks Fixes modelcontextprotocol#1564
🦋 Changeset detectedLatest commit: 75c8800 The changes in this PR will be included in the next version bump. This PR includes changesets to release 4 packages
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 |
@modelcontextprotocol/client
@modelcontextprotocol/server
@modelcontextprotocol/express
@modelcontextprotocol/hono
@modelcontextprotocol/node
commit: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Note: This PR was authored by Claude (AI), operated by @MaxwellCalkin.
Fixes #1564
Problem
When an MCP client disconnects abruptly (closes stdio pipes before the server can write a response),
stdout.write()throws an unhandledEPIPEerror that crashes the entire Node.js process:This happens because
StdioServerTransporthas noerrorevent listener on the stdout stream, andstdout.write()insend()is not wrapped in error handling.Fix
Three changes to
StdioServerTransport:Add
_onstdouterrorhandler — an arrow function (matching the existing pattern for_ondata/_onerror) that forwards stdout errors toonerrorand triggers a cleanclose().Register/unregister the listener —
start()addsthis._stdout.on('error', this._onstdouterror);close()removes it with.off().Wrap
stdout.write()in try/catch insend()— ifwrite()throws synchronously, the promise rejects instead of crashing the process.This matches how the client-side
StdioClientTransportalready handles errors onstdinandstdout.Tests
Added two new tests:
onerrorandclose()write()throws are caught and the promise rejectsChangeset
Included a patch changeset for
@modelcontextprotocol/server.