fix(streamable_http): handle ClientDisconnect during POST at WARNING level#4
Merged
wiggzz merged 2 commits intodbt-labs/patched-1.27.0from May 5, 2026
Merged
Conversation
…level Catch ClientDisconnect in _handle_post_request so client disconnections (network timeout, cancel, LB drop) log at WARNING instead of ERROR and do not attempt to send a response to the closed socket. This fixes pattern 1 (67% of all prod ERRORs in ai-codegen-api): when the client disconnects mid-POST, the current except-Exception handler synthesizes a 500 response to a closed socket and logs ERROR-level noise to Datadog. Changes: - Catch ClientDisconnect before the generic except-Exception handler - Log at WARNING (honest level: we aborted but it wasn't our fault) - Notify the session writer so inner session tasks unblock cleanly - Suppress errors from writer.send() since the writer may already be closed - Skip sending a response (the socket is gone) Upstream context: - modelcontextprotocol#1647 (POST-only scope, right approach) - modelcontextprotocol#1947 (writer-notification semantics, skip-response) - modelcontextprotocol#1648 (tracking issue, still open, no PRs merged) This patch takes the spirit of modelcontextprotocol#1647 (POST-only scope) + the semantics of modelcontextprotocol#1947 (notify writer, skip response) in a tight ~10-line change. Github-Issue:modelcontextprotocol#1648
…tr|int, not None)
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.
Problem
When a client disconnects mid-POST (network timeout, cancel, LB drop), the MCP SDK's
_handle_post_requestcatchesClientDisconnectin its genericexcept Exceptionhandler, which:Solution
Catch
ClientDisconnectbefore the genericexcept Exceptionhandler in_handle_post_request:ClientDisconnect()so inner session tasks unblock cleanly (stateful sessions)_handle_post_requestTests
4 new unit tests in
tests/server/streamable_http/test_client_disconnect_post.py:test_client_disconnect_logs_warning_not_error— verifies WARNING, not ERRORtest_client_disconnect_does_not_send_response— verifies no ASGI sends to closed sockettest_client_disconnect_notifies_writer— verifies writer receives ClientDisconnecttest_client_disconnect_writer_suppresses_errors— verifies broken writer doesn't crash usAll existing streamable_http tests pass (pre-existing flaky test
test_stateless_get_returns_405excluded).Upstream context
This patch combines the approach of two open upstream PRs (neither merged):
This is a tight ~10-line change taking the spirit of modelcontextprotocol#1647 + semantics of modelcontextprotocol#1947. Tracking issue: modelcontextprotocol/python-sdk#1648.
Follow-up
After this lands in the fork and is verified in prod via ai-codegen-api bump:
ClientDisconnectHandlerMiddlewarein ai-codegen-api (no longer load-bearing for POST path)