Skip to content

fix: prevent unbounded onclose handler chain on transport reconnect (#2607) - #2821

Draft
nikolas-sapa wants to merge 1 commit into
modelcontextprotocol:mainfrom
nikolas-sapa:fix-unbounded-onclose-chain-2607
Draft

nikolas-sapa wants to merge 1 commit into
modelcontextprotocol:mainfrom
nikolas-sapa:fix-unbounded-onclose-chain-2607

Conversation

@nikolas-sapa

Copy link
Copy Markdown

Protocol.connect() wrapped transport.onclose in a new closure on every call. When connect() is invoked multiple times on the same transport (e.g. session-resuming reconnects in StreamableHTTP), each call nests a new closure around the previous one, creating an unbounded chain. Every transport close then walks the entire accumulated chain — a memory leak proportional to the number of reconnects.

The fix detects when the transport is the same as the previously connected one and skips re-wrapping onclose/onerror/onmessage, so the existing wrapper (which already chains the prior handlers and calls this._onclose()) is reused instead of nested again.

Fixes #2607

…odelcontextprotocol#2607)

Protocol.connect() wrapped transport.onclose in a new closure on every
call. When connect() is invoked multiple times on the same transport
(e.g. session-resuming reconnects), each call nests a new closure
around the previous one. Every transport close then walks the entire
accumulated chain — a memory leak proportional to the number of
reconnects.

The fix detects when the transport is the same as the previously
connected one and skips re-wrapping onclose/onerror/onmessage, so the
existing wrapper (which already chains the prior handlers and calls
this._onclose()) is reused instead of nested again.
@changeset-bot

changeset-bot Bot commented Sep 17, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: dd847b0

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@modelcontextprotocol/core-internal Patch

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

@pkg-pr-new

pkg-pr-new Bot commented Sep 17, 2026

Copy link
Copy Markdown

Open in StackBlitz

@modelcontextprotocol/client

npm i https://pkg.pr.new/@modelcontextprotocol/client@2821

@modelcontextprotocol/codemod

npm i https://pkg.pr.new/@modelcontextprotocol/codemod@2821

@modelcontextprotocol/core

npm i https://pkg.pr.new/@modelcontextprotocol/core@2821

@modelcontextprotocol/server

npm i https://pkg.pr.new/@modelcontextprotocol/server@2821

@modelcontextprotocol/server-legacy

npm i https://pkg.pr.new/@modelcontextprotocol/server-legacy@2821

@modelcontextprotocol/express

npm i https://pkg.pr.new/@modelcontextprotocol/express@2821

@modelcontextprotocol/fastify

npm i https://pkg.pr.new/@modelcontextprotocol/fastify@2821

@modelcontextprotocol/hono

npm i https://pkg.pr.new/@modelcontextprotocol/hono@2821

@modelcontextprotocol/node

npm i https://pkg.pr.new/@modelcontextprotocol/node@2821

commit: dd847b0

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

createMcpHandler: reused McpServer instance grows an unbounded onclose chain — memory leak, then uncatchable RangeError after ~20k requests

1 participant