fix: prevent infinite retry loop in StreamableHTTP client reconnection#2398
Open
chernistry wants to merge 2 commits intomodelcontextprotocol:mainfrom
Open
fix: prevent infinite retry loop in StreamableHTTP client reconnection#2398chernistry wants to merge 2 commits intomodelcontextprotocol:mainfrom
chernistry wants to merge 2 commits intomodelcontextprotocol:mainfrom
Conversation
The `_handle_reconnection` method reset its attempt counter to 0 when a stream ended normally (without exception), causing an infinite retry loop when the server repeatedly dropped connections after partially delivering events. This is because the recursive call on the "stream ended without response" path passed `attempt=0` instead of incrementing. Change the normal-close reconnection path to pass `attempt + 1`, matching the exception path, so total reconnection attempts are tracked across the entire chain regardless of how each individual stream ended. Fixes modelcontextprotocol#2393 Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
be86955 to
9630947
Compare
The previous fix (attempt + 1 unconditionally) broke legitimate multi-reconnection scenarios where the server checkpoints progress between disconnections. Now we check whether new SSE events were delivered (last_event_id changed) — if so, reset the counter; if not, increment it toward MAX_RECONNECTION_ATTEMPTS. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
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
Fix
_handle_reconnection()infinite retry loop when a server repeatedly drops connections after partially delivering SSE events (#2393).Approach: Track whether each reconnection made forward progress (received new SSE events with a different
last_event_id). If progress was made, reset the attempt counter — the server is legitimately checkpointing work across disconnections. If no progress was made (same or no new event IDs), increment the counter towardMAX_RECONNECTION_ATTEMPTSto prevent infinite loops.This preserves the existing multi-reconnection behavior tested by
test_streamable_http_multiple_reconnectionswhile preventing the infinite loop in #2393.Fixes #2393
Details
The bug is on this line: when the SSE stream ends normally (iterator exhausted, no exception),
_handle_reconnectioncalls itself recursively withattempt=0. This means every connection that drops before delivering a response/error resets the counter, andMAX_RECONNECTION_ATTEMPTSis never reached.The fix compares
reconnect_last_event_idagainst the incominglast_event_id:Test plan
test_handle_reconnection_does_not_retry_infinitelyuses a static event ID (no progress) and verifies termination withinMAX_RECONNECTION_ATTEMPTStest_streamable_http_multiple_reconnectionscontinues to pass — it delivers new event IDs on each reconnection (progress), so the counter resets as expected🤖 Generated with Claude Code