Skip to content

POST failure identical to task cancellation in streamable HTTP client #3560

Open

Description

@charles-dyfis-net

Initial Checks

Release line

2.x (current stable)

Description

#2047 made non-2xx responses request-scoped (#2604). A POST that raises before there is any response -- connection refused or similar -- still escapes handle_request_async into post_writer's task group. The waiting caller gets CancelledError with WouldBlock as __context__, identical to what happens in cancellation. The ConnectError never reaches the caller; it only appears in the ExceptionGroup raised when streamable_http_client exits.

A caller therefore can't tell "server went away" from "this task is being cancelled", and can't retry the first without swallowing the second.

Expected: the failed call raises MCPError with the transport exception reachable as its cause. External cancellation still raises CancelledError.

#1830 reported this and was closed after 1.25.0 appeared to produce an ExceptionGroup instead. Both happen: the caller gets CancelledError, and the ExceptionGroup surfaces later at transport exit. #1830's repro used only sse_client.

Example Code

# /// script
# requires-python = ">=3.11"
# dependencies = ["mcp>=2.2.0", "uvicorn"]
# ///
"""A: kill the server mid-session, then call a tool. B: cancel an in-flight call.

Prints the exception each caller receives, with its __cause__/__context__ chain.
"""

import asyncio
import importlib.metadata
import multiprocessing
import socket
import time

import uvicorn

from mcp import ClientSession
from mcp.client.streamable_http import streamable_http_client
from mcp.server import MCPServer

HOST = "127.0.0.1"


def get_free_port() -> int:
    with socket.socket() as sock:
        sock.bind((HOST, 0))
        return int(sock.getsockname()[1])


def run_server(port: int) -> None:
    server = MCPServer("repro", log_level="CRITICAL")

    @server.tool()
    def echo(text: str) -> str:
        return text

    @server.tool()
    async def slow() -> str:
        await asyncio.sleep(30)
        return "done"

    uvicorn.run(server.streamable_http_app(), host=HOST, port=port, log_level="critical")


def start_server() -> tuple[multiprocessing.Process, str]:
    port = get_free_port()
    proc = multiprocessing.Process(target=run_server, args=(port,), daemon=True)
    proc.start()
    deadline = time.monotonic() + 30
    while time.monotonic() < deadline:
        try:
            with socket.create_connection((HOST, port), timeout=0.25):
                return proc, f"http://{HOST}:{port}/mcp"
        except OSError:
            time.sleep(0.05)
    raise RuntimeError("server did not come up")


def report(label: str, exc: BaseException) -> None:
    print(f"  {label}:")
    seen: set[int] = set()
    current: BaseException | None = exc
    depth = 2
    while current is not None and id(current) not in seen:
        seen.add(id(current))
        print(f"{'  ' * depth}{type(current).__name__}: {current!r}")
        if isinstance(current, BaseExceptionGroup):
            for sub in current.exceptions:
                print(f"{'  ' * (depth + 1)}| {type(sub).__name__}: {sub!r}")
        current = current.__cause__ or current.__context__
        depth += 1


async def scenario_server_dies() -> None:
    proc, url = start_server()
    try:
        async with streamable_http_client(url) as (read, write):
            async with ClientSession(read, write) as session:
                await session.initialize()
                await session.call_tool("echo", {"text": "before"})
                proc.kill()
                proc.join()
                try:
                    await session.call_tool("echo", {"text": "after"})
                except BaseException as exc:
                    report("caller", exc)
                    return
                print("  caller: no exception")
    except BaseException as exc:
        report("streamable_http_client exit", exc)
    finally:
        if proc.is_alive():
            proc.kill()


async def scenario_external_cancel() -> None:
    proc, url = start_server()
    try:
        async with streamable_http_client(url) as (read, write):
            async with ClientSession(read, write) as session:
                await session.initialize()
                task = asyncio.create_task(session.call_tool("slow", {}))
                await asyncio.sleep(0.5)
                task.cancel()
                try:
                    await task
                except BaseException as exc:
                    report("caller", exc)
                    return
                print("  caller: no exception")
    except BaseException as exc:
        report("streamable_http_client exit", exc)
    finally:
        if proc.is_alive():
            proc.kill()


async def main() -> None:
    print(f"mcp {importlib.metadata.version('mcp')}")
    print("A: server killed mid-session")
    await scenario_server_dies()
    print("B: in-flight call cancelled externally")
    await scenario_external_cancel()


