fix: handle ClientDisconnect gracefully instead of returning HTTP 500#2161
Closed
giulio-leone wants to merge 3 commits intomodelcontextprotocol:mainfrom
Closed
fix: handle ClientDisconnect gracefully instead of returning HTTP 500#2161giulio-leone wants to merge 3 commits intomodelcontextprotocol:mainfrom
giulio-leone wants to merge 3 commits intomodelcontextprotocol:mainfrom
Conversation
ClientDisconnect is raised by Starlette when a client disconnects mid-request (network timeouts, user cancellation, load balancer timeouts). This is a normal client-side event, not a server failure. Previously, the broad 'except Exception' handler in _handle_post_request caught ClientDisconnect and: - Logged it as ERROR with full traceback - Returned HTTP 500 Internal Server Error - Triggered false 5XX alerts in production Now ClientDisconnect is caught before the generic handler and: - Logged at INFO level (expected operational event) - Returns early without sending a response (client is gone) - Properly cleans up by notifying the writer Fixes modelcontextprotocol#1648
6a76777 to
4f3c799
Compare
…educe complexity Reduces C901 complexity of _handle_post_request from 26 to within the limit by extracting JSON-RPC body parsing and session validation into dedicated helper methods.
Author
|
Closing to reduce noise — focusing on the most impactful PRs. Happy to reopen if there's interest. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #1648
ClientDisconnectis raised by Starlette when a client disconnects mid-request (network timeouts, user cancellation, load balancer timeouts). This is a normal client-side event, not a server failure.Problem
The broad
except Exceptionhandler in_handle_post_requestcatchesClientDisconnectand:Fix
Catch
ClientDisconnectbefore the genericexcept Exceptionhandler:Test Results
475 server tests pass (2 consecutive clean runs).