Skip to content

Streamable HTTP: ASGI application returns before the full SSE body is sent #3494

Open

Description

@jonpspri

Release line: 2.x (current stable) — also reproducible on the 1.x maintenance line, see below.

Description

On mcp 1.x and 2.x, a streamable-HTTP POST response can stop before the server sends the full SSE body. uvicorn writes ASGI callable returned without completing response and closes the connection. The client receives httpx.RemoteProtocolError: peer closed connection without sending complete message body. The failure occurs on the first initialize POST of a session. Session setup fails.

We observed this behavior in GitHub Actions and in controlled reproduction environments.

The failure rate grows with two factors: CPU contention, and repeated uvicorn server start/stop cycles in one process.

Verified results, each with a fresh server per attempt:

Experiment mcp 2.2.0 mcp 1.30.0
One server per process, moderate CPU load 24 of 25 runs failed 0 of 25 runs failed
20 servers in one process, 1 CPU limit 17 of 20 failed 17 of 20 failed
One server, 200 sequential POSTs 0 failed 0 failed

In the 20-server test, the first two or three servers pass. All later servers fail. One reused server never fails. This pattern points at state that accumulates across server restarts on one event loop.

Expected behavior

The server must always send the last body chunk of the response. The client must always receive the complete stream for a request that has an answer.

Suspected cause (inference)

This section is an inference from code reading.

  • The POST handler sends the response through an EventSourceResponse object.
  • The response runs several helper tasks in one shared task group.
  • The first task that ends cancels all other tasks in the group.
  • The stream task sets active = False before it sends the last body chunk.
  • If the last send operation waits, the disconnect task can end first and cancel the send.
  • The h11 receive() function in uvicorn returns immediately after it consumes the request body. This lets the disconnect task run in a loop.

Example Code

"""Repro: streamable-HTTP SSE response terminates mid-body."""
import anyio, httpx, uvicorn
from fastmcp import FastMCP

mcp = FastMCP("repro")

@mcp.tool()
def echo(message: str) -> str:
    return message

INIT = {
    "jsonrpc": "2.0", "id": 1, "method": "initialize",
    "params": {"protocolVersion": "2025-06-18", "capabilities": {},
               "clientInfo": {"name": "repro", "version": "0"}},
}
HEADERS = {"Accept": "application/json, text/event-stream"}

async def one_attempt() -> str | None:
    config = uvicorn.Config(mcp.http_app(transport="streamable-http"),
                            host="127.0.0.1", port=0, log_level="error")
    server = uvicorn.Server(config)
    async with anyio.create_task_group() as tg:
        tg.start_soon(server.serve)
        while not server.started:
            await anyio.sleep(0.05)
        port = server.servers[0].sockets[0].getsockname()[1]
        try:
            async with httpx.AsyncClient() as client:
                r = await client.post(f"http://127.0.0.1:{port}/mcp", json=INIT, headers=HEADERS)
                r.raise_for_status()
        except (httpx.HTTPError, RuntimeError) as error:
            return f"{type(error).__name__}: {error}"
        finally:
            server.should_exit = True
    return None

async def main() -> None:
    failures = 0
    for attempt in range(20):
        if (error := await one_attempt()) is not None:
            failures += 1
            print(f"attempt {attempt}: {error}")
    print(f"RESULT: {failures}/20 initialize POSTs failed")

anyio.run(main)

Run it in a CPU-limited container:

  1. Start a container: docker run --rm --cpus=1 -it python:3.13-slim bash.
  2. Install the dependencies: pip install fastmcp==4.0.3 uvicorn==0.52.4 httpx anyio.
  3. Run the script.

Expected result: 20 of 20 attempts pass. Actual result: approximately 17 of 20 attempts fail from the third or fourth server onwards.

Python & MCP Python SDK

