fix: terminate active StreamableHTTP sessions during shutdown

Approval required
WE
6mo ago
+14-0

Summary

Fixes #2150 - StreamableHTTP sessions were not properly terminating active HTTP sessions during shutdown.

This PR addresses two root causes identified in the issue:

  1. StreamableHTTPSessionManager.run() was directly canceling the task group without first calling terminate() on active transport instances
  2. StreamableHTTPServerTransport.terminate() was only closing _request_streams, leaving _sse_stream_writers unclosed

Changes

streamable_http_manager.py

  • Added explicit terminate() calls for all active server instances before canceling the task group in the finally block of run()
  • 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_writers and calls close() 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