Skip to content

mcp: retire an ended listen when UnsubscribeHandler fails - #1558

Merged
guglielmo-san merged 2 commits into
mainfrom
serob/retire-ended-listen-when-UnsubscribeHandler-fails
Dec 1, 2026
Merged

guglielmo-san merged 2 commits into
mainfrom
serob/retire-ended-listen-when-UnsubscribeHandler-fails

Conversation

@nahapetyan-serob

Copy link
Copy Markdown
Contributor

When a subscriptions/listen ends, its deferred endSubscription call ran
UnsubscribeHandler with the listen's already-cancelled context and returned
on the handler's error before removing the registration. A handler returning
ctx.Err() was enough to leave the listen registered, so ResourceUpdated
kept notifying it and stale entries accumulated across subscribe/unsubscribe
cycles until the session disconnected.

Changes:

  • An ended listen is now removed whatever the handler returns; the error is
    logged at debug level, since no one is waiting for it. An explicit
    resources/unsubscribe can still be refused by the handler.
  • At listen teardown, UnsubscribeHandler now gets a non-cancelled context
    (context.WithoutCancel) bounded by a 5s timeout, so context-aware cleanup
    can run. This changes what handlers receive on that path

Fixes #1545

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

No active review issues block the change to clean up ended resource subscriptions.

No open issues 🎉

What changed in this PR

Fixes stale resource subscriptions in the Go MCP SDK by retiring ended listens even when UnsubscribeHandler fails, addressing #1545.

Changes:

  • Removes ended listen registrations and logs handler errors while preserving the handler’s ability to reject explicit unsubscribe requests.
  • Gives teardown handlers a non-cancelled context with a five-second deadline.
  • Adds regression tests for registration cleanup, handler context, and legacy unsubscribe behavior.
File Description
mcp/​server.go Updates listen cleanup, error logging, and teardown context handling.
mcp/​mcp_test.go Tests modern listen cleanup and legacy unsubscribe rejection when the handler fails.

Review effort: Balanced

@guglielmo-san
guglielmo-san merged commit ef91eda into main Dec 1, 2026
9 checks passed
@guglielmo-san
guglielmo-san deleted the serob/retire-ended-listen-when-UnsubscribeHandler-fails branch December 1, 2026 09:12
Tangerg added a commit to Tangerg/go-sdk that referenced this pull request Dec 2, 2026
Brings the fork up to upstream main at 723d70e, which is v1.9.0 plus
fourteen later commits. Upstream changes: modelcontextprotocol#1238, modelcontextprotocol#1511, modelcontextprotocol#1515, modelcontextprotocol#1517,
modelcontextprotocol#1521, modelcontextprotocol#1523, modelcontextprotocol#1524, modelcontextprotocol#1527, modelcontextprotocol#1533, modelcontextprotocol#1537, modelcontextprotocol#1539, modelcontextprotocol#1540, modelcontextprotocol#1544, modelcontextprotocol#1547,
modelcontextprotocol#1549, modelcontextprotocol#1550, modelcontextprotocol#1553, modelcontextprotocol#1554, modelcontextprotocol#1558, modelcontextprotocol#1560, modelcontextprotocol#1561, modelcontextprotocol#1562.

Fork-side adaptations:

- internal/jsonrpc2: the -32600 reply to a duplicate in-flight ID
  (modelcontextprotocol#1550) is written off the read loop and holds its admission slot
  until written, like every other reply under MaxIncomingRequests.
- mcp: ClientSession.initialize takes the session state lock that
  upstream added, and still projects the negotiated version to the
  connection.
- mcp: the fork's copies of hintomitempty, nowrapinvalidparams and
  nomethodnotfoundcodeinerror go away with upstream's removal (modelcontextprotocol#1533).
- extensions/skills: reads the protocol version from the fork's
  json.RawMessage Meta.
- Tests: the duplicate-ID admission test drains the new reply, and the
  client cancellation test runs on 2025-11-25 only, because 2026-07-28
  clients reject every standalone server request (modelcontextprotocol#1537, modelcontextprotocol#1540, modelcontextprotocol#1554).
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.

mcp: an ended subscriptions/listen stays subscribed when UnsubscribeHandler returns an error

3 participants