fix(mcp): preserve sequential replacements

Co-authored-by: Codex noreply@openai.com
This commit is contained in:
viyatb-oai
2026-06-25 10:52:52 -07:00
co-authored by Codex noreply@openai.com
parent b32ef8a9de
commit d2a04a2e8c
2 changed files with 39 additions and 2 deletions
+13 -1
View File
@@ -167,6 +167,7 @@ pub struct McpConnectionManager {
client_runtime: Option<Arc<McpClientRuntime>>,
cancel_on_drop: AtomicBool,
pending_replacement_cancellation_token: Option<CancellationToken>,
cancel_pending_replacement_on_drop: AtomicBool,
}
impl McpConnectionManager {
@@ -268,6 +269,7 @@ impl McpConnectionManager {
client_runtime: Some(client_runtime),
cancel_on_drop: AtomicBool::new(true),
pending_replacement_cancellation_token: None,
cancel_pending_replacement_on_drop: AtomicBool::new(false),
};
tokio::spawn(async move {
let outcomes = join_set.join_all().await;
@@ -351,6 +353,7 @@ impl McpConnectionManager {
client_runtime: None,
cancel_on_drop: AtomicBool::new(true),
pending_replacement_cancellation_token: None,
cancel_pending_replacement_on_drop: AtomicBool::new(false),
}
}
@@ -442,11 +445,16 @@ impl McpConnectionManager {
client_runtime: Some(Arc::clone(runtime)),
cancel_on_drop: AtomicBool::new(false),
pending_replacement_cancellation_token: replacement_cancellation_token,
cancel_pending_replacement_on_drop: AtomicBool::new(true),
})
}
/// Transfers ownership of the shared startup lifecycle after a successful swap.
pub fn take_cancellation_ownership_from(&self, superseded: &Self) {
// Once committed, this manager's replacement client belongs to the shared
// lifecycle. A later replacement may drop this snapshot without cancelling it.
self.cancel_pending_replacement_on_drop
.store(false, Ordering::Release);
self.cancel_on_drop.store(true, Ordering::Release);
superseded.cancel_on_drop.store(false, Ordering::Release);
}
@@ -978,7 +986,11 @@ impl Drop for McpConnectionManager {
fn drop(&mut self) {
if self.cancel_on_drop.load(Ordering::Acquire) {
self.startup_cancellation_token.cancel();
} else if let Some(cancel_token) = &self.pending_replacement_cancellation_token {
} else if self
.cancel_pending_replacement_on_drop
.load(Ordering::Acquire)
&& let Some(cancel_token) = &self.pending_replacement_cancellation_token
{
// A prepared replacement that was never committed owns only its new client.
cancel_token.cancel();
}
@@ -1476,7 +1476,32 @@ async fn server_replacement_preserves_and_does_not_cancel_unrelated_clients() {
assert!(!alpha_cancel.is_cancelled());
assert!(!replacement.clients["beta"].cancel_token.is_cancelled());
assert!(!cancel_token.is_cancelled());
replacement.shutdown().await;
let replacement_beta_cancel = replacement.clients["beta"].cancel_token.clone();
let next_replacement = Arc::new(
replacement
.prepare_server_replacement(
"alpha".to_string(),
local_stdio_server("second-replacement"),
None,
"second-refresh".to_string(),
)
.await
.expect("second replacement manager should build"),
);
let next_alpha_cancel = next_replacement.clients["alpha"].cancel_token.clone();
next_replacement.take_cancellation_ownership_from(&replacement);
let retire_task = tokio::spawn(
Arc::clone(&replacement).shutdown_server_after_readers_release("alpha".to_string()),
);
drop(replacement);
retire_task.await.expect("retirement task should finish");
assert!(alpha_cancel.is_cancelled());
assert!(!replacement_beta_cancel.is_cancelled());
assert!(!next_alpha_cancel.is_cancelled());
assert!(!cancel_token.is_cancelled());
next_replacement.shutdown().await;
}
#[test]