Skip to content

fix: prevent async generator cleanup errors in StreamableHTTP transport #1271

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 14, 2025

Conversation

mous222
Copy link
Contributor

@mous222 mous222 commented Aug 13, 2025

  • Add await response.aclose() before breaking SSE loops
  • Prevents RuntimeError: async generator ignored GeneratorExit

Motivation and Context

Problem: The MCP SDK's StreamableHTTPTransport has a critical async resource cleanup bug in SSE handling that causes production failures in multi-task environments (using AnyIO like chainlit or langchain).

Related Issues:

Root Cause: After investigating thoroughly, it turned out that in _handle_sse_response() and _handle_resumption_request(), when is_complete=True, the loop breaks without properly closing the HTTP response connection. This leaves async generators not closed.
Impact:

  • RuntimeError: async generator ignored GeneratorExit
  • Production failures

Underlying Issue: The problem stems from httpx/httpcore async generator cleanup patterns. When SSE streams are terminated abruptly without proper cleanup, downstream async generators in the HTTP stack remain active, causing GeneratorExit exceptions.

How Has This Been Tested?

Production Testing: Fix has been battle-tested in production FastMCP + Chainlit/langchain applications where the issue was consistently reproducible.

Scenarios Tested:

  • Multi-task async environments
  • Rapid connect/disconnect cycles

Results: Complete elimination of the error RuntimeError: async generator ignored GeneratorExit.

Breaking Changes

None - This is a pure bug fix with no API changes. Existing code continues to work unchanged with improved reliability.

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Documentation update

Checklist

  • I have read the MCP Documentation
  • My code follows the repository's style guidelines
  • New and existing tests pass locally
  • I have added appropriate error handling
  • I have added or updated documentation as needed

Additional context

Technical Details: The fix ensures immediate HTTP response cleanup before generator exit, preventing downstream cleanup errors in httpx/httpcore. This aligns with best practices for async resource management in Python.

Alternative Approaches Considered:

  • HTTPX's proposed safer wrapper to make sure async generators are closed (#3593) - doesn't fully resolve the issue
    Intuitively, trying to solve this tracking all called async generator down the road and make sure a proper closing is done (using aclosing from contextlib or adding finally like it was proposed here (Ensured explicit closing of async generators encode/httpx#3593) but it doesn't solve it. The only solution was by the proposed fix making sure to manually close the response resource (used for SSE stream).

Alternative Approaches Considered:

  • This PR in HTTPX proposed safer wrapper to make sure async generators are closed (#3593), doesn't fully resolve the issue
  • Downstream async generator tracking with automatic cleanup - tested but insufficient
  • Adding finally blocks throughout the httpx/httpcore stack (as proposed in #3593) - doesn't solve the core issue

The only effective solution is explicit response resource cleanup at the SSE stream termination point, which this fix implements.

Production Impact: This 2-line fix resolves a blocking issue affecting multiple production applications using MCP in async frameworks. The workaround gist has proven the fix's effectiveness (waiting for an official fix).

Code Quality: Minimal. No performance impact.

- Add await response.aclose() before breaking SSE loops
- Prevents RuntimeError: async generator ignored GeneratorExit
@dsp-ant dsp-ant merged commit d1ac8d6 into modelcontextprotocol:main Aug 14, 2025
18 checks passed
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.

2 participants