Skip to content

Streamable HTTP stateless connections leave server Read goroutines after client close #1073

Closed

Description

@hurtener

Version

github.com/modelcontextprotocol/go-sdk v1.7.0-pre.2

Reproduction

Serve a NewStreamableHTTPHandler with StreamableHTTPOptions{Stateless: true}. Create multiple StreamableClientTransport clients (with DisableStandaloneSSE: true and MaxRetries: -1), initialize/call a tool, then close every ClientSession and the httptest.Server.

Each completed stateless client leaves a streamableServerConn.Read goroutine alive after close. In Dockyard’s integration test, 10 concurrent clients leave 10 goroutines after a five-second teardown poll.

The stateless client has no session ID, so streamableClientConn.Close correctly skips DELETE, but the corresponding server-side connection read loop does not terminate.

Expected

Stateless request-response connections should terminate their server-side read loops after the response lifecycle completes, or the SDK should expose an explicit server shutdown/lifecycle mechanism.

Impact

This blocks strict goroutine-leak assertions when testing MCP 2026-07-28 stateless HTTP. We retained functional concurrency coverage but temporarily allow one SDK-owned goroutine per stateless request while awaiting a fix.

Environment

Go toolchain: 1.26
macOS

Activity

  1. guglielmo-san commented on Jul 13, 2026

    @guglielmo-san
    Contributor

    Hi @hurtener , can you provide more details about your set up?
    Is the server side tool handler long running and still active when you close the ClientSession?

  2. hurtener commented on Jul 13, 2026

    @hurtener
    Author

    Yep. Im working on a framework in around the sdk to make it easy and repeatable the creation of mcp apps (which we use a lot in our work). having said that, i was migrating to the latest spec for a future release and profiling a test server (just a simple ping, no background jobs or real work) got that issue (that didnt happen when pointing to the stable version).
    Happened just after the migration, but, i couldnt replicate it using only the SDK, so i guess its something related to how i wrap the sdk that the change between one version and the other it changed so im having that issue..

    My bad, I think for the moment we can close this until i further dig on the issue. Sorry for the initial opening!

  3. guglielmo-san commented on Jul 13, 2026

    @guglielmo-san
    Contributor

    At first sight my guess is that your tool handler did not return yet when the clientSession is closed. In the current implementation Read and Write goroutine are not closed until there are outgoing/incoming requests. This could be the reason why you are noticing this leak

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