fix(server): stop createMcpHandler chaining onclose on reused instances - #2787
Open
voidstackloop wants to merge 1 commit into
Open
fix(server): stop createMcpHandler chaining onclose on reused instances#2787voidstackloop wants to merge 1 commit into
voidstackloop wants to merge 1 commit into
Conversation
`createMcpHandler` wraps `server.onclose` on every request to track the
instance in `inflight` until its exchange tears down:
const previousOnClose = server.onclose;
inflight.add(server);
server.onclose = () => {
inflight.delete(server);
previousOnClose?.();
};
If the factory returns the same `Server`/`McpServer` instance for every
session (a valid pattern), this installs a new wrapper on every request
instead of once per instance, chaining an unbounded closure onto the
previous `onclose`. The chain grows for the process's lifetime and,
when it eventually runs (session cleanup under load, or
`handler.close()`), recurses one stack frame per accumulated wrapper —
observed overflowing around 19-25k accumulated sessions with
`RangeError: Maximum call stack size exceeded`, surfacing as an
uncaught async error after `close()` has already resolved.
Track which instances already have the wrapper installed in a
`WeakSet` and only install once per instance, keeping the chain length
at 1 regardless of how many requests reuse it. The normal case (a
fresh instance per request, the common factory shape) is unaffected.
Added a regression test that reuses one `McpServer` across five
requests and asserts `onclose` is only ever assigned once.
Closes modelcontextprotocol#2607
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
|
@modelcontextprotocol/client
@modelcontextprotocol/codemod
@modelcontextprotocol/core
@modelcontextprotocol/server
@modelcontextprotocol/server-legacy
@modelcontextprotocol/express
@modelcontextprotocol/fastify
@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.
createMcpHandlerwrapsserver.oncloseon every request to track the instance ininflightuntil its exchange tears down:If the factory returns the same
Server/McpServerinstance for every session (a valid pattern), this installs a new wrapper on every request instead of once per instance, chaining an unbounded closure onto the previousonclose. The chain grows for the process's lifetime and, when it eventually runs (session cleanup under load, orhandler.close()), recurses one stack frame per accumulated wrapper — observed overflowing around 19-25k accumulated sessions withRangeError: Maximum call stack size exceeded, surfacing as an uncaught async error afterclose()has already resolved.Track which instances already have the wrapper installed in a
WeakSetand only install once per instance, keeping the chain length at 1 regardless of how many requests reuse it. The normal case (a fresh instance per request, the common factory shape) is unaffected.Added a regression test that reuses one
McpServeracross five requests and assertsoncloseis only ever assigned once.Closes #2607
Motivation and Context
How Has This Been Tested?
Breaking Changes
Types of changes
Checklist
Additional context