Skip to content

Unshielded session teardown in streamable_http_client aborts DELETE under cancellation, leaking server session #3877

Open

Description

@shivamm-gupta

Release Line

2.x (main branch)

Problem Description

In src/mcp/client/streamable_http.py:787-793, streamable_http_client cleans up the session and cancels the task group in a finally: block:

            try:
                yield read_stream, write_stream
            finally:
                if transport.session_id and terminate_on_close:
                    await transport.terminate_session(client)
                tg.cancel_scope.cancel()

When caller execution exits due to cancellation or timeout (for example, wrapping the client session in anyio.move_on_after(...) or cancelling the enclosing task), the active cancel scope is already cancelled when entering the finally: block.

Because transport.terminate_session(client) performs an asynchronous HTTP DELETE request without a shielded cancel scope:

  1. The DELETE request raises CancelledError (or anyio.get_cancelled_exc_class()) immediately at the first checkpoint.
  2. terminate_session catches Exception, but not BaseException / CancelledError. The exception propagates, aborting the DELETE request before it can reach the server.
  3. The server session is leaked and remains open until remote timeout.
  4. Line 792 (tg.cancel_scope.cancel()) is never executed because the exception unwinds out of the finally block before reaching it.

Expected Behavior

Teardown logic in finally: blocks that issues network calls should be executed inside a shielded cancel scope (with an optional bounded timeout) so that cancellation of the client caller does not prevent sending the DELETE session termination request, nor block task group cancellation:

            finally:
                with anyio.CancelScope(shield=True):
                    if transport.session_id and terminate_on_close:
                        with anyio.move_on_after(5):
                            await transport.terminate_session(client)
                    tg.cancel_scope.cancel()

Environment

  • Python: 3.11+
  • MCP Python SDK: 2.x (main branch)

I would be happy to submit a PR with test coverage if assigned.


Assisted by AI; reviewed and verified.

Activity

  1. added
    bugSomething isn't working
    v1Affects the v1.x maintenance line
    v2Affects the v2 line (2.x on main)
    on Nov 19, 2026
  2. ar-ramchandra commented on Nov 19, 2026

    @ar-ramchandra

    Independently reproduced this on main at 77001c9 with a real Client and stateful server through the in-process HTTP transport. Cancellation before DELETE, and cancellation after DELETE has started, both leave the old session accepting ping (HTTP 200). Normal close makes that same session return 404.

    I've prepared regression coverage for those paths, terminate_on_close=False, and a stalled DELETE. A candidate fix using bounded, shielded cleanup passes all 6,241 tests with 100% coverage locally. The proposed five-second bound also limits normal teardown when the configured HTTP timeout is longer, so that budget needs a maintainer decision.

    @shivamm-gupta, happy to contribute the regression tests to your implementation rather than open a competing PR.

    Investigation, tests, and candidate patch were AI-assisted with OpenAI Codex; the before/after checks were executed locally.

  3. AtharvaJoshi8216 commented on Nov 20, 2026

    @AtharvaJoshi8216

    The five-second limit is the part I'd be careful with. Shielding cleanup makes sense, but I'd probably keep the timeout configurable rather than silently overriding the client's existing HTTP timeout. Otherwise a slow but valid DELETE could still leave the session around. Did you test what happens when the server takes longer than five seconds to process the termination?

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

    bugSomething isn't workingv1Affects the v1.x maintenance linev2Affects the v2 line (2.x on main)

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions