Skip to content

fix(server): restore onclose after modern exchanges - #2778

Open
vjymisal0 wants to merge 1 commit into
modelcontextprotocol:mainfrom
vjymisal0:fix/2607-reused-server-onclose-leak
Open

fix(server): restore onclose after modern exchanges#2778
vjymisal0 wants to merge 1 commit into
modelcontextprotocol:mainfrom
vjymisal0:fix/2607-reused-server-onclose-leak

Conversation

@vjymisal0

@vjymisal0 vjymisal0 commented Sep 10, 2026

Copy link
Copy Markdown

Summary

Fixes #2607.

When a createMcpHandler factory returns the same McpServer instance for multiple modern requests, the handler temporarily wraps server.onclose for in-flight tracking. The wrapper removed the server from the in-flight set but did not restore the previous onclose, so reused servers accumulated a nested wrapper chain across requests.

This restores the previous onclose before invoking it, so each completed exchange leaves a reused server with its original close callback instead of another wrapper layer.

Tests

  • pnpm --filter @modelcontextprotocol/server exec vitest run test/server/createMcpHandler.test.ts -t "restores a reused server onclose"
  • pnpm --filter @modelcontextprotocol/server exec vitest run test/server/createMcpHandler.test.ts
  • pnpm exec prettier --check packages/server/src/server/createMcpHandler.ts packages/server/test/server/createMcpHandler.test.ts
  • pnpm --dir packages/server exec eslint src/server/createMcpHandler.ts
  • pnpm --filter @modelcontextprotocol/server run typecheck

Note: pnpm --filter @modelcontextprotocol/server run check currently reaches Prettier warnings across many existing package files unrelated to this patch, so I also ran targeted Prettier/ESLint on the touched files.

@vjymisal0
vjymisal0 requested a review from a team as a code owner September 10, 2026 10:37
@changeset-bot

changeset-bot Bot commented Sep 10, 2026

Copy link
Copy Markdown

⚠️ No Changeset found

Latest commit: 94d37c4

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@pkg-pr-new

pkg-pr-new Bot commented Sep 10, 2026

Copy link
Copy Markdown

Open in StackBlitz

@modelcontextprotocol/client

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

@modelcontextprotocol/codemod

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

@modelcontextprotocol/core

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

@modelcontextprotocol/server

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

@modelcontextprotocol/server-legacy

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

@modelcontextprotocol/express

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

@modelcontextprotocol/fastify

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

@modelcontextprotocol/hono

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

@modelcontextprotocol/node

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

commit: 94d37c4

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