fix: gracefully terminate active sessions on shutdown#2181
Open
giulio-leone wants to merge 1 commit intomodelcontextprotocol:mainfrom
Open
fix: gracefully terminate active sessions on shutdown#2181giulio-leone wants to merge 1 commit intomodelcontextprotocol:mainfrom
giulio-leone wants to merge 1 commit intomodelcontextprotocol:mainfrom
Conversation
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>
3ff7f5d to
6c44f2f
Compare
2 tasks
Author
|
Friendly ping — CI is green and this is ready for review. Happy to address any feedback. Thanks! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
When shutting down a
StreamableHTTPSessionManager(e.g. CTRL+C) while clients have active streaming connections, Uvicorn logs: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()'sfinallyblock, iterate through all active_server_instancesand callterminate()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 verifiesterminate()is called during shutdown.Fixes #2150