-
Notifications
You must be signed in to change notification settings - Fork 2.2k
fix(server): restore onclose after modern exchanges #2778
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
Merged
felixweinberger
merged 7 commits into
modelcontextprotocol:main
from
vjymisal0:fix/2607-reused-server-onclose-leak
Sep 28, 2026
+58
−1
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
79e9743
fix(server): restore onclose after modern exchanges
vjymisal0 37260aa
chore(server): add changeset for onclose restoration
vjymisal0 19ba6f0
Merge branch 'main' into fix/2607-reused-server-onclose-leak
vjymisal0 c9ffcac
Merge branch 'main' into fix/2607-reused-server-onclose-leak
felixweinberger c4d278b
Merge branch 'main' into fix/2607-reused-server-onclose-leak
felixweinberger 041fb0e
chore(changeset): say what was fixed and keep the fresh-instance cont…
felixweinberger 67f14d2
fix(server): restore onclose by identity so a handler set during the …
felixweinberger 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| '@modelcontextprotocol/server': patch | ||
| --- | ||
|
|
||
| Fix a stack overflow in `createMcpHandler` when the factory returns the same server instance for more than one request. Returning a fresh instance per request is still required. |
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 (optional): maintainers get no test that a reused server's
oncloseis restored on the failure or shutdown paths, only on the happy path. The new test at packages/server/test/server/createMcpHandler.test.ts:277-296 drives three successfultools/callexchanges; the restore also has to hold when the exchange fails internally (createMcpHandler.ts:900 callsserver.close()) and whenhandler.close()aborts an in-flight reused instance (createMcpHandler.ts:1005). Fix: add coverage for both error/teardown paths with a reusedMcpServer, assertingreused.server.oncloseis the original function afterwards and that it ran once per exchange. [also at: packages/server/test/server/createMcpHandler.test.ts:277 - nit: REVIEW.md asks that new behavior have vitest coverage including error paths: the new test 'restores a reused server onclose handler after each modern exchange' only exercises three successful tools/call exchanges.]Why this was flagged
REVIEW.md asks that new behavior have vitest coverage including error paths. The behavior this diff adds is the
server.onclose = previousOnClose;restore at packages/server/src/server/createMcpHandler.ts:872, which is reached through three routes: the transport's auto-close after a terminal response, the explicitserver.close()on the internal-failure path at createMcpHandler.ts:900, andhandler.close()at createMcpHandler.ts:1005. The added test at packages/server/test/server/createMcpHandler.test.ts:277-296 only exercises the first route with three successfultools/callexchanges and assertsoriginalOnClosewas called 3 times. The existing failure-path test at createMcpHandler.test.ts:298-325 uses a fresh per-request instance fromtestFactory()and never inspectsonclose, so a regression that leaves the wrapper installed after a failed or aborted exchange on a reused server (the original bug this PR fixes, on a sibling path) would pass CI.…Verification: nit. The REVIEW.md block above contains, verbatim under "Tests & docs", "- New behavior has vitest coverage including error paths". The new behavior in this diff is the single added line
server.onclose = previousOnClose;at /home/claude/typescript-sdk/packages/server/src/server/createMcpHandler.ts:872, inside the wrapper installed at lines 870-874. That wrapper is reached on three routes:…