mcp: prune completed listen request IDs from the session - #1190
Open
latent-9 wants to merge 1 commit into
Open
Conversation
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.
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.
Problem
ServerSessiontracks the request IDs of in-flightsubscriptions/listenstreams soClosecan 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.
Closecallsjsonrpc2.Connection.Cancelon 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
mainand passing here:TestListenPrunedAfterSingleCompletionTestListenIDsDoNotAccumulateTestListenIDsLeakViaPublicSubscribeUnsubscribeThe third exercises the leak through the public
Subscribe/UnsubscribeAPI.go test ./mcp/passes.