MCP 服务 2026-10-10 09:15 UTC
go-sdk
由提交人 jmrplens 提交,主要更新类型为:常规更新与维护。提交说明:mcp: retire a listen's subscriptions only while they are its own (#1360)。对项目做了常规维护与小幅调整。
Commit提交信息原文
mcp: retire a listen's subscriptions only while they are its own (#1360) The subscription registries are keyed by session, so a second `subscriptions/listen` that subscribes to a URI the session already holds, or opts in to a list-changed notification the session already receives, replaces the first listen's entry with its own request ID. The first listen's teardown then deleted that entry by session, and the second listen stayed open with nothing delivered to it. This SDK's own client reaches it on 2026-07-28 over stdio or any other persistent transport; over streamable HTTP two listens never share a session. `ClientSession.Unsubscribe` only cancels the listen, so a `Subscribe` for the same URI right after it can be acknowledged before the server has unwound the first listen, whose deferred `unsubscribe` then removes what the second listen has just registered. Over the in-memory transport and without `-race`, `Subscribe`, `Unsubscribe` and `Subscribe` again lost the subscription in 85 to 110 of 200 attempts in each of five runs on `main`, and in none with this change; under `-race` the window narrows to 0 to 4 in 200. #1283 guards the client's own table against the same race with a generation number; this is the server's half. The change follows the guards #1275 added: - The teardown of the three list-changed registries deletes an entry only while it still holds this listen's request ID. - A listen's deferred resource unsubscribe goes through a new internal `endSubscription`, which removes `resourceSubscriptions[uri][session]` only while it holds this listen's request ID. `resources/unsubscribe` calls it with the zero ID and removes the session's subscription whichever request made it, as before. - The `UnsubscribeHandler` is still called for every URI of every listen that ends, so its calls stay paired with the `SubscribeHandler`'s. Skipping it when a later listen holds the URI would leave a handler that counts listens one unsubscribe short. I kept it this way on purpose; say if you would rather it ran only when the session's entry goes away. The other side of it: a handler that keeps a set of subscribed sessions, as the field's doc reads, now sees an unsubscribe for a URI the session is still subscribed to. If you keep this, I can add a sentence to the `UnsubscribeHandler` doc saying it is called once for each listen that ends. There are no exported API changes, and the keying by session stays. What this does not change is two listens held open at once on one URI: the later one receives every update, and if it ends first the earlier one receives nothing, as on `main` today. That needs the table keyed per listen, and #1359 sets out that shape and one more for you to choose from. This change stands on its own if you keep the keying by session, and the per-listen keying would replace it rather than conflict with it. Tests, next to the existing `TestSubscriptionsListen_*` ones and reusing their fixtures: - `TestSubscriptionsListen_ResubscribeOutlivesEarlierTeardown` drives the Go client through `Subscribe`, `Unsubscribe` and `Subscribe`, and holds the first listen's teardown in the `UnsubscribeHandler` until the client has seen the second listen acknowledged, so the losing order happens every time. It then checks that `ResourceUpdated` reaches the second listen with that listen's subscription ID, and that unsubscribing the second listen still retires the subscription. - `TestSubscriptionsListen_ListChangedOutlivesEarlierTeardown` opens a second listen next to the auto-listen for each of tools, prompts and resources list-changed, ends the auto-listen, and checks that the change reaches the second listen. Against `main` at 8dd5d6a both fail, the second in all three subtests (`timed out waiting for "updated"`, then `"tool"`, `"prompt"` and `"resource"`), and with the change they pass. Taking out any one of the four guards fails the test for that registry, making a listen never retire its resource entry fails `TestSubscriptionsListen_TeardownKeepsOtherListens`, `TestSubscriptionsListen_ResubscribeOutlivesEarlierTeardown`, `TestResourceSubscriptions_InMemory` and `TestResourceSubscriptions_MultipleURIs`, and making `resources/unsubscribe` keep the entry fails `TestEndToEnd`. `go test ./...` on Go 1.25.14 and `go test -race ./...` on Go 1.26.8 and 1.27.1 pass, the new tests passed 20 runs in a row under `-race`, `gofmt -l` and `go vet ./...` are clean, and so is `staticcheck` v0.6.1 on Go 1.26.8. @cgair named this case in the Known limitations of #1275 and showed it there with failing tests; this takes the part of it that the Go client reaches. Updates #1359 Co-authored-by: Guglielmo Colombo <guglielmoc@google.com>