Skip to content

fix(http): terminate active streamable-HTTP transports before lifespan shutdown - #4118

Merged
jlowin merged 1 commit into
PrefectHQ:mainfrom
SarthakB11:fix/issue-3025
May 10, 2026
Merged

jlowin merged 1 commit into
PrefectHQ:mainfrom
SarthakB11:fix/issue-3025

Conversation

@SarthakB11

Copy link
Copy Markdown
Contributor

Description

StreamableHTTPSessionManager.run() (upstream mcp) cancels its task group inside finally without first terminating active transports. During Uvicorn graceful shutdown the lifespan in create_streamable_http_app exits before the session manager, so tg.cancel_scope.cancel() aborts long-lived GET /mcp/ SSE response tasks mid-flight. The streaming response never sends its final http.response.body and Uvicorn logs ERROR: ASGI callable returned without completing response.

The fix wraps the lifespan's yield in try/finally and, on exit, awaits terminate() on every transport currently registered in session_manager._server_instances. StreamableHTTPServerTransport.terminate() is idempotent (_terminated guard) and closes the per-request and read/write streams, so the SSE GET handler completes its ASGI response before the SDK's __aexit__ cancels the task group. Termination errors are caught and logged at debug; shutdown must not raise.

Stateless servers don't populate _server_instances, so this is a no-op there. The issue's "Option 1" await anyio.sleep(0.5) isn't needed because terminate() already awaits stream close; the "Option 2" @on_shutdown hook is a larger API design and out of scope for a bug fix.

A regression test in tests/server/http/test_streamable_http_shutdown.py registers a fake transport on the live session manager during the lifespan and asserts terminate() is awaited before the lifespan completes. It fails on main and passes with the fix.

Closes #3025

Contribution type

  • Bug fix (simple, well-scoped fix for a clearly broken behavior)
  • Documentation improvement
  • Enhancement (maintainers typically implement enhancements — see CONTRIBUTING.md)

Checklist

  • This PR addresses an existing issue (or fixes a self-evident bug)
  • I have read CONTRIBUTING.md
  • I have added tests that cover my changes
  • I have run uv run prek run --all-files and all checks pass
  • I have self-reviewed my changes
  • If I used an LLM, it followed the repo's contributing conventions (not generic output)

@claude claude Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Claude Code Review

This pull request is from a fork — automated review is disabled. A repository maintainer can comment @claude review to run a one-time review.

@marvin-context-protocol marvin-context-protocol Bot added bug Something isn't working. Reports of errors, unexpected behavior, or broken functionality. server Related to FastMCP server implementation or server-side functionality. http Related to HTTP transport, networking, or web server functionality. labels May 9, 2026

@jlowin jlowin left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working. Reports of errors, unexpected behavior, or broken functionality. http Related to HTTP transport, networking, or web server functionality. server Related to FastMCP server implementation or server-side functionality.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ASGI shutdown error with active SSE connections - StreamableHTTPSessionManager cancels task group without graceful stream termination

2 participants