Reuse pending MCP startups during reconciliation (#38493)

## Why

MCP runtime reconciliation can occur while an unchanged server is still
starting, such as when capability roots become available. Reconciliation
should neither wait for that startup nor initialize the same server again.

## What changed

- Reuse an active, uncancelled pending MCP connection when its identity,
  catalog limit, and protocol mode still match.
- Publish the shared startup result through the reconciled server view while
  applying its updated tool filter.
- Preserve cancellation when a reused pending server is subsequently removed.

## Testing

Added coverage for filtered-tool publication, disabling a reused pending
server, and capability-root reconciliation during Codex Apps startup.

GitOrigin-RevId: 690cf8695e75b341d097d7d22b5da6993b24e787
This commit is contained in:
Matthew Zeng authored and copyberry committed 2026-08-14 04:49:14 +00:00
1 parent 4e5a08feb6
commit 45c9c74e29
3 files changed
+252 -2

No files matched your search

+21 -1
View File
@@ -376,6 +376,15 @@ impl McpConnectionSet {
reusable_previous.and_then(|previous| previous.servers.get(&server_name))
{
let connection = Arc::clone(&previous_view.connection);
let reusable_pending_startup = connection.identity.as_ref()
== Some(&connection_identity)
&& !connection.client.startup_complete.load(Ordering::Acquire)
&& !connection.startup_is_dormant()
&& !connection.client.cancel_token.is_cancelled()
&& previous_view.catalog_item_limit == catalog_item_limit
&& expected_protocol_mode.is_some()
&& reusable_previous
.is_some_and(|previous| previous.protocol_mode == protocol_mode);
let unchanged_auth_failure = if connection.identity.as_ref()
== Some(&connection_identity)
&& connection_identity.oauth_store_was_contended
@@ -391,7 +400,8 @@ impl McpConnectionSet {
} else {
None
};
if unchanged_auth_failure.is_some()
if reusable_pending_startup
|| unchanged_auth_failure.is_some()
|| connection
.reusable_client(&connection_identity)
.await
@@ -402,6 +412,8 @@ impl McpConnectionSet {
})
})
{
let pending_client =
reusable_pending_startup.then(|| connection.client.clone());
servers.insert(
server_name.clone(),
McpServerView {
@@ -449,6 +461,14 @@ impl McpConnectionSet {
}
(server_name, Err(error))
});
} else if let Some(client) = pending_client {
let publication_gate = publication_gate.clone();
join_set.spawn(async move {
if !publication_gate.wait().await {
return (server_name, Err(StartupOutcomeError::Cancelled));
}
(server_name, client.client().await)
});
} else {
reused_ready.push(server_name);
}