Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/mcp/server/streamable_http.py
Original file line number Diff line number Diff line change
Expand Up @@ -784,6 +784,12 @@ async def terminate(self) -> None:
self._terminated = True
logger.info(f"Terminating session: {self.mcp_session_id}")

# Close all SSE stream writers so that active EventSourceResponse
# coroutines complete gracefully instead of being cancelled mid-stream.
for writer in list(self._sse_stream_writers.values()): # pragma: no cover
writer.close()
self._sse_stream_writers.clear()

# We need a copy of the keys to avoid modification during iteration
request_stream_keys = list(self._request_streams.keys())

Expand Down
8 changes: 8 additions & 0 deletions src/mcp/server/streamable_http_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,14 @@ async def lifespan(app: Starlette) -> AsyncIterator[None]:
yield # Let the application run
finally:
logger.info("StreamableHTTP session manager shutting down")
# Gracefully terminate all active sessions before cancelling
# tasks so that EventSourceResponse coroutines can complete
# and Uvicorn does not log ASGI-incomplete-response errors.
for transport in list(self._server_instances.values()): # pragma: no cover
try:
await transport.terminate()
except Exception: # pragma: no cover
logger.exception("Error terminating transport during shutdown")
# Cancel task group to stop all spawned tasks
tg.cancel_scope.cancel()
self._task_group = None
Expand Down
Loading