-
Notifications
You must be signed in to change notification settings - Fork 3.7k
fix(server): terminate streamable HTTP sessions on manager shutdown #3218
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -153,6 +153,18 @@ async def lifespan(app: Starlette) -> AsyncIterator[None]: | |
| yield # Let the application run | ||
| finally: | ||
| logger.info("StreamableHTTP session manager shutting down") | ||
| # Terminate active transports before cancelling the task group so | ||
| # in-flight SSE responses can complete cleanly (issue #2150). | ||
| active_transports = list(self._server_instances.values()) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. P1: A session created while this loop awaits an earlier Prompt for AI agents |
||
| for transport in active_transports: | ||
| if not transport.is_terminated: # pragma: no branch | ||
| try: | ||
| await transport.terminate() | ||
| except Exception: # pragma: no cover | ||
| logger.exception( | ||
| "Error terminating streamable HTTP session %s during shutdown", | ||
| transport.mcp_session_id, | ||
| ) | ||
| # Cancel task group to stop all spawned tasks | ||
| tg.cancel_scope.cancel() | ||
| self._task_group = None | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
P1: Concurrent standalone GETs can still both pass the one-stream check because it only observes
_request_streams, which is populated later by the writer task. The second request overwrites this newly registered writer, and either response's cleanup can then remove the other registration; that leaves a live SSE response thatterminate()cannot close. Consider making the duplicate-stream guard use the writer registration (or reserving the GET key atomically) and only removing a mapping when it still refers to that handler's writer.Prompt for AI agents