python-sdk icon indicating copy to clipboard operation
python-sdk copied to clipboard

SSE: a POST blocks forever (and SIGTERM hangs) when the server lifespan raises after an await

Open khuisman opened this issue 2 months ago • 4 comments

Summary

With the SSE transport, if the server's lifespan raises after its first await, a client POST /messages/ that arrives in the meantime is answered 202 and then blocks forever in handle_post_message. On SIGTERM, uvicorn then waits on that request task indefinitely (Waiting for background tasks to complete.).

Cause

SseServerTransport.connect_sse creates the per-session streams with buffer size 0, and handle_post_message does await writer.send(session_message) after responding 202. That send completes only when Server.run receives the message or the stream is closed.

The stream ends are closed only in response_wrapper, after EventSourceResponse returns normally:

await EventSourceResponse(...)(scope, receive, send)
await read_stream_writer.aclose()
await write_stream_reader.aclose()
await sse_stream_reader.aclose()

If Server.run raises (here, from the lifespan, before the read loop starts), the task group cancels response_wrapper before those aclose() calls run. connect_sse's finally only removes the session from _read_stream_writers/_session_owners. Nothing ever receives from or closes the stream, so the parked send() never returns.

The await matters for timing. It lets the response task send the endpoint event before the lifespan fails, so a client that POSTs immediately (as MCP clients do) lands in the window. A lifespan that raises without awaiting fails before the endpoint event is sent, so the client never POSTs.

Repro (mcp 2.0.0, uvicorn 0.47.0, Python 3.13, macOS)

server.py:

import asyncio
from contextlib import asynccontextmanager

from mcp.server.mcpserver import MCPServer


@asynccontextmanager
async def lifespan(server):
    await asyncio.sleep(0.1)  # any await before failing, e.g. loading credentials
    raise RuntimeError("startup failed")
    yield


mcp = MCPServer("repro", lifespan=lifespan)

if __name__ == "__main__":
    mcp.run(transport="sse", host="127.0.0.1", port=8765)

client.py: POSTs initialize as soon as the endpoint event arrives:

import json
import re
import socket
import urllib.request

s = socket.create_connection(("127.0.0.1", 8765))
s.sendall(b"GET /sse HTTP/1.1\r\nHost: 127.0.0.1:8765\r\nAccept: text/event-stream\r\n\r\n")
buf = b""
while not (m := re.search(rb"session_id=([0-9a-f]{32})", buf)):
    buf += s.recv(4096)
body = json.dumps({"jsonrpc": "2.0", "id": 1, "method": "initialize", "params": {
    "protocolVersion": "2025-06-18", "capabilities": {},
    "clientInfo": {"name": "repro", "version": "0"}}}).encode()
req = urllib.request.Request(
    f"http://127.0.0.1:8765/messages/?session_id={m.group(1).decode()}",
    data=body, headers={"Content-Type": "application/json"},
)
print(urllib.request.urlopen(req).status)  # 202

Steps: run python server.py, run python client.py (prints 202), then send the server SIGTERM.

Expected: the server exits. Actual: Waiting for background tasks to complete. (CTRL+C to force quit), indefinitely. 5 of 5 runs.

A task dump of the hung server shows the POST request task parked here:

mcp/server/sse.py:262 handle_post_message
mcp/shared/_context_streams.py:37 send
anyio/streams/memory.py:256 send

Possible fix

Close the session's stream ends (or at least read_stream_writer) in connect_sse's finally, so that a pending send() raises instead of waiting forever when the body exits for any reason.

Related

#514 shows the same symptom (SSE server stuck on "Waiting for background tasks to complete" at shutdown) with a different trigger: an open session after normal requests. This case needs no open session or handled requests, only a lifespan failure after an await.

khuisman avatar Oct 02 '26 05:10 khuisman

Reproduced the trace on main and confirmed every step. src/mcp/server/sse.py:

read_stream_writer, read_stream = create_context_streams[SessionMessage | Exception](0)
write_stream, write_stream_reader = create_context_streams[SessionMessage](0)

Both zero-buffer, as described. And the finally at the end of connect_sse:

finally:
    self._read_stream_writers.pop(session_id, None)
    self._session_owners.pop(session_id, None)

— dict cleanup only. The aclose() calls live in response_wrapper, after await EventSourceResponse(...)(scope, receive, send), so task-group cancellation on lifespan failure skips them. The parked await writer.send(...) has no counterparty to wake it. Agreed on all of it.

One extra path that doesn't need a failing lifespan at all. The malformed-body branch in handle_post_message does the same unbounded send:

except ValidationError as err:
    ...
    await response(scope, receive, send)
    await writer.send(err)
    return