Python 3.13; mcp 2.2.0 (newest 2.x) and mcp 1.30.0 (newest 1.x); fastmcp 4.0.3 / 3.4.7; uvicorn 0.52.4 (h11); starlette 1.6.0; anyio 4.14.2; httpx 0.28.1; Linux (Ubuntu 24.04, Debian slim; x86_64 and arm64). The closest existing issues (#2150, #3441) describe different defects.

Activity

  1. added
    v2Affects the v2 line (2.x on main)
    v1Affects the v1.x maintenance line
    on Sep 11, 2026
  2. Ben-Home commented on Sep 11, 2026

    @Ben-Home
  3. skulitom commented on Sep 11, 2026

    @skulitom

    Local investigation suggests a restart-specific dependency mechanism worth separating from the final-send race hypothesis.

    On SDK main 9972c21 / mcp 2.2.0, Windows, Python 3.11.5, AnyIO 4.10.0, Starlette 1.6.0, h11 0.16.0, httpx2/httpcore2 2.5.0, using real Uvicorn HTTP connections and protocolVersion=2025-06-18:

    sse-starlette Uvicorn Second initialization after stopping the first server
    3.4.11 0.52.4 RemoteProtocolError, incomplete chunked response
    3.0.2 0.52.4 Succeeds
    3.4.11 0.52.4, diagnostic reset of AppStatus.should_exit Succeeds

    The 3.4.11 watcher captures the first Uvicorn instance, then copies its shutdown state into global AppStatus.should_exit. That flag remains true for the second server, whose SSE exit listener immediately cancels the response. The probe records the actual captured server and waits for that watcher's shutdown broadcast; it does not simulate disconnects. Resetting the flag is only a causal control, not a recommended fix.

    Which sse-starlette version was installed in the reported runs? This reproduces the restart symptom, but does not yet explain the fresh-process CPU-load failures. Also, Uvicorn 0.52.4's h11 receive() waits on an event while the response is incomplete, so a receiver repeatedly returning empty requests would not reproduce that implementation.

    AI disclosure: Codex investigated the code, wrote and ran the diagnostic, and drafted this comment.

    Reproducer and commands

    Save as reproduce_restart.py in an SDK checkout. With uv >=0.9.5:

    uv run --frozen --with sse-starlette==3.4.11 --with uvicorn==0.52.4 python reproduce_restart.py
    uv run --frozen --with sse-starlette==3.4.11 --with uvicorn==0.52.4 python reproduce_restart.py --reset-between
    uv run --frozen --with uvicorn==0.52.4 python reproduce_restart.py
    """Diagnose MCP initialization after programmatic Uvicorn restarts.
    
    The private SSE exit listener observes the dependency's background watcher;
    it does not set the shutdown flag. --reset-between is a diagnostic control,
    not a proposed production workaround.
    """
    
    import argparse
    import json
    import platform
    import socket
    from functools import partial
    from importlib.metadata import version
    from unittest.mock import patch
    
    import anyio
    import httpx2
    import sse_starlette.sse as sse
    import uvicorn
    
    from mcp.server.mcpserver import MCPServer
    
    
    class ReadyServer(uvicorn.Server):
        def __init__(self, config: uvicorn.Config) -> None:
            super().__init__(config)
            self.ready = anyio.Event()
    
        async def startup(self, sockets: list[socket.socket] | None = None) -> None:
            await super().startup(sockets)
            self.ready.set()
    
    
    async def attempt(label: str) -> tuple[ReadyServer, dict[str, str | bool]]:
        app = MCPServer("restart-probe").streamable_http_app()
        server = ReadyServer(uvicorn.Config(app, log_level="error", lifespan="on"))
        with socket.socket() as listener, anyio.fail_after(5):
            listener.bind(("127.0.0.1", 0))
            port = listener.getsockname()[1]
            async with anyio.create_task_group() as tasks:
                tasks.start_soon(partial(server.serve, sockets=[listener]))
                with anyio.fail_after(5):
                    await server.ready.wait()
                observation: dict[str, str | bool] = {"phase": label, "exit_before": sse.AppStatus.should_exit}
                try:
                    async with httpx2.AsyncClient(trust_env=False, timeout=5) as client:
                        response = await client.post(
                            f"http://127.0.0.1:{port}/mcp",
                            headers={"Accept": "application/json, text/event-stream"},
                            json={
                                "jsonrpc": "2.0",
                                "id": 1,
                                "method": "initialize",
                                "params": {
                                    "protocolVersion": "2025-06-18",
                                    "capabilities": {},
                                    "clientInfo": {"name": "probe", "version": "1"},
                                },
                            },
                        )
                        response.raise_for_status()
                        data = next(line[6:] for line in response.text.splitlines() if line.startswith("data: "))
                        result = json.loads(data)
                        assert result["id"] == 1
                        assert result["result"]["protocolVersion"] == "2025-06-18"
                        observation["result"] = "initialize succeeded; complete HTTP response"
                except httpx2.HTTPError as error:
                    observation["result"] = f"{type(error).__name__}: {error}"
                finally:
                    server.should_exit = True
        print(json.dumps(observation), flush=True)
        return server, observation
    
    
    async def main(reset_between: bool) -> None:
        packages = ("mcp", "mcp-types", "sse-starlette", "uvicorn", "anyio", "starlette", "h11", "httpx2", "httpcore2")
        print(
            json.dumps(
                {
                    "python": platform.python_version(),
                    "platform": platform.system(),
                    "architecture": platform.machine(),
                    "packages": {name: version(name) for name in packages},
                }
            ),
            flush=True,
        )
        assert not sse.AppStatus.should_exit
        captured: list[object] = []
        capture_ready = anyio.Event()
        original_lookup = getattr(sse, "_get_uvicorn_server", None)
    
        def record_lookup() -> object:
            result = original_lookup()
            captured.append(result)
            capture_ready.set()
            return result
    
        # Observe which live server the existing watcher captures; preserve its return value.
        with patch.object(sse, "_get_uvicorn_server", record_lookup, create=True):
            first, first_result = await attempt("first")
            assert first_result["result"] == "initialize succeeded; complete HTTP response"
            if original_lookup is not None:
                with anyio.fail_after(5):
                    await capture_ready.wait()
                assert captured == [first]
                print(json.dumps({"watcher_captured_first_server": True, "exit_after_first": sse.AppStatus.should_exit}))
                # The watcher has already captured server #1. Observe its shutdown broadcast.
                with anyio.fail_after(5):
                    await sse.EventSourceResponse._listen_for_exit_signal()
                assert sse.AppStatus.should_exit
            else:
                print(json.dumps({"watcher_available": False, "exit_after_first": sse.AppStatus.should_exit}))
    
            if reset_between:
                sse.AppStatus.should_exit = False
                print(json.dumps({"diagnostic_reset": True}))
            await attempt("second")
    
    
    if __name__ == "__main__":
        parser = argparse.ArgumentParser(description=__doc__)
        parser.add_argument("--reset-between", action="store_true")
        args = parser.parse_args()
        anyio.run(main, args.reset_between)
  4. YS-OH-CORE commented on Sep 12, 2026

    @YS-OH-CORE

    I isolated the final-send ordering hypothesis on sse-starlette current main 6754ef387da97cf6cfbcd1bd5c216b533937b304, separately from the restart/global-state mechanism in the preceding comment.

    Probes, exact sources and candidate diff · Executed run34703822255

    The trigger is explicit scheduling instrumentation: wait to deliver the first ordinary receive event until the final ASGI send begins, and delay that send by20ms. The minimal probe returns one http.request then blocks. A second probe uses a real Uvicorn/h11 + HTTPX localhost connection and forwards real server receive events through the same barrier. No repeated empty-receive loop is used.

    Same probe Baseline Move active=False after final awaited send
    Normal response Complete Complete
    Delayed final send, normal request event Final send cancelled Final send completes
    Supplied disconnect event during final send Cancellation and close callback once Same behavior
    Instrumented localhost HTTP RemoteProtocolError; Uvicorn logs incomplete response HTTP200, exact Korean SSE body

    The listener returns because active is already false; cancel_on_finish then cancels the pending final send. Moving the completion flag after the send, inside the existing lock, fixes this selected interleaving without shielding cancellation. The original59 cases in test_sse.py, test_issue167.py, and test_event.py pass unchanged on both variants.

    All eight probe executions use fresh processes. Both HTTP cases have AppStatus.should_exit=False before stopping their only server, so this case does not require a stale global shutdown flag. Versions: sse-starlette3.4.11 source, AnyIO4.15.1, Starlette1.6.0, Uvicorn0.52.4, HTTPX0.28.1, hosted Linux/Python3.12.

    Scope: this demonstrates a separate final-send failure under controlled scheduling. It does not yet show that this interleaving explains the original fresh-process CPU-load failures or the reported restart frequency. No actual MCP initialize request is used. The SSE application payload may already be sent; the missing message here is the final HTTP completion. The supplied-disconnect control is an ASGI event test, not a physical socket-close test.

    The downloaded artifact10301616324 contains actual observations, client/server logs, original-test XMLs and the one-ordering patch; SHA256 d6a1108f4287b546bd57c0adbcd7505c45763ee2749ad0acb95d40f58f17d50d. Original issue/hypothesis credit stays with jonpspri and the preceding restart analysis with skulitom. AI disclosure: prepared and executed with Zero (ChatGPT).


    Follow-up, 2026-09-13 KST: this candidate does not repair the tested MCP restart failure.

    I crossed the same unchanged patch with actual mcp==2.2.0 initialization over Uvicorn/h11 HTTP, now forwarding receives immediately and unchanged (only final send delayed20ms). Countercheck and exact conditions · Run34705469328.

    Baseline and candidate outcomes are identical: first initialization succeeds; after observing the real watcher carry server1's shutdown state forward, server2 fails with RemoteProtocolError; resetting only that flag as a diagnostic restores initialization on both. The two restart failures occur before any final-send attempt, with AppStatus.should_exit=True. All ten requests consume the ordinary POST body before response start.

    This supports skulitom's separate restart explanation and limits what my earlier plain-SSE result establishes. The final-send ordering patch is not a solution for that restart path. The flag reset remains diagnostic, not a recommended fix. These six fresh-process scenarios do not reproduce the original CPU-contention frequency. Both failed requests remain failures in the evidence; the completed comparison is not an all-passing-initializations claim. I appended this here rather than creating another issue or repeating the original result.

  5. vobornik commented on Sep 27, 2026

    @vobornik

    Independent confirmation of the restart mechanism from the first comment, on the 1.x line: mcp 1.30.0 (and 1.28.1), sse-starlette 3.4.5, uvicorn 0.50.0, Python 3.12, Linux. Our test suite starts a real uvicorn server per test in one process. The first initialize POST after an earlier server had stopped hung with incomplete chunked read, 4 of 6 runs on 1.28.1 and 6 of 6 on 1.30.0. At the hang, sse_starlette.sse.AppStatus.should_exit was True.

    A standalone reproducer without MCP bisects the regression to sse-starlette 3.1.1 (PR sysid/sse-starlette#151). 3.1.0 and earlier are fine, and 3.1.1 through 3.4.11 fail on every run. I've filed it upstream as sysid/sse-starlette#211 with the script.

    For test harnesses that start several servers in one process, sse-starlette's public AppStatus.disable_automatic_graceful_drain() removes the latch. Called once before the first server starts, it made our suite pass deterministically (20 consecutive runs of the full suite and of the end-to-end file). We also have a regression test that forces the watcher to observe a shutdown and fails without it. This doesn't address the separate final-send race from the second comment, which does not need a restart.

  6. khuisman commented on Oct 10, 2026

    @khuisman

    Another trigger for the same ASGI callable returned without completing response, on mcp 2.3.0 / sse-starlette 3.4.4 / uvicorn 0.47.0: a SIGTERM while a client holds the standalone GET /mcp stream open.

    Repro: start a streamable-http server, POST initialize then notifications/initialized, open GET /mcp (Accept: text/event-stream + the session ID), wait ~2s, SIGTERM the server, read the GET to EOF. The client gets IncompleteRead(0 bytes read) and uvicorn logs the error above.

    Cause: on shutdown EventSourceResponse sees AppStatus.should_exit and cancels its task group before sending its closing http.response.body (more_body=False). _handle_get_request then returns normally, so the response is left started but never finished. The SSE-mode POST and replay responses end the same way.

    We're working around it with an ASGI wrapper: if the app returns while AppStatus.should_exit is set and the response has started but isn't complete, it sends {"type": "http.response.body", "body": b"", "more_body": False}. A fix closer to the source would be for EventSourceResponse to send its closing body even when the shutdown signal cancels it.

  7. ayushmnnit commented on Nov 21, 2026

    @ayushmnnit

    I tried to separate the mechanisms in this thread with deterministic repros (scripts below). All three are in sse-starlette's EventSourceResponse, and they share the symptom: http.response.start goes out, more_body=False never does.

    1. Restarts in one process (the 20-servers table). This is the AppStatus.should_exit latch, sysid/sse-starlette#211, present in 3.1.1 to 3.4.11 and fixed in 3.5.0. Sequential uvicorn servers in one loop, one initialize each, on main and on mcp 1.30.0: with 3.4.11, every server after the watcher has seen one stop fails (5/6; with no gap between servers it is 3/6, the "first two or three pass" pattern). With 3.5.0 and 3.7.0, 0/6. pyproject.toml allows sse-starlette>=3.0.0, so installs can still resolve a broken version, and uv.lock pins 3.0.2, so CI never sees it. I'd suggest raising the floor to >=3.5.0 and the lock to 3.7.0. With 3.7.0 locked, the full suite passes (6232 passed), plus ruff and pyright. Same one-line change would apply to v1.x if you want it there.

    2. Shutdown truncation (khuisman's report). Still present on sse-starlette 3.7.0. When the exit signal fires, the task group is cancelled before _stream_response sends the closing chunk, and __call__ returns with the response half sent. That is also why the latch shows up as incomplete chunked read and not as an empty stream. Repro: one server, a 3 s tool, SIGTERM at 0.5 s gives RemoteProtocolError plus uvicorn's "ASGI callable returned without completing response". The SDK client mostly tolerates it (it ends up in the same "stream ended without a response" path), but other clients see a protocol error.

    3. Final-send race (active = False before the closing send). Reproducible at the ASGI level, but I couldn't reach it through uvicorn 0.35 or 0.52.4 (h11 and httptools). Even with the closing send delayed 20 ms, receive() never returned during the response; the only message was http.disconnect after completion. It needs a receive() that returns http.request after more_body=False, which ASGI doesn't allow. So I don't think it explains the CI failures.

    I couldn't reproduce the fresh-process CPU-load failures (0/25 with 3.4.11 and with 3.7.0, 20 busy loops on 10 cores). @jonpspri, which sse-starlette version was installed for the "one server per process" runs, and did those runs start more than one server?

    2 and 3 belong upstream, so I opened sysid/sse-starlette#235 for them (4 new tests fail before and pass after; the unit suite passes). On the SDK side I'm happy to send the floor bump if you'd like it.

    Repro scripts

    repro_restart.py:

    """Mechanism A: sse-starlette's process-global shutdown latch across uvicorn restarts.
    
    Starts N uvicorn servers one after another in one process and event loop, sends one
    `initialize` POST to each, then stops it. Between servers it waits `--gap` seconds.
    sse-starlette's shutdown watcher polls every 0.5 s, so a gap > 0.5 s guarantees the
    watcher sees the stopped server's `should_exit=True` before the next server starts.
    """
    
    import argparse
    import json
    import logging
    import socket
    from importlib.metadata import version
    
    import anyio
    try:
        import httpx2 as httpx  # v2 depends on httpx2
    except ImportError:
        import httpx
    import uvicorn
    from sse_starlette.sse import AppStatus
    
    try:
        from mcp.server.mcpserver import MCPServer as Server  # v2 (main)
    except ImportError:
        from mcp.server.fastmcp import FastMCP as Server  # v1.x
    
    INIT = {
        "jsonrpc": "2.0",
        "id": 1,
        "method": "initialize",
        "params": {"protocolVersion": "2025-06-18", "capabilities": {}, "clientInfo": {"name": "r", "version": "0"}},
    }
    HEADERS = {"Accept": "application/json, text/event-stream", "Content-Type": "application/json"}
    
    incomplete_logs: list[str] = []
    
    
    class Capture(logging.Handler):
        def emit(self, record: logging.LogRecord) -> None:
            if "without completing response" in record.getMessage():
                incomplete_logs.append(record.getMessage())
    
    
    logging.getLogger("mcp").setLevel(logging.WARNING)
    
    
    async def one_server(i: int) -> str:
        app = Server(f"restart-{i}").streamable_http_app()
        server = uvicorn.Server(uvicorn.Config(app, log_level="error", lifespan="on"))
        # uvicorn.Config reconfigures its loggers, so attach the capture handler afterwards.
        uv_log = logging.getLogger("uvicorn.error")
        if not any(isinstance(h, Capture) for h in uv_log.handlers):
            uv_log.addHandler(Capture())
        sock = socket.socket()
        sock.bind(("127.0.0.1", 0))
        port = sock.getsockname()[1]
        latched_before = AppStatus.should_exit
        async with anyio.create_task_group() as tg:
            tg.start_soon(lambda: server.serve(sockets=[sock]))
            while not server.started:
                await anyio.sleep(0.01)
            try:
                async with httpx.AsyncClient(timeout=5) as client:
                    r = await client.post(f"http://127.0.0.1:{port}/mcp", json=INIT, headers=HEADERS)
                    r.raise_for_status()
                    assert '"protocolVersion"' in r.text, r.text
                    result = "ok"
            except Exception as e:  # noqa: BLE001
                result = f"FAIL {type(e).__name__}: {e}"
            finally:
                server.should_exit = True
        return f"server {i}: AppStatus.should_exit before={latched_before} -> {result}"
    
    
    async def main(n: int, gap: float) -> None:
        print(json.dumps({p: version(p) for p in ("mcp", "sse-starlette", "uvicorn", "starlette", "anyio")}))
        fails = 0
        for i in range(n):
            line = await one_server(i)
            fails += "FAIL" in line
            print(line)
            await anyio.sleep(gap)
        print(f"RESULT {fails}/{n} failed; uvicorn 'without completing response' logs: {len(incomplete_logs)}")
    
    
    if __name__ == "__main__":
        p = argparse.ArgumentParser()
        p.add_argument("-n", type=int, default=4)
        p.add_argument("--gap", type=float, default=1.0)
        a = p.parse_args()
        anyio.run(main, a.n, a.gap)

    repro_shutdown.py:

    """Mechanism C: graceful shutdown truncates an in-flight SSE response.
    
    One server, one process, sse-starlette at any version. A client calls a tool that takes
    3 s; 0.5 s in, the server is asked to exit (what SIGTERM does). sse-starlette's exit
    listener cancels the response task group before `_stream_response` sends the closing
    `more_body=False` chunk, so the ASGI app returns with the response half-sent.
    """
    
    import json
    import os
    import signal
    import sys
    import logging
    import socket
    from importlib.metadata import version
    
    import anyio
    import uvicorn
    
    try:
        import httpx2 as httpx
    except ImportError:
        import httpx
    
    try:
        from mcp.server.mcpserver import MCPServer as Server
    except ImportError:
        from mcp.server.fastmcp import FastMCP as Server
    
    logging.getLogger("mcp").setLevel(logging.WARNING)
    H = {"Accept": "application/json, text/event-stream", "Content-Type": "application/json"}
    
    
    def rpc(id_: int | None, method: str, params: dict) -> dict:
        msg = {"jsonrpc": "2.0", "method": method, "params": params}
        if id_ is not None:
            msg["id"] = id_
        return msg
    
    
    async def main() -> None:
        print(json.dumps({p: version(p) for p in ("mcp", "sse-starlette", "uvicorn")}), flush=True)
        mcp = Server("shutdown")
    
        @mcp.tool()
        async def slow() -> str:
            await anyio.sleep(3)
            return "done"
    
        server = uvicorn.Server(uvicorn.Config(mcp.streamable_http_app(), log_level="error", lifespan="on"))
        sock = socket.socket()
        sock.bind(("127.0.0.1", 0))
        base = f"http://127.0.0.1:{sock.getsockname()[1]}/mcp"
        async with anyio.create_task_group() as tg:
            tg.start_soon(lambda: server.serve(sockets=[sock]))
            while not server.started:
                await anyio.sleep(0.01)
            async with httpx.AsyncClient(timeout=10) as c:
                r = await c.post(base, headers=H, json=rpc(1, "initialize", {
                    "protocolVersion": "2025-06-18", "capabilities": {}, "clientInfo": {"name": "r", "version": "0"}}))
                h = {**H, "mcp-session-id": r.headers["mcp-session-id"], "mcp-protocol-version": "2025-06-18"}
                await c.post(base, headers=h, json=rpc(None, "notifications/initialized", {}))
    
                async def stop_soon() -> None:
                    await anyio.sleep(0.5)
                    if "--sigterm" in sys.argv:
                        os.kill(os.getpid(), signal.SIGTERM)
                    else:
                        server.should_exit = True  # the flag uvicorn's SIGTERM handler sets
    
                tg.start_soon(stop_soon)
                try:
                    r = await c.post(base, headers=h, json=rpc(2, "tools/call", {"name": "slow", "arguments": {}}))
                    print(f"status={r.status_code} body={r.text!r}  -> stream closed cleanly", flush=True)
                except httpx.HTTPError as e:
                    print(f"FAIL {type(e).__name__}: {e}", flush=True)
    
    
    anyio.run(main)

    repro_final_send.py:

    """Mechanism B: final-send race inside sse-starlette's EventSourceResponse.
    
    `_stream_response` sets `self.active = False` and only then awaits the closing
    `more_body=False` send. `_listen_for_disconnect` loops `while self.active: await receive()`.
    If receive() hands back any non-disconnect message while that final send is suspended,
    the listener sees active=False, returns, and `cancel_on_finish` cancels the pending
    final send. The ASGI app then returns with the response unfinished.
    
    Two parts:
      asgi     Drive EventSourceResponse directly. Deterministic: the final send waits until
               the listener has run, and receive() returns one extra `http.request` event.
      uvicorn  Real uvicorn + MCP initialize, with an ASGI wrapper that delays the final send
               by 20 ms. Optional --chatty-receive makes receive() return an empty
               `http.request` once the final send has started. Prints how many times the
               app's receive() returned while the response was in flight.
    """
    
    import argparse
    import json
    import socket
    from importlib.metadata import version
    
    import anyio
    from sse_starlette import EventSourceResponse
    
    FINAL = {"type": "http.response.body", "body": b"", "more_body": False}
    
    
    async def asgi_case(extra_event: str) -> None:
        final_started = anyio.Event()
        listener_ran = anyio.Event()
        sent: list[dict] = []
        calls = 0
    
        async def receive() -> dict:
            nonlocal calls
            calls += 1
            if calls == 1:
                return {"type": "http.request", "body": b"{}", "more_body": False}
            if calls == 2:
                await final_started.wait()
                listener_ran.set()
                if extra_event == "disconnect":
                    return {"type": "http.disconnect"}
                return {"type": "http.request", "body": b"", "more_body": False}
            await anyio.sleep_forever()
    
        async def send(message: dict) -> None:
            if message == FINAL:
                final_started.set()
                await listener_ran.wait()  # the "slow" final write: yields until the listener has run
                await anyio.sleep(0)
            sent.append(message)
    
        async def events():
            yield {"event": "message", "data": "the JSON-RPC response"}
    
        scope = {"type": "http", "method": "POST", "headers": []}
        await receive()  # the app consumed the request body before building the response
        await EventSourceResponse(events(), ping=0)(scope, receive, send)
        print(f"  asgi, extra receive event={extra_event!r}: final more_body=False sent: {FINAL in sent}")
    
    
    async def uvicorn_case(chatty: bool) -> None:
        import uvicorn
    
        try:
            import httpx2 as httpx
        except ImportError:
            import httpx
        try:
            from mcp.server.mcpserver import MCPServer as Server
        except ImportError:
            from mcp.server.fastmcp import FastMCP as Server
    
        stats = {"receive_returned_during_response": [], "final_sent": False}
        inner = Server("race").streamable_http_app()
    
        async def app(scope, receive, send):
            if scope["type"] != "http" or scope["method"] != "POST":
                return await inner(scope, receive, send)
            final_started = anyio.Event()
            state = {"started": False}
    
            async def wrapped_receive():
                if state["started"] and chatty:
                    await final_started.wait()
                    stats["receive_returned_during_response"].append("http.request (injected)")
                    return {"type": "http.request", "body": b"", "more_body": False}
                msg = await receive()
                if state["started"]:
                    stats["receive_returned_during_response"].append(msg["type"])
                return msg
    
            async def wrapped_send(message):
                if message["type"] == "http.response.start":
                    state["started"] = True
                if message.get("type") == "http.response.body" and not message.get("more_body", False):
                    final_started.set()
                    await anyio.sleep(0.02)  # stand-in for a slow write or a send-wrapping middleware
                    await send(message)
                    stats["final_sent"] = True
                    return
                await send(message)
    
            await inner(scope, wrapped_receive, wrapped_send)
    
        server = uvicorn.Server(uvicorn.Config(app, log_level="critical", lifespan="on"))
        sock = socket.socket()
        sock.bind(("127.0.0.1", 0))
        port = sock.getsockname()[1]
        async with anyio.create_task_group() as tg:
            tg.start_soon(lambda: server.serve(sockets=[sock]))
            while not server.started:
                await anyio.sleep(0.01)
            try:
                async with httpx.AsyncClient(timeout=5) as c:
                    r = await c.post(
                        f"http://127.0.0.1:{port}/mcp",
                        headers={"Accept": "application/json, text/event-stream", "Content-Type": "application/json"},
                        json={"jsonrpc": "2.0", "id": 1, "method": "initialize", "params": {
                            "protocolVersion": "2025-06-18", "capabilities": {},
                            "clientInfo": {"name": "r", "version": "0"}}},
                    )
                    result = f"HTTP {r.status_code}, complete body ({len(r.content)} bytes)"
            except httpx.HTTPError as e:
                result = f"FAIL {type(e).__name__}: {e}"
            finally:
                server.should_exit = True
        print(f"  uvicorn, chatty_receive={chatty}: {result}; final sent={stats['final_sent']}; "
              f"receive() returned during response: {stats['receive_returned_during_response']}")
    
    
    async def main(part: str) -> None:
        print(json.dumps({p: version(p) for p in ("sse-starlette", "uvicorn", "anyio")}))
        if part in ("asgi", "all"):
            await asgi_case("request")
            await asgi_case("disconnect")
        if part in ("uvicorn", "all"):
            await uvicorn_case(chatty=False)
            await uvicorn_case(chatty=True)
    
    
    if __name__ == "__main__":
        p = argparse.ArgumentParser()
        p.add_argument("part", nargs="?", default="all", choices=["asgi", "uvicorn", "all"])
        anyio.run(main, p.parse_args().part)

    AI disclosure: I used Claude Code to write and run the repro scripts, read the sse-starlette and uvicorn code paths, and draft this comment. I reviewed the scripts, the results and the conclusions myself.

  8. ayushmnnit commented on Nov 23, 2026

    @ayushmnnit

    Update: sysid merged sysid/sse-starlette#235 and released it as 3.7.1, so items 2 and 3 from my earlier comment are fixed upstream too (item 1 was fixed in 3.5.0).

    I re-ran the two repros from that comment:

    sse-starlette restarts in one process (repro_restart.py -n 6) shutdown during a tool call (repro_shutdown.py, and with --sigterm)
    3.7.0 0/6 failed RemoteProtocolError: incomplete chunked read, uvicorn logs "ASGI callable returned without completing response"
    3.7.1 0/6 failed HTTP 200, stream closed cleanly, no uvicorn error

    On shutdown the client now gets a clean end of stream with no result for the interrupted call, instead of a protocol error.

    So the SDK side is one line: sse-starlette>=3.7.1 instead of >=3.0.0, and the lock from 3.0.2 to 3.7.1. Every sse-starlette from 3.1 on already requires starlette>=0.49.1, so this floor raises the effective starlette floor no further than 3.5.0 would have. With it the full suite passes locally (6232 passed, 9 skipped, 1 xfailed). The commit is at ayushmnnit/python-sdk@478ff7a6 if you'd like it as a PR. v1.x allows >=1.6.1 and could take the same bump.

    AI disclosure: I used Claude Code to re-run the repros and the test suite and to draft this comment. I checked the results myself.

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