-
Notifications
You must be signed in to change notification settings - Fork 1.8k
fix: wait for pending requests before aborting transports on close #1692
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
travisbreaks
wants to merge
2
commits into
modelcontextprotocol:main
from
travisbreaks:fix/abort-controller-successful-requests
+48
−1
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🟡 Nit: The
setTimeoutfallback inPromise.racewithinclose()is never cleared whenPromise.allSettledresolves first. In Node.js, an active timer keeps the event loop alive, so the process cannot exit for up to 2 seconds afterclose()completes — this will cause test runners (Jest/Vitest) to report open handles. Fix by storing the timer ID and callingclearTimeoutafter the race, or by usingsetTimeout(...).unref(). Same issue exists instreamableHttp.tsline 462.Extended reasoning...
What the bug is
In both
sse.ts(line 246) andstreamableHttp.ts(line 462), theclose()method usesPromise.racebetweenPromise.allSettled(this._pendingRequests)and asetTimeout-based promise as a 2-second fallback. However, the timer ID fromsetTimeoutis never stored, so whenPromise.allSettledwins the race (the common case — pending requests complete quickly), the timer is never cleared.Why this matters
In Node.js, an active timer handle keeps the event loop alive. This means that even after
close()has resolved and all cleanup is done, the process cannot exit for up to 2 seconds while waiting for the orphaned timer to fire. This is particularly problematic for:close()with pending requests.Step-by-step proof
send(), which adds a tracked promise to_pendingRequests.close(). Since_pendingRequests.size > 0, the code enters thePromise.raceblock.Promise.allSettledresolves almost immediately (the request already completed).Promise.raceresolves, andclose()proceeds to abort and clean up.setTimeout(resolve, 2000)timer is still active in the event loop — nobody stored its ID or calledclearTimeout.How to fix
The simplest fix is to use
.unref()on the timer, which tells Node.js not to keep the event loop alive for it:Alternatively, store the timer ID and clear it after the race:
Both approaches are straightforward and prevent the delayed-exit behavior.