Skip to content

mcp: prune completed listen request IDs from the session - #1190

Open
latent-9 wants to merge 1 commit into
modelcontextprotocol:mainfrom
latent-9:fix/listen-ids-pruning
Open

mcp: prune completed listen request IDs from the session#1190
latent-9 wants to merge 1 commit into
modelcontextprotocol:mainfrom
latent-9:fix/listen-ids-pruning

Conversation

@latent-9

Copy link
Copy Markdown
Contributor

Problem

ServerSession tracks the request IDs of in-flight subscriptions/listen streams so Close can cancel the parked handlers and avoid the jsonrpc2 drain deadlock. The ID is appended when the listen starts, but it is never removed when the listen handler returns.

Every completed listen stream (peer cancel, unsubscribe, stream break) therefore leaves one stale entry in the session for its entire lifetime. On a long-lived session where a client repeatedly subscribes and unsubscribes, the slice grows without bound: one leaked entry per cycle.

Close calls jsonrpc2.Connection.Cancel on each recorded ID, and a stale ID is a no-op there today, but the bookkeeping is meant to hold only in-flight listens and currently never shrinks.

Change

Drop the request ID from the session's bookkeeping when the listen handler returns, matching the lifecycle of the in-flight handler that owns it.

Tests

Three tests reproduce the leak, failing on main and passing here:

  • TestListenPrunedAfterSingleCompletion
  • TestListenIDsDoNotAccumulate
  • TestListenIDsLeakViaPublicSubscribeUnsubscribe

The third exercises the leak through the public Subscribe/Unsubscribe API. go test ./mcp/ passes.

ServerSession tracks the request IDs of in-flight subscriptions/listen
streams so Close can cancel the parked handlers and avoid the jsonrpc2
drain deadlock. The ID is appended when the listen starts but never
removed when the handler returns, so every completed listen (peer
cancel, unsubscribe, stream break) leaks one entry for the life of the
session; a client that repeatedly subscribes and unsubscribes leaks one
entry per cycle. Close's Cancel on a stale ID is a no-op today, but the
slice grows without bound.

Remove the ID when the listen handler returns.
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.

1 participant