Shut down superseded MCP managers on refresh (#29608)

## Summary

MCP refresh replaced the published connection manager without shutting
down the manager it superseded. If another task retained that old
manager, its stdio MCP processes stayed alive and accumulated across
refreshes.

Atomically swap in the refreshed manager, then explicitly shut down the
exact manager returned by the swap. Add a process-level regression test
that retains the old manager during refresh and verifies its stdio
process exits while the replacement remains available.

## Context

Explicit cleanup was lost when manager publication moved to `ArcSwap`.
Dropping the old manager is not a reliable shutdown boundary because
active callers can retain its `Arc` and underlying client process
handles.
This commit is contained in:
jif authored and GitHub committed 2026-06-23 18:29:27 +01:00
1 parent 9fe689783d
commit 8751fd3fcb
5 files changed
+195 -4

No files matched your search

+9 -2
View File
@@ -345,8 +345,15 @@ impl McpConnectionManager {
/// Stop all MCP clients owned by this manager and terminate stdio server processes.
pub async fn shutdown(&self) {
self.startup_cancellation_token.cancel();
for client in self.clients.values() {
client.shutdown().await;
let clients = self.clients.values().cloned().collect::<Vec<_>>();
// Keep cleanup alive if an interrupt cancels the refresh that requested it.
let shutdown_task = tokio::spawn(async move {
for client in clients {
client.shutdown().await;
}
});
if let Err(error) = shutdown_task.await {
warn!("MCP client shutdown task failed: {error}");
}
}