Skip to content

mcp: prevent leaks of ServerSession on StatefulServer - #1137

Merged
guglielmo-san merged 2 commits into
mainfrom
guglielmoc/avoid_initialization_of_init_params_when_unnecessary
Aug 3, 2026
Merged

mcp: prevent leaks of ServerSession on StatefulServer#1137
guglielmo-san merged 2 commits into
mainfrom
guglielmoc/avoid_initialization_of_init_params_when_unnecessary

Conversation

@guglielmo-san

Copy link
Copy Markdown
Contributor

Fixes #1136

…InitializeParams persistence during discovery
@scottlz0310-user

Copy link
Copy Markdown

Thanks for the quick turnaround. I verified this branch (bfee8f4) as the reporter of #1136, and it fixes the leak.

Reproduction from #1136 — was FINAL leaked sessions = 3 on v1.7.0, now:

client 1 connected: negotiated=2025-11-25 sessions=1
client 1 closed   : sessions=0 (want 0)
client 2 connected: negotiated=2025-11-25 sessions=1
client 2 closed   : sessions=0 (want 0)
client 3 connected: negotiated=2025-11-25 sessions=1
client 3 closed   : sessions=0 (want 0)

FINAL leaked sessions = 0 (want 0)

The session count now stays at exactly 1 while a client is connected, instead of climbing by one per connection.

Other checks on this branch:

  • go test -count=1 ./mcp/... — green.
  • The new TestStreamableStateful_DiscoverDoesNotLeakSession — passes.
  • The downstream project whose test suite regressed on the v1.6.1 → v1.7.0 bump (its test asserts that a stateful server reuses exactly one session across requests) is green again with this branch dropped in via replace, with no changes on its side.

Your approach is neater than the one I sketched in #1136. I proposed routing discover-only requests through the ephemeral path in serveStatefulPOST; gating the InitializeParams persistence on whether the transport can actually serve 2026-07-28 addresses the same root cause with a smaller change and lets the existing #578 safety net do the cleanup. The added regression test covers the coupling to that safety net, which was my only concern with relying on it.

Nothing further from me — happy to see this land.

@guglielmo-san
guglielmo-san enabled auto-merge (squash) August 3, 2026 11:42
@guglielmo-san
guglielmo-san merged commit 0d0cdbc into main Aug 3, 2026
9 checks passed
@guglielmo-san
guglielmo-san deleted the guglielmoc/avoid_initialization_of_init_params_when_unnecessary branch August 3, 2026 11:54
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.

Streamable HTTP: server/discover leaks a ServerSession on stateful servers

3 participants