This repository was archived by the owner on May 16, 2026. It is now read-only.
fix: emit connection error asynchronously to avoid unhandled rejection crashes#1
Open
AmSach wants to merge 1 commit into
Open
fix: emit connection error asynchronously to avoid unhandled rejection crashes#1AmSach wants to merge 1 commit into
AmSach wants to merge 1 commit into
Conversation
…n crashes When a spawned server exits unexpectedly in stdio mode, the Protocol synchronously calls reject() on all pending request promises via _responseHandlers. If no .catch() handler has been attached to those promises (or to client.connect()), the rejection fires synchronously before the caller's try/catch is active, and Node.js v15+ treats it as an unhandled rejection that calls triggerUncaughtException, killing the entire process. The fix adds a new onConnectionError(handler) registration API on the Protocol class. When the transport closes unexpectedly, the error is emitted asynchronously via setImmediate(), giving callers a chance to attach .catch() to the connect() promise before the error fires. The onConnectionError callback receives the SdkError with code ConnectionClosed, allowing callers to handle unexpected disconnections gracefully without the process crashing. Fixes: modelcontextprotocol#1049
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 subscribe to this conversation on GitHub.
Already have an account?
Sign in.
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.
Summary
When a spawned server exits unexpectedly in stdio mode, the method synchronously calls on all pending request promises via . If no handler has been attached to those promises (or to ), the rejection fires before the caller's try/catch is active, and Node.js v15+ treats it as an unhandled rejection that calls , killing the entire process.
This was reported in issue modelcontextprotocol#1049 where using with a server that exits immediately causes to crash the Node.js process.
Root Cause
In , calls synchronously in a block:
When the stdio child process exits unexpectedly:
Fix
Added a new registration API on the class:
The defer means:
API Usage
Files Changed
Closes modelcontextprotocol#1049