mcp: reject Subscribe on a closed session and roll back failed registrations - #1198
Open
GerardGao wants to merge 1 commit into
Open
Conversation
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.
What
ClientSession.Subscribe(SEP-2575 path) starts asubscriptions/listenstreamwithout awaiting the send result, so on a session that is closing — or when the
listen send fails for any other reason — it can return
nileven though thestream never started. The local
resourceSubsmap also keeps an entry whosecancel func is then never called, leaking the registration and misreporting the
URI as subscribed. Fixes #1171.
How
closedflag toClientSession, set at the top ofClose.Subscribechecks the flag underresourceSubsMubefore registering, so acall after
Closehas started returnsErrConnectionClosedand registersnothing. Because the check and the registration share the mutex with the
Closecleanup sweep, a racing registration is either swept byCloseorrefused outright — no interleaving leaks.
subscriptionsListenreports an error, the registration is rolled back(entry removed and cancel func called), so a later
Subscriberetries thelisten instead of treating the URI as already subscribed.
Unsubscribeand idempotent double-Subscribebehavior are unchanged.Tests
TestResourceSubscriptions_SubscribeClosedSession: Subscribe after Closereturns
ErrConnectionClosedand leaves noresourceSubsentry.TestResourceSubscriptions_SubscribeListenFailureRollsBack: a failed listensend (injected via
AddSendingMiddleware) rolls back the registration and aretry re-attempts the listen.
TestResourceSubscriptions_SubscribeCloseRace: 50 rounds of racingCloseand
Subscribeasserting that a failedSubscribenever leaves an entry.go test -race ./mcp/andgo test ./...pass;gofmt,go vet, andstaticcheckare clean.