Skip to content

fix: refresh closed MCP connections before turns - #562

Closed
GraciousGazelles wants to merge 3 commits into
mainfrom
fix/mcp-liveness-20260731
Closed

GraciousGazelles wants to merge 3 commits into
mainfrom
fix/mcp-liveness-20260731

Conversation

@GraciousGazelles

Copy link
Copy Markdown
Collaborator

Current-base replacement for #554.

Refresh initialized MCP connections that have lost their transport before an ordinary turn publishes its provider catalogue. Probes are once per turn and use bounded exponential backoff after repeated closed results. Includes focused liveness and refresh-cleanup coverage.

Supersedes the stale-base PR only after this replacement is validated and merged.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Code Review

This pull request introduces MCP connection liveness probing and backoff logic to detect and replace closed MCP server connections during a turn. It adds is_closed and has_closed_connections checks across the MCP connection manager, runtime, and session, along with backoff state tracking in McpRefresh to avoid excessive reconnect attempts. The reviewer identified a logical issue in should_probe_liveness_at where the turn ID is updated even when the probe is skipped due to backoff, which prematurely consumes the probe attempt for that turn. A refactoring was suggested to only update the turn ID when a probe is actually allowed to run.

Comment on lines +61 to +73
fn should_probe_liveness_at(&self, turn_id: &str, now: Instant) -> bool {
let mut state = self
.liveness_probe
.lock()
.unwrap_or_else(std::sync::PoisonError::into_inner);
if state.last_turn_id.as_deref() == Some(turn_id) {
return false;
}
state.last_turn_id = Some(turn_id.to_string());
!state
.retry_not_before
.is_some_and(|retry_not_before| now < retry_not_before)
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

medium

Currently, should_probe_liveness_at updates state.last_turn_id even when it returns false due to backoff (now < retry_not_before). This "consumes" the turn's single probe attempt without actually performing any liveness check.

If a turn has multiple steps and the backoff expires mid-turn, subsequent steps will be blocked from probing because last_turn_id was already set.

By only updating last_turn_id when we actually decide to perform a probe (returning true), we allow a later step in the same turn to successfully probe once the backoff window has expired.

    fn should_probe_liveness_at(&self, turn_id: &str, now: Instant) -> bool {
        let mut state = self
            .liveness_probe
            .lock()
            .unwrap_or_else(std::sync::PoisonError::into_inner);
        if state.last_turn_id.as_deref() == Some(turn_id) {
            return false;
        }
        if state
            .retry_not_before
            .is_some_and(|retry_not_before| now < retry_not_before)
        {
            return false;
        }
        state.last_turn_id = Some(turn_id.to_string());
        true
    }

@GraciousGazelles

Copy link
Copy Markdown
Collaborator Author

Addressed the unresolved liveness review in signed commit 9e60b0eccaf835a7352883460b860bab5a74e21b: the retry-window check now returns before recording last_turn_id, and the regression test verifies a later step in the same turn can probe after backoff expires while still allowing only one successful probe.

@GraciousGazelles

Copy link
Copy Markdown
Collaborator Author

Temporarily reopening this same candidate to request fresh checks for the corrected head; no code is being discarded.

@GraciousGazelles

Copy link
Copy Markdown
Collaborator Author

Superseded by fresh current-head replacement #563. The reviewed liveness fix remains unchanged; #563 exists solely to obtain new pull-request hosted validation receipts after GitHub emitted no checks for the repaired head here.

@GraciousGazelles

Copy link
Copy Markdown
Collaborator Author

Closed as superseded by #563.

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.

1 participant