-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Fix: Prevent session manager shutdown on individual session crash #841
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?
Fix: Prevent session manager shutdown on individual session crash #841
Conversation
To address a test failure, I updated the cleanup logic to only remove error sessions and not those that have been explicitly terminated. I see that the terminated sessions are retained and used for the 404 vs 400 return code logic. However, I do not see any place where those are ever removed so I imagine they just accumulate until server restart. It's not within the scope of this PR to address but that's likely not desirable behavior. |
any update on this @soby? maybe they implemented this in some other issue perhaps? |
@UYousafzai it's ready to merge IMO. I've been running it in a fork and it works as intended to keep the server up and stable. Hopefully it can make it into tomorrow's release. @ihrpr ? |
…python-sdk into fix/session-manager-resilience
self.app.create_initialization_options(), | ||
stateless=True, | ||
) | ||
try: |
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.
Did we look into .run
function to see how to handle the error there?
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.
@NAVNAV221 That should also happen, but there should be no scenarios in which a per-session error is allowed to destabilize the entire server until reboot. This is a catch-all to make sure that the server as a whole survives any errors where proper error handling was missed.
Previously, an unhandled exception within a single MCP session's
MCPServer.run()
task could propagate to theStreamableHTTPSessionManager
's main task group. This would cause the entire task group to cancel, effectively shutting down the session manager and terminating all active sessions.This commit addresses the issue by:
self.app.run(...)
call within therun_server
(for stateful requests) andrun_stateless_server
(for stateless requests) inner functions inStreamableHTTPSessionManager
with atry...except Exception
block.This change ensures that if a single session encounters an unexpected error and crashes, it only affects that specific session. The
StreamableHTTPSessionManager
will continue to run, and other active sessions will remain operational. This significantly improves the robustness and availability of the server.Motivation and Context
Unhandled exceptions such as a network error would render the server unusable until restart
How Has This Been Tested?
Yes, specifically by generating client disconnects and observing the server log the unhandled error but remain running and stable
Breaking Changes
No breaking changes
Types of changes
Checklist
Additional context