fix: terminate active StreamableHTTP sessions during shutdown
Approval required
WE
6mo ago+14+14+01234567890123456789-0-0-0123456789
Summary
Fixes #2150 - StreamableHTTP sessions were not properly terminating active HTTP sessions during shutdown.
This PR addresses two root causes identified in the issue:
- StreamableHTTPSessionManager.run() was directly canceling the task group without first calling
terminate()on active transport instances - StreamableHTTPServerTransport.terminate() was only closing
_request_streams, leaving_sse_stream_writersunclosed
Changes
streamable_http_manager.py
- Added explicit
terminate()calls for all active server instances before canceling the task group in thefinallyblock ofrun() - This ensures proper cleanup of all transport resources before task cancellation
streamable_http.py
- Enhanced
terminate()method to close all SSE stream writers - Iterates through
_sse_stream_writersand callsclose()on each writer before clearing the dictionary - Maintains idempotent behavior - safe to call multiple times
Impact
- All active HTTP connections and streams are now properly closed during shutdown
- Prevents resource leaks
- Ensures clean termination of StreamableHTTP sessions
Testing
- Existing tests pass, including
test_session_termination - No new tests added as this fixes shutdown behavior that's already tested