So a client that opens a session and then POSTs invalid JSON parks that request forever on the same zero-buffer stream — no lifespan exception, no SIGTERM, no race. A client that simply retries a truncated or mis-encoded body hits it. On a default-1-buffer stream this would park at the first message instead of only after the endpoint event, which is why I think the zero buffer is what makes it look lifespan-specific.

Two questions that decide where the fix belongs:

  1. Should the streams get a small non-zero buffer (create_context_streams(1)), or should the POST side use send_nowait and 503 when the session isn't draining? The first keeps 202-then-deliver semantics; the second makes backpressure visible instead of hanging. I lean toward the second for the error path regardless, since an error response doesn't need to queue behind a stuck session.
  2. Is closing the streams in connect_sse's finally (rather than only in response_wrapper) safe with respect to the normal disconnect path, or does the task group rely on response_wrapper owning those closes?

Happy to take this if assigned — AGENTS.md closes unassigned PRs, and I'd rather not open one blind. I can also add a regression test that asserts a POST against a session whose lifespan has died returns (rather than hanging) under a anyio.fail_after.

i20000109 avatar Oct 02 '26 06:10 i20000109

Confirming the sequence matters here, and the await is the whole story. The window exists because the lifespan yields control: the response task gets to send the endpoint event before the lifespan fails, so a client that POSTs immediately (which is what MCP clients do) lands squarely in the gap.

The structural problem is that the stream teardown lives on the success path. response_wrapper closes the streams only after EventSourceResponse returns normally, but when Server.run raises, the task group cancels response_wrapper before those aclose() calls are reached. connect_sse's finally only unregisters the session, so nothing ever drains or closes the streams and the parked await writer.send(...) never returns.

Two fix shapes, and they are complementary:

  1. Make teardown a property of the session, not of one code path. Own the read/write stream ends in a context manager entered by connect_sse itself, so they are closed whenever the session exits, cancelled or not. That removes the dependency on response_wrapper reaching its last line.
  2. Do not hold the response open on a send that may never drain. handle_post_message currently answers 202 and then awaits the write. Either push into the read stream non-blockingly with a bounded queue, or complete the 202 and let the write be fire-and-forget with the stream's closure as the failure signal. A 202 that can block indefinitely contradicts what 202 is supposed to mean to the client.

The SIGTERM hang is the same defect surfacing at shutdown: uvicorn waits on a request task that is waiting on a stream that will never close. Fix 1 resolves it because cancellation then closes the streams and the send returns.

This class, teardown owned by the happy path while cancellation bypasses it, is one we dealt with on the connector side when a client's connection was cancelled mid-handshake and the session leaked. Moving stream ownership into the session's own lifecycle fixed both the leak and the shutdown hang. More at https://corpusiq.io.

Ben-Home avatar Oct 02 '26 12:10 Ben-Home

Reproduced on main: with a lifespan that awaits before failing, the client POSTs initialize, gets 202, and the request task stays parked in writer.send at src/mcp/server/sse.py:287. My 2 s SIGTERM wait catches uvicorn's Waiting for background tasks to complete; the aclose() calls that would wake the sender live in response_wrapper after EventSourceResponse, so task-group cancellation skips them.

The zero-buffer streams are created at src/mcp/server/sse.py:152. Closing read_stream_writer in connect_sse's finally — or propagating lifespan failure to cancel in-flight request tasks — unblocks shutdown. Happy to open a PR if you want me to take it.

Disclosure: I contribute to thyn-ai/Algenta (open-source agent tooling).

0xamlab avatar Oct 03 '26 18:10 0xamlab

I’m working through this issue with AI assistance and would like to contribute a focused fix, coordinating with the reporter and prior investigations.

On main at 77001c96, an event-gated regression confirms that the POST receives 202/400 while its ASGI task remains blocked. After failed or cancelled startup, closing the receiving end of the input stream releases the waiting sender; closing only the sending end does not.

The proposed patch closes the session-owned stream endpoints in connect_sse’s finally and narrowly handles closed/broken-stream errors during POST delivery after the response is sent. It keeps the existing status codes, zero-buffer backpressure and public APIs.

Four cases (initialize/invalid JSON × startup failure/cancellation) fail on the base and pass with the patch. They check ASGI completion, original exception identity and stale-session 404, without sleeps or harness cancellation hiding a leak. Local full-suite validation is 6,235 passed (10 skipped, one existing xfailed) with 100% coverage and strict-no-cover clean; two unrelated macOS Pyright diagnostics also occur on the base.

Would this approach fit, and would you consider assigning this issue to @Christiansada so I can open the PR?

Disclosure: AI assisted the investigation, patch and test execution. These are local regression results, not a claim of production use.

Christiansada avatar Nov 13 '26 11:11 Christiansada