fix: increment reconnection attempt counter on clean stream disconnect#2421
Open
manjunathgujjar wants to merge 2 commits intomodelcontextprotocol:mainfrom
Open
Conversation
_handle_reconnection() was resetting the attempt counter to 0 whenever a reconnection succeeded at the HTTP level but the stream closed without delivering a complete response. This made MAX_RECONNECTION_ATTEMPTS ineffective: a server that repeatedly accepts then drops SSE connections would loop forever, hanging the caller indefinitely. The fix passes attempt+1 on clean-close recursion, matching the behaviour of the exception path. MAX_RECONNECTION_ATTEMPTS now bounds total attempts regardless of whether individual connection attempts succeed at the HTTP level. MAX_RECONNECTION_ATTEMPTS is also raised from 2 to 5 to give legitimate transient disconnects more headroom. Also removes the pragma: no cover on the MAX_RECONNECTION_ATTEMPTS guard now that the new test exercises it. Fixes modelcontextprotocol#2393
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
_handle_reconnection()instreamable_http.pyresets theattemptcounter to0when a reconnection succeeds at the HTTP level but the stream closes without delivering a complete response (line 494 before this fix). This makesMAX_RECONNECTION_ATTEMPTSineffective: only the exception path (network error) incremented the counter. A server that repeatedly accepts connections but drops them before sending a final response causes the client to retry forever.This was reported in #2393 where production jobs hung for hours in a reconnection loop.
Root Cause
The asymmetry means
MAX_RECONNECTION_ATTEMPTSonly guards against consecutive HTTP-level failures, not against total reconnection attempts.Fix
Pass
attempt + 1on clean-close recursion, matching the exception path.MAX_RECONNECTION_ATTEMPTSis also raised from 2 to 5 to give more headroom for legitimate transient disconnects while still bounding the retry loop.The
# pragma: no coveron the MAX guard is also removed since the new test now exercises that branch.Test
Added
test_reconnection_attempt_counter_increments_on_clean_disconnecttotests/shared/test_streamable_http.py. The test spies on_handle_reconnectionto record theattemptvalues seen, patchesMAX_RECONNECTION_ATTEMPTS=2, then calls a tool that closes the SSE stream multiple times without completing. With the fix, the spy records[0, 1, 2]— the counter increments until the limit is hit. Without the fix it would record[0, 0, 0, ...]indefinitely.