fix(streamable_http): handle ClientDisconnect during POST at WARNING level#3
Closed
wiggzz wants to merge 1 commit intodbt-labs/patched-1.27.0from
Closed
fix(streamable_http): handle ClientDisconnect during POST at WARNING level#3wiggzz wants to merge 1 commit intodbt-labs/patched-1.27.0from
wiggzz wants to merge 1 commit intodbt-labs/patched-1.27.0from
Conversation
Cherry-pick modelcontextprotocol#2509 Co-authored-by: Will James <will.james@dbtlabs.com>
29bad70 to
0a408f6
Compare
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 (49 + 18) pass.
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)