Skip to content

Fix infinite retry loop in _handle_reconnection#2397

Open
Christian-Sidak wants to merge 2 commits intomodelcontextprotocol:mainfrom
Christian-Sidak:fix/reconnection-infinite-retry-loop
Open

Fix infinite retry loop in _handle_reconnection#2397
Christian-Sidak wants to merge 2 commits intomodelcontextprotocol:mainfrom
Christian-Sidak:fix/reconnection-infinite-retry-loop

Conversation

@Christian-Sidak
Copy link
Copy Markdown

Summary

  • Fixes _handle_reconnection resetting the attempt counter to 0 when an SSE stream ends without a complete response, which made MAX_RECONNECTION_ATTEMPTS ineffective and caused infinite retries
  • Changes line 426 to pass attempt + 1 instead of 0 so total reconnection attempts are tracked across recursions

Root Cause

When a reconnection succeeds at the HTTP level (200 OK) but the SSE stream drops before delivering a complete JSONRPCResponse, the method recursed with attempt=0. Only the exception path (connection failure) incremented the counter. A server that accepts connections but drops streams caused the client to retry forever at 1-second intervals.

Test plan

Fixes #2393

When a reconnection succeeds at the HTTP level but the SSE stream ends
without delivering a complete response, _handle_reconnection recursed
with attempt=0, resetting the counter and making MAX_RECONNECTION_ATTEMPTS
ineffective. This caused the client to retry forever if the server
repeatedly accepted connections but dropped streams.

Pass attempt + 1 instead of 0 so total reconnection attempts are properly
tracked across recursions.

Fixes modelcontextprotocol#2393
When the server sends new events before closing the stream (indicated by
a changed last_event_id), the reconnection is intentional and the attempt
counter should reset to 0. Only increment the counter when no new events
were received, which indicates an actual failure to make progress.

This prevents legitimate server-initiated reconnection patterns from
being penalized by the retry limit while still protecting against
infinite loops when the server returns empty responses.

Github-Issue: modelcontextprotocol#2397
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

StreamableHTTP client: _handle_reconnection resets attempt counter to 0, causing infinite retry loop

1 participant