Close nested async response iterators on early exit

KL
2mo ago
+242-15

Close nested async response generators when SSE, text, line, or default-network iteration stops early. Cleanup runs in the consuming task, including cancellation and SSE parser errors, without changing public return annotations or requiring generic iterator overrides to implement aclose().

This addresses the cleanup warnings blocking MCP Python SDK #3511.

Validation

  • UV_FROZEN=1 scripts/test -q --tb=short: 2,012 passed, 1 skipped, 100% coverage; Ruff, mypy, and unasync checks pass.
  • Python 3.10: 2,013 passed.
  • MCP integration with this source eliminates the async-generator warnings: 11,964 passed, with two remaining downstream test-helper failures caused by the newer default SSE event-size limit, not cleanup.

AI Disclaimer

This PR was developed with the assistance of either Claude or Codex. I've reviewed and verified the changes.