Skip to content

fix: gracefully terminate active sessions on shutdown#2181

Open
giulio-leone wants to merge 1 commit intomodelcontextprotocol:mainfrom
giulio-leone:fix/graceful-session-shutdown
Open

fix: gracefully terminate active sessions on shutdown#2181
giulio-leone wants to merge 1 commit intomodelcontextprotocol:mainfrom
giulio-leone:fix/graceful-session-shutdown

Conversation

@giulio-leone
Copy link

Summary

When shutting down a StreamableHTTPSessionManager (e.g. CTRL+C) while clients have active streaming connections, Uvicorn logs:

ERROR: ASGI callable returned without completing response.

The error occurs because the run() shutdown path cancels the task group without first gracefully terminating active SSE/streaming connections.

Fix

Before cancelling the task group in run()'s finally block, iterate through all active _server_instances and call terminate() on each transport. This gives each transport a chance to close its SSE connections properly, so clients receive a proper HTTP response instead of an abrupt disconnect.

Tests

Added test_shutdown_terminates_active_sessions — injects a mock transport and verifies terminate() is called during shutdown.

12 passed in 0.38s

Fixes #2150

During StreamableHTTPSessionManager shutdown, iterate through all active
transports and call terminate() before cancelling the task group. This
allows SSE connections to receive a proper HTTP response instead of being
abruptly dropped, which caused Uvicorn to log 'ASGI callable returned
without completing response'.

Fixes modelcontextprotocol#2150

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@giulio-leone
Copy link
Author

Friendly ping — CI is green and this is ready for review. Happy to address any feedback. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Active Streamable HTTP sessions are not terminated during shutdown

1 participant