Skip to content

mcp: Unsubscribe followed by Subscribe can leave a 2026-07-28 session subscribed to nothing #1359

Closed

Description

@jmrplens

Every line number below is from main at 8dd5d6a.

What I did. On a 2026-07-28 session, with the SDK's client against the SDK's server over the in-memory transport, I called ClientSession.Subscribe(uri), then ClientSession.Unsubscribe(uri), then Subscribe(uri) again, and then Server.ResourceUpdated for the URI once the first listen had finished unwinding on the server.

What I saw. The second Subscribe is acknowledged, with a notifications/subscriptions/acknowledged carrying the new listen's ID, and its listen stays open on the server. The update is never delivered. Afterwards the client still records a subscription to the URI and the server no longer records the session as subscribed, so neither side reports anything. Without any help to the timing, that sequence lost the subscription in 85 to 110 of 200 attempts in each of five runs (in-memory transport, Go 1.27.1, GOMAXPROCS=5, without -race). Under -race the window is much narrower: 0 to 4 of 200 in each of five runs.

What I expected. The update delivered on the second listen, carrying its subscription ID.

Why it happens. resourceSubscriptions is keyed by URI and session (mcp/server.go:63), so the second listen's subscribe overwrites the first listen's request ID (mcp/server.go:1278). ClientSession.Unsubscribe only cancels the first listen and returns (mcp/client.go:1504), so the second listen can be registered and acknowledged before the server has unwound the first. When the first does unwind, its deferred unsubscribe (mcp/server.go:1356) deletes resourceSubscriptions[uri][session] whatever ID it holds (mcp/server.go:1296), which by then is the second listen's. #1283 guards the client's own table against this same race with a generation number, so that a finishing listen only clears the entry it created; the server's table has no such guard.

It needs a session that outlives a single request: stdio, the in-memory transport, or another persistent transport. Over streamable HTTP two listens never share a session, because a stateful handler refuses 2026-07-28 requests (mcp/streamable.go:1697) and a stateless one makes a new session for every POST (mcp/streamable.go:486).

The three list-changed registries behave the same way. Since #1275 a listen's teardown only touches the registries it opted in to, but it still deletes by session (mcp/server.go:1334, :1337, :1340), so a second listen that opts in to the same notification loses its registration when the first one ends, although the comment above those lines says only this listen's registrations are retired. This SDK's client opens a single list-changed listen per session, so only a client that opens a second one reaches this half.

Reproduction. The order is timing-dependent, so my test holds the first listen's teardown in the UnsubscribeHandler until the client has seen the second listen acknowledged, then waits for the first listen to finish unwinding and calls ResourceUpdated. Against main it fails with timed out waiting for "updated", and the list-changed counterpart fails for each of tools, prompts and resources. The pull request I am opening right after this issue carries both tests.

Versions. main at 8dd5d6a. The same table, subscribe and deferred unsubscribe are in v1.8.0. The tests fail on main with Go 1.27.1, and pass with the change on Go 1.25.14, 1.26.8 and 1.27.1.

Where this has come up before. The Known limitations of #1275 name this case: "Two listens subscribed to the same URI. resourceSubscriptions[uri][session] overwrites the same way, and the deferred unsubscribe deletes it." @cgair showed it there with failing tests and offered to open an issue, and the pull request recorded the case as reachable only from a client other than this SDK's. @guglielmo-san's view on that pull request was that keying by session is fine and that only the teardown was wrong, and it read the resource teardown as already scoped, since the deferred unsubscribe deletes only resourceSubscriptions[uri][session]. That deletion is scoped to the URI but not to the listen, and the sequence above, which comes from this SDK's own client, falls in that gap. I still think the keying by session can stay and the teardown is where the fix belongs.

Three shapes I considered.

  1. A listen's teardown removes an entry only while the entry still records that listen's request ID. No API change, and the keying by session stays. It fixes the Go client's re-subscribe, and the list-changed equivalent for a client that opens a second list-changed listen. It does not change what two listens held open at once on one URI receive: the later one receives every update, and if the later one ends first, the earlier one receives nothing, which is what main does today. I measured both with the change applied.
  2. Key resourceSubscriptions and the three list-changed registries by listen as well as session, and deliver to every listen that asked. This fixes both closing orders and matches the 2026-07-28 subscriptions page, under which "each subscription is identified by the JSON-RPC request ID of its subscriptions/listen request". It is internal too, but ResourceUpdated and notifySubscribedSessions change with the table, and the per-session sender proposed in mcp: a resource update cannot be delivered to one session, and the caller's context never reaches the sending middleware #1265 would have to pick among several IDs.
  3. Refuse a second listen for a URI the session already holds. It is the smallest, but it breaks the Go client in exactly the sequence above. I added the refusal to a scratch copy and held the first teardown as in the test: the second listen is refused, Subscribe has already returned nil, and since mcp: clear a resource subscription when its listen stream ends #1283 the client drops its record of the subscription when the listen fails, so the application believes it is subscribed while neither side holds anything.

The pull request implements the first shape, because it fixes the case this SDK's client reaches without touching the table, and the second shape would replace it rather than conflict with it. If you would rather go straight to the second, tell me and I will rework the pull request.

For context, I maintain a GitLab MCP server built on this SDK (#1257). It polls a subscribed resource for its subscriber, so in the state above it keeps polling GitLab with the subscriber's token for a stream that never receives anything.

Activity

  1. guglielmo-san commented on Oct 15, 2026

    @guglielmo-san
    Contributor

    Fixed by #1360

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions