fix(server): fire onsessionclosed once when DELETEs overlap - #2583
Draft
axits-lab wants to merge 1 commit into
Draft
fix(server): fire onsessionclosed once when DELETEs overlap#2583axits-lab wants to merge 1 commit into
axits-lab wants to merge 1 commit into
Conversation
`handleDeleteRequest` awaited `onsessionclosed` before `close()` ran, so `_closed` was still false while the callback was in flight. A second DELETE for the same session passed `validateSession`/`validateProtocolVersion` unchanged — neither consults `_closed` — and invoked the callback again for a session already being torn down. Claim the notification synchronously before the await. `_closed` cannot serve this purpose: it is set by `close()`, which only runs once the callback has settled. DELETE stays idempotent — a concurrent or repeat request still terminates the session and answers 200, it just does not re-run the callback. Refs modelcontextprotocol#2562 (item 2) Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
🦋 Changeset detectedLatest commit: ac6312f The changes in this PR will be included in the next version bump. This PR includes changesets to release 6 packages
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 |
@modelcontextprotocol/client
@modelcontextprotocol/codemod
@modelcontextprotocol/core
@modelcontextprotocol/server
@modelcontextprotocol/server-legacy
@modelcontextprotocol/express
@modelcontextprotocol/fastify
@modelcontextprotocol/hono
@modelcontextprotocol/node
commit: |
This was referenced Jul 30, 2026
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.
Refs #2562 — item 2 of three. Items 1 and 3 are not touched here; item 1 will follow separately, and item 3 looks like it needs a call on the
EventStorecontract first.The race
handleDeleteRequestawaitsonsessionclosedand only then runsclose()from thefinally:_closedis set byclose(), so it is stillfalsefor the whole time the callback is in flight. NeithervalidateSessionnorvalidateProtocolVersionconsults_closed— they checksessionIdGenerator,_initialized, and the session-id match — so a second DELETE arriving in that window passes every guard and fires the callback a second time for a session already being torn down.Fix
Claim the notification synchronously, before the await.
_closedcannot serve this purpose, since by construction it is only set after the callback has settled — hence the separate flag.DELETE stays idempotent: a concurrent or repeat request still terminates the session and answers
200, it just does not re-run the callback.Test
Added to the existing
Session Callbacksblock. It holds the callback open on a promise the test controls, issues the second DELETE while the first is parked on it, then releases:That is the failure on
mainwithout the src change; it passes with it.Verification
469/469tests pass in@modelcontextprotocol/server(53 instreamableHttp.test.ts)pnpm lintandpnpm typecheckcleanonsessionclosedis implemented only inpackages/server/src/server/streamableHttp.ts, so no sibling transport carries the same windowChangeset included.
You noted the item applies to
v1.xas well — happy to put up the equivalent there if you'd like it as a separate PR.