if __name__ == "__main__":
    multiprocessing.set_start_method("spawn")
    asyncio.run(main())

Output from running the above:

mcp 2.2.0
A: server killed mid-session
  caller:
    CancelledError: CancelledError('Cancelled via cancel scope 10bbefe30')
      WouldBlock: WouldBlock()
  streamable_http_client exit:
    ExceptionGroup: ExceptionGroup('unhandled errors in a TaskGroup', [ConnectError('All connection attempts failed')])
      | ConnectError: ConnectError('All connection attempts failed')
      CancelledError: CancelledError('Cancelled via cancel scope 10bbefe30')
        CancelledError: CancelledError("Cancelled via cancel scope 10bc90410 by <Task cancelling name='Task-1' ...>")
B: in-flight call cancelled externally
  caller:
    CancelledError: CancelledError()
      WouldBlock: WouldBlock()

Python & MCP Python SDK

Python 3.13.14, macOS arm64
mcp 2.2.0

Activity

  1. charles-dyfis-net commented on Sep 22, 2026

    @charles-dyfis-net
    Author

    I have a local patch I've been using to address this issue, and would be glad to submit it as a PR if y'all were to assign the ticket my way.

  2. added
    v2Affects the v2 line (2.x on main)
    v1Affects the v1.x maintenance line
    on Sep 22, 2026
  3. anilloutombam commented on Sep 27, 2026

    @anilloutombam

    I reproduced the same task-group failure pattern with mcp 2.2.0 while testing the published mcp-failure-lab@0.11.0 package.

    The session_loss tool interrupted its active legacy Streamable HTTP response. Across three runs:

    • the activation call was cancelled after 2.26 ms, 2.25 ms, and 2.30 ms;
    • calls on another client session owned by the same surrounding task context then failed with Connection closed or the same cancellation;
    • transport cleanup raised an ExceptionGroup each time;
    • reconnect could not be attempted inside the cancelled context.

    The equivalent TypeScript test kept the unrelated server session usable, which points to client task-group propagation rather than server-side session leakage.

    Full environment, method, and results: https://github.com/anilloutombam/mcp-failure-lab/blob/main/docs/compatibility/v0.11.0.md

  4. josendfa commented on Oct 13, 2026

    @josendfa

    Related: #1401 (which #3584 was filed against) and #3522 (the same teardown on 2.x, from a RemoteProtocolError).

    This also cancels every other request in flight on the session, not only the one whose POST raised, and on 1.x a per-call read_timeout_seconds turns it into a delayed failure of later requests. Reproduced on the newest release of both lines, 1.30.0 (httpx 0.28.1) and 2.3.0 (httpx2 2.13.1), and on 2.1.1, Python 3.13.

    Setup: a server in JSON response mode, so a POST's headers arrive only when its tool returns. The client's HTTP read timeout is 3 s. slow takes 8 s, medium 2.5 s, and echo is instant.

    A. A concurrent call outlives another call's read timeout. slow has no per-call timeout. medium is sent 1 s later and would finish at 3.5 s. At 3 s, slow's ReadTimeout escapes its POST task, and the transport's task group cancels everything:

                          1.30.0                                    2.3.0
    t=1.00 medium sent
    t=3.01 medium  ->     CancelledError                            CancelledError
    t=3.01 slow    ->     CancelledError                            CancelledError
    t=3.01 echo    ->     CancelledError                            MCPError('Connection closed')
           leaving streamable_http_client raises ExceptionGroup (both)
           [server] medium finished    <- the server completed the call the client was told was cancelled
    

    B (1.x). A call made after a per-call timeout. slow is called with read_timeout_seconds=0.5, and the caller gets its timeout at 0.5 s. The POST is not ended, though: it stays open until its HTTP read timeout. medium is sent at 1.5 s as a new request, and is cancelled at about 3 s, when the abandoned POST's ReadTimeout fires. So does echo. The server completes medium anyway. On 2.3.0 against a 2.3.0 server, B does not reproduce: in a session opened with initialize(), below the 2026-07-28 envelope, the client sends notifications/cancelled for slow and the server ends that request, so its POST never reaches the read timeout. (At the 2026-07-28 era the client aborts the POST locally instead.) It would still reproduce against any server that does not end a cancelled request's POST.

    Why it matters. A session carries concurrent tool calls, for example an agent's parallel tool use. Any one POST that sees a transport error before its headers (a slow tool against a read timeout, or a response lost in transit) cancels all the others. Each of those callers is told its call was cancelled while the server may have completed it. For a mutating tool that means a write that happened is reported as failed, and the caller cannot tell which. The session is then unusable, and the error that caused it only surfaces as an ExceptionGroup on exit.

    Where. 1.30.0: post_writer runs each request as tg.start_soon(handle_request_async) (client/streamable_http.py L587) in the transport's single task group (L672), and nothing in _handle_post_request (L355) catches the error from stream_within_origin (L361). 2.3.0: the same shape. _run_request_post (L346–361) wraps the POST in the _InFlightPost cancel scope, but in a try/finally with no except; it is started into the transport-wide group (L680, created at L771). The header read is at L370.

    Fix. The approach in #3584 (auto-closed, unreviewed) covers this: catch the transport error in _handle_post_request, resolve only that request (CONNECTION_CLOSED, naming the cause), and drop a failed notification without ending post_writer. Its tests include test_post_transport_error_on_one_call_leaves_a_concurrent_call_in_flight. A backport to v1.x would matter to anyone held on 1.x by a downstream pin.

    The analysis and the script were prepared with AI assistance; I reviewed both and ran the script myself.

    Reproduction script (runs unchanged on 1.x and 2.x)
    # /// script
    # requires-python = ">=3.11"
    # dependencies = ["mcp", "uvicorn"]
    # ///
    """A read timeout on one streamable-HTTP POST cancels every other request on the session.
    
    Runs unchanged against the 1.x line (FastMCP, httpx) and the 2.x line (MCPServer, httpx2):
    
        uv run --with "mcp==1.30.0" --with uvicorn python mcp_post_read_timeout_cascade.py
    
    The server answers in JSON mode, so a POST's response headers arrive only when its tool returns.
    The client's HTTP read timeout is READ_TIMEOUT seconds.
    
      A. `slow` (runs longer than READ_TIMEOUT, no per-call timeout) and, 1 s later, `medium`
         (2.5 s, inside its own read timeout, but still running when `slow`'s read timeout
         fires at 3 s). Then `echo` on the same session.
      B. `slow` with read_timeout_seconds=0.5: the caller gets its timeout at 0.5 s. Then
         `medium` is called after that, as a new request. Then `echo`. (On 2.x, in a session
         opened with initialize(), the client sends notifications/cancelled for `slow`, and a
         2.x server ends that request, so B does not reproduce against a 2.x server.)
    
    Expected in both: only `slow` fails; `medium` and `echo` succeed.
    The server prints when each tool actually finishes, so a call that completed on the server
    but failed on the client is visible.
    """
    
    from __future__ import annotations
    
    import asyncio
    import importlib.metadata
    import multiprocessing
    import socket
    import sys
    import time
    from datetime import timedelta
    from typing import Any
    
    import uvicorn
    from mcp import ClientSession
    from mcp.client.streamable_http import streamable_http_client
    
    MCP_VERSION = importlib.metadata.version("mcp")
    V2 = not MCP_VERSION.startswith("1.")
    if V2:
        import httpx2 as http
        from mcp.server import mcpserver  # type: ignore[attr-defined,unused-ignore]
    
        Server: Any = mcpserver.MCPServer
    else:
        import httpx as http  # type: ignore[no-redef,unused-ignore]
        from mcp.server.fastmcp import FastMCP
    
        Server = FastMCP
    
    HOST = "127.0.0.1"
    READ_TIMEOUT = 3.0
    SLOW_SECONDS = 8.0
    MEDIUM_SECONDS = 2.5
    SERVER_START_SECONDS = 30.0
    
    
    class Clock:
        """Seconds since the scenario's first call, for the printed timeline."""
    
        def __init__(self) -> None:
            """Start at zero."""
            self.start = time.monotonic()
    
        def reset(self) -> None:
            """Start again at zero."""
            self.start = time.monotonic()
    
        def stamp(self) -> str:
            """Return the elapsed time, formatted for the timeline."""
            return f"t={time.monotonic() - self.start:5.2f}s"
    
    
    def free_port() -> int:
        """Return a free loopback port."""
        with socket.socket() as sock:
            sock.bind((HOST, 0))
            return int(sock.getsockname()[1])
    
    
    def run_server(port: int) -> None:
        """Serve `slow`, `medium` and `echo` in JSON response mode, printing when each finishes."""
        # JSON response mode: 1.x takes it on the server, 2.x on the app.
        server: Any = Server("repro", log_level="CRITICAL", **({} if V2 else {"json_response": True}))
    
        async def slow() -> str:
            await asyncio.sleep(SLOW_SECONDS)
            print("    [server] slow finished", flush=True)
            return "slow done"
    
        async def medium() -> str:
            await asyncio.sleep(MEDIUM_SECONDS)
            print("    [server] medium finished", flush=True)
            return "medium done"
    
        def echo(text: str) -> str:
            return text
    
        for tool in (slow, medium, echo):
            server.tool()(tool)
        app = server.streamable_http_app(**({"json_response": True} if V2 else {}))
        uvicorn.run(app, host=HOST, port=port, log_level="critical")
    
    
    def start_server() -> tuple[multiprocessing.Process, str]:
        """Start the server in its own process and return it with its URL, once it listens."""
        port = free_port()
        proc = multiprocessing.Process(target=run_server, args=(port,), daemon=True)
        proc.start()
        deadline = time.monotonic() + SERVER_START_SECONDS
        while time.monotonic() < deadline:
            try:
                with socket.create_connection((HOST, port), timeout=0.25):
                    return proc, f"http://{HOST}:{port}/mcp"
            except OSError:
                time.sleep(0.05)
        message = "server did not come up"
        raise RuntimeError(message)
    
    
    def describe(result: object) -> str:
        """Return what a call came back with: a result's text, or the exception it raised."""
        if isinstance(result, BaseException):
            return f"FAILED {type(result).__name__}: {str(result)[:90]!r}"
        content = getattr(result, "content", None) or []
        text = getattr(content[0], "text", "") if content else ""
        error = getattr(result, "isError", None) or getattr(result, "is_error", None)
        return f"{'ERROR RESULT' if error else 'ok'} {text[:60]!r}"
    
    
    async def call(session: ClientSession, name: str, args: dict[str, Any], **kwargs: Any) -> object:  # noqa: ANN401
        """Call a tool and return its result, or the exception the caller received."""
        try:
            return await session.call_tool(name, args, **kwargs)
        except BaseException as exc:
            return exc
    
    
    async def scenario(label: str, deadline_on_slow: float | None) -> None:
        """Run one scenario on a fresh server and session, printing the timeline."""
        proc, url = start_server()
        clock = Clock()
        print(f"\n== {label}")
        try:
            async with (
                http.AsyncClient(timeout=http.Timeout(30.0, read=READ_TIMEOUT)) as client,
                streamable_http_client(url, http_client=client) as streams,  # type: ignore[arg-type,unused-ignore]
                ClientSession(streams[0], streams[1]) as session,
            ):
                await session.initialize()
                clock.reset()
                kwargs: dict[str, Any] = {}
                if deadline_on_slow is not None:
                    kwargs["read_timeout_seconds"] = (
                        deadline_on_slow if V2 else timedelta(seconds=deadline_on_slow)
                    )
                slow_task = asyncio.create_task(call(session, "slow", {}, **kwargs))
                if deadline_on_slow is not None:
                    slow = await slow_task
                    print(f"  {clock.stamp()} slow   -> {describe(slow)}")
                await asyncio.sleep(1.0)
                print(f"  {clock.stamp()} medium sent")
                medium = await call(session, "medium", {})
                print(f"  {clock.stamp()} medium -> {describe(medium)}")
                if deadline_on_slow is None:
                    slow = await slow_task
                    print(f"  {clock.stamp()} slow   -> {describe(slow)}")
                echo = await asyncio.wait_for(call(session, "echo", {"text": "still alive?"}), 10)
                print(f"  {clock.stamp()} echo   -> {describe(echo)}")
        except BaseException as exc:
            print(
                f"  {clock.stamp()} leaving the client raised {type(exc).__name__}: {str(exc)[:120]!r}"
            )
        finally:
            time.sleep(SLOW_SECONDS)  # let the server print what it finished
            proc.kill()
            proc.join()
    
    
    async def main() -> None:
        """Print the versions, then run both scenarios."""
        print(
            f"mcp {MCP_VERSION}, {http.__name__} {importlib.metadata.version(http.__name__)}, "
            f"Python {sys.version.split()[0]}, client read timeout {READ_TIMEOUT:g}s"
        )
        await scenario("A: a concurrent call outlives another call's read timeout", None)
        await scenario("B: a call made after another call's per-call timeout", 0.5)
    
    
    if __name__ == "__main__":
        asyncio.run(main())

    Run with: uv run --with "mcp==1.30.0" --with uvicorn python mcp_post_read_timeout_cascade.py (or mcp==2.3.0).

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