Skip to content

fix(transport): honor retry policy and server retry interval on SSE stream errors - #1572

Open
seedfunded wants to merge 2 commits into
modelcontextprotocol:mainfrom
seedfunded:fix/sse-stream-error-retry-policy
Open

seedfunded wants to merge 2 commits into
modelcontextprotocol:mainfrom
seedfunded:fix/sse-stream-error-retry-policy

Conversation

@seedfunded

Copy link
Copy Markdown

Motivation

In SseAutoReconnectStream::poll_next, the Some(Err(e)) branch bypassed SseRetryPolicy::retry(0) and server_retry_interval, jumping straight into Retrying via connector.retry_connection(...).

This caused three inconsistencies with the graceful-close (None) path and SEP-1699 / WHATWG SSE reconnection rules:

  1. NeverRetry and max_times: Some(0) still reconnected on stream errors: A non-oversized stream error (e.g., ConnectionReset) triggered an unwanted reconnect attempt instead of terminating and surfacing the fatal stream error. Policies with max_times: Some(N) also performed N + 1 reconnect attempts on stream errors.
  2. Server-sent retry: intervals were ignored on stream errors: Even when the server sent a retry field, an abrupt stream error reconnected immediately with zero delay instead of waiting server_retry_interval (or the initial policy delay).
  3. Graceful close with no reconnect policy did not transition to Terminated: Returning Poll::Ready(None) while staying in Connected risked re-polling the exhausted inner stream if polled again.

Changes

  • crates/rmcp/src/transport/common/client_side_sse.rs:
    • Consult self.retry_policy.retry(0) and self.server_retry_interval on Some(Err(e)), matching the None branch: transition to WaitingNextRetry when retries are permitted, or transition to Terminated and return map_fatal_stream_error(e) when retries are disabled.
    • Transition to Terminated before returning Poll::Ready(None) in the None branch when retry_policy.retry(0) is None.
    • Added 4 regression tests:
      • never_retry_does_not_reconnect_after_stream_error
      • max_times_zero_does_not_reconnect_after_stream_error
      • server_retry_interval_applies_after_stream_error
      • graceful_close_with_never_retry_transitions_to_terminated

Verification

  • cargo test -p rmcp --lib --features "client,transport-streamable-http-client" transport::common::client_side_sse
  • cargo test -p rmcp --test test_streamable_http_reconnect_classification --features "client,transport-streamable-http-client"
  • cargo test -p rmcp --test test_streamable_http_get_404_session_expired --features "client,transport-streamable-http-client-reqwest,reqwest"
  • cargo clippy -p rmcp --lib --features "client,server,transport-streamable-http-client-reqwest,transport-streamable-http-server,reqwest,transport-io" -- -D warnings
  • cargo fmt --all -- --check

@seedfunded
seedfunded requested a review from a team as a code owner December 4, 2026 17:02
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Dec 4, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-12-05T17:53:29.670629Z 5e08f08 New commits
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@jokemanfire jokemanfire left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verified at 5586fbb0 (base 37257723). Ran locally: the fork's CI has not started (action_required), so these numbers are the only ones available.

What I ran

  • cargo test -p rmcp --lib --features "client,transport-streamable-http-client" transport::common::client_side_sse → 23 passed.
  • cargo test -p rmcp --test test_streamable_http_reconnect_classification --features "client,transport-streamable-http-client,reqwest" → 14 passed.
  • cargo fmt --all -- --check → clean on stable (CI formats with nightly, which I do not have here).
  • Red before green: I put the four new tests on the base file (kept this PR's tests, restored the two source hunks) and ran the same lib filter → 3 failed / 20 passed:
    • never_retry_does_not_reconnect_after_stream_error → Matches fails: base yields Some(Ok(..)) from the reconnected stream, so the reconnect really happened
    • max_times_zero_does_not_reconnect_after_stream_error → same
    • server_retry_interval_applies_after_stream_error → left: [0ns], right: [5s]

So claims 1 and 2 in the body are real and the fix works.

One test does not pin its change. graceful_close_with_never_retry_transitions_to_terminated passes on base as well (20 passed there includes it). The only observable difference of the added this.state.set(SseAutoReconnectStreamState::Terminated) in the None branch is that a non-fused inner stream which yields Some after None will not be read again; a second poll_next on an exhausted Connected stream already returns None without reconnecting, so the assertions in that test hold either way. Either drop it from the list of regression tests or make it use such a source; as written, a reader will think it covers the third change and it does not.

Two things I would rather see as follow-ups than in this PR

  1. The first wait after an error uses server_retry_interval.unwrap_or(interval), while a failed reconnect uses server_retry_interval.max(interval) (:538-543). With a server sending retry: 100 and ExponentialBackoff, the first reconnect is at 100ms and later ones at the backoff delay. That asymmetry is pre-existing (the graceful path already used unwrap_or), but now both error and graceful paths share it.
  2. retry_times is reset to 0 every time a connection succeeds, so a server that accepts and immediately errors loops forever regardless of max_times; max_times only counts failed connection attempts. Also pre-existing, and also newly visible now that stream errors go through the policy.

The only thing I would change is the fourth test; the code change itself looks right to me, and with the error branch now surfacing map_fatal_stream_error for NeverRetry/max_times: 0, the body's "Breaking: None" is accurate as far as I can tell (a client that installed NeverRetry was reconnecting against its own configuration before).

@seedfunded

Copy link
Copy Markdown
Author

Thanks for the thorough review and local verification, @jokemanfire—good catch on graceful_close_with_never_retry_transitions_to_terminated.

Updated in 5e08f08 to use a non-fused poll_fn inner stream that returns None on the first poll and yields a valid notification event if polled again, plus an assertion that the inner stream is only polled once—so a second poll_next would read past None and fail without the Terminated transition.

Agreed on keeping the unwrap_or vs. max asymmetry and the retry_times reset behavior out of this PR; happy to open follow-ups for those once this lands.

@connor4312 connor4312 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Stream errors now go through the retry policy and the server's retry: interval, the same as a graceful close, using the field #1519 keeps across reconnects. NeverRetry/max_times: Some(0) now end the stream with the original error. 404/auth/scope errors are still terminal, and the wait is cancellable. The unit tests use paused time, and the streamable HTTP integration suites pass with an explicit feature set.

Minor: the description's "N + 1 attempts" only applies to N = 0; for N ≥ 1 the total number of attempts doesn't change. The two older behaviours jokemanfire raised (how the server interval combines with the policy, and the counter resetting after each successful connection) are worth separate issues.

This branch has not been deployed

No deployments
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.

3 participants