Skip to content

fix(everything): cleanup subscriptions on session disconnect - #4715

Closed
dlowzzxx wants to merge 2 commits into
modelcontextprotocol:mainfrom
dlowzzxx:fix/mcp-4710
Closed

dlowzzxx wants to merge 2 commits into
modelcontextprotocol:mainfrom
dlowzzxx:fix/mcp-4710

Conversation

@dlowzzxx

Copy link
Copy Markdown

Fixes #4710 — resources/subscribeadded tosubscriptionsmap butcleanup()never removed session on disconnect, leaking sessionIds. AddremoveSubscriber(sessionId)iterating map and deleting empty entries, called fromserver/index.ts:cleanup(). Verified with npm test (107 passed).

opencode and others added 2 commits August 29, 2026 09:12
Fixes modelcontextprotocol#4710. The subscriptions Map retained sessionIds after
disconnect because cleanup() only cleared intervals and task store.
Export removeSubscriber(sessionId) that iterates the map, deletes the
session from each Set and drops empty entries, and call it from
server factory cleanup() (used by streamableHttp and sse onclose).

The reference server is the example clients copy; this prevents
unbounded growth from clients that subscribe then disconnect without
unsubscribing.
@olaservo

Copy link
Copy Markdown
Member

Thanks for the PR! Adding a belated comment here. This was closed as a duplicate. #4712 was opened first with the same removeSubscriber and cleanup() change and equivalent tests, and it merged as f46d957.

chrikrah added a commit to chrikrah/servers that referenced this pull request Sep 26, 2026
syncRoots caches one Root[] per session in the module-level map at
server/roots.ts:8, and that cache is the only thing deciding whether a session's
roots get fetched again. cleanup(sessionId) at server/index.ts:109 stops the
simulated logging and resource-update intervals and clears the task store, but
never removes the session's entry, so on an SSE or streamable-HTTP server the
map grows by one entry per past connection for the life of the process.

The sibling map in resources/subscriptions.ts has the same gap, and five open
pull requests fix it: modelcontextprotocol#4798, modelcontextprotocol#4718, modelcontextprotocol#4716, modelcontextprotocol#4715 and modelcontextprotocol#4712. Not one of them
touches roots.ts, so fixing subscriptions leaves this identical leak alive next
to it.

Tests: 108 passed. One new case sets an entry, calls cleanup, and asserts the
entry is gone. Reverting roots.ts and index.ts and keeping the test gives
1 failed, 5 passed in server.test.ts. npm run build is clean.
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.

everything: subscriptions map never drops a disconnected session

2 participants