Restart pending MCP connections when startup timeout changes (#40636)

## Why

Reusing an MCP connection that is still starting after its
`startup_timeout_sec` changes leaves the old startup budget in effect.

## What changed

- Track the effective startup timeout on each MCP connection.
- Reuse a pending startup only when its timeout still matches the current
  configuration.
- Continue reusing ready connections when only the startup timeout changes.

## Testing

Added integration coverage that refreshes the timeout while one server is
still initializing and another is ready, verifying that only the pending
startup is replaced.

GitOrigin-RevId: 5ed71a57d3bf0dd6e227aacd16acd3a35bc37ecf
This commit is contained in:
Tamir Duberstein authored and copyberry committed 2026-08-25 16:20:38 +00:00
1 parent f5853cbe90
commit 20c3f9733f
3 files changed
+116 -2

No files matched your search

@@ -37,6 +37,7 @@ use crate::mcp::ToolPluginProvenance;
use crate::pagination::MAX_CODEX_APPS_TOOL_CATALOG_ITEMS;
use crate::pagination::MAX_MCP_CATALOG_ITEMS;
use crate::rmcp_client::AsyncManagedClient;
use crate::rmcp_client::DEFAULT_STARTUP_TIMEOUT;
use crate::rmcp_client::DEFAULT_TOOL_TIMEOUT;
use crate::rmcp_client::ManagedClient;
use crate::rmcp_client::StartupOutcomeError;
@@ -81,6 +82,8 @@ static LIVE_CONNECTIONS: Gauge = Gauge::new("mcp.connections.live");
pub(crate) struct McpServerConnection {
identity: Option<McpServerConnectionIdentity>,
client: AsyncManagedClient,
// Startup-only budget; changing it must not replace a ready connection.
startup_timeout: Duration,
startup_trigger: Option<watch::Sender<bool>>,
_diagnostics_guard: GaugeGuard,
}
@@ -299,6 +302,9 @@ impl McpConnectionSet {
let metadata = McpServerMetadata::from(&server);
let configured_config = server.config().clone();
let configured_tool_filter = ToolFilter::from_config(&configured_config);
let startup_timeout = configured_config
.startup_timeout_sec
.unwrap_or(DEFAULT_STARTUP_TIMEOUT);
let configured_tool_timeout = Some(
configured_config
.tool_timeout_sec
@@ -385,6 +391,7 @@ impl McpConnectionSet {
let reusable_pending_startup = connection.identity.as_ref()
== Some(&connection_identity)
&& !connection.client.startup_complete.load(Ordering::Acquire)
&& connection.startup_timeout == startup_timeout
&& !connection.startup_is_dormant()
&& !connection.client.cancel_token.is_cancelled()
&& previous_view.catalog_item_limit == catalog_item_limit
@@ -544,6 +551,7 @@ impl McpConnectionSet {
connection: Arc::new(McpServerConnection {
identity: Some(connection_identity),
client: async_managed_client.clone(),
startup_timeout,
startup_trigger,
_diagnostics_guard: LIVE_CONNECTIONS.track(),
}),