Skip to content

The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.

fix(mcp): report unavailable when agent context is removed

Steven Enamakele3439644755f
diff --git a/crates/openhuman-core/src/mcp/host.rs b/crates/openhuman-core/src/mcp/host.rs
index 427ab975d8..2f73e294a1 100644
--- a/crates/openhuman-core/src/mcp/host.rs
+++ b/crates/openhuman-core/src/mcp/host.rs
@@ -52,7 +52,9 @@ pub fn schedule_agent_close(workspace_dir: &std::path::Path, agent_id: &str) {
/// Schedules module close followed by host-owned cleanup for this agent's
/// scoped data directory. The directory remains reserved until both the
-/// module acknowledgment and callback succeed.
+/// module acknowledgment and callback succeed. The callback runs synchronously
+/// while MCP cleanup locks are held, so it must be short and must not reenter
+/// MCP client lookup, close, or scheduling APIs.
#[cfg(feature = "mcp")]
pub fn schedule_agent_close_with_cleanup(
workspace_dir: &std::path::Path,
diff --git a/crates/openhuman-core/src/mcp/module_client.rs b/crates/openhuman-core/src/mcp/module_client.rs
index b66c85f9e3..6a8c37d804 100644
--- a/crates/openhuman-core/src/mcp/module_client.rs
+++ b/crates/openhuman-core/src/mcp/module_client.rs
@@ -269,7 +269,8 @@ impl McpConfiguredClient {
.unwrap_or_else(std::sync::PoisonError::into_inner);
// The cleanup reservation can be installed after the first check but
// before this cache lock is acquired. Recheck before creating a client.
- if Self::cleanup_pending(&data_dir) {
+ if current_agent_context_is_removed() || Self::cleanup_pending(&data_dir) {
+ ModuleClient::report_unavailable(MODULE);
return Err(ModuleCallError::Unavailable);
}
if let Some(client) = clients.get(&data_dir) {
@@ -357,28 +358,7 @@ impl McpConfiguredClient {
tracing::warn!(?error, "[mcp] workspace cleanup is still pending");
}
}
- let reservations = CLEANUP_RESERVATIONS
- .get()
- .map(|reservations| {
- reservations
- .lock()
- .unwrap_or_else(std::sync::PoisonError::into_inner)
- .iter()
- .map(|(path, reservation)| (path.clone(), Arc::clone(reservation)))
- .collect::<Vec<_>>()
- })
- .unwrap_or_default();
- for (data_dir, reservation) in reservations {
- let has_client = CLIENTS.get().is_some_and(|clients| {
- clients
- .lock()
- .unwrap_or_else(std::sync::PoisonError::into_inner)
- .contains_key(&data_dir)
- });
- if !has_client {
- Self::finish_cleanup(&data_dir, None, reservation);
- }
- }
+ Self::retry_orphaned_cleanups();
SHUTDOWN_REGISTERED.store(false, Ordering::Release);
});
}
@@ -486,6 +466,31 @@ impl McpConfiguredClient {
});
}
+ fn retry_orphaned_cleanups() {
+ let reservations = CLEANUP_RESERVATIONS
+ .get()
+ .map(|reservations| {
+ reservations
+ .lock()
+ .unwrap_or_else(std::sync::PoisonError::into_inner)
+ .iter()
+ .map(|(path, reservation)| (path.clone(), Arc::clone(reservation)))
+ .collect::<Vec<_>>()
+ })
+ .unwrap_or_default();
+ for (data_dir, reservation) in reservations {
+ let has_client = CLIENTS.get().is_some_and(|clients| {
+ clients
+ .lock()
+ .unwrap_or_else(std::sync::PoisonError::into_inner)
+ .contains_key(&data_dir)
+ });
+ if !has_client {
+ Self::finish_cleanup(&data_dir, None, reservation);
+ }
+ }
+ }
+
pub(crate) fn agent_close_pending(data_dir: &std::path::Path) -> bool {
Self::cleanup_pending(data_dir)
|| CLIENTS
@@ -580,7 +585,7 @@ impl McpConfiguredClient {
client: Option<Arc<Self>>,
reservation: Arc<CleanupReservation>,
) -> bool {
- let mut after_close = reservation
+ let after_close = reservation
.after_close
.lock()
.unwrap_or_else(std::sync::PoisonError::into_inner);
diff --git a/crates/openhuman-embed/src/runtime/lifecycle.rs b/crates/openhuman-embed/src/runtime/lifecycle.rs
index 4739c4f05d..d337af1918 100644
--- a/crates/openhuman-embed/src/runtime/lifecycle.rs
+++ b/crates/openhuman-embed/src/runtime/lifecycle.rs
@@ -118,8 +118,9 @@ impl Runtime {
/// state slots are cleared and its configured MCP client is closed before
/// its context is deregistered, so its cron jobs stay dormant. The id is
/// free for reuse once cleanup ends.
- /// Cancelling an already-started removal still tears down the old instance
- /// and performs a requested purge before releasing its id.
+ /// Cancelling an already-started removal still tears down the old instance.
+ /// A requested purge transfers to the cleanup task, which keeps the id
+ /// reserved until module close and home deletion both finish.
///
/// Cancellation is cooperative: a tool already executing when the agent
/// is removed, or a sub-agent it detached, may finish after this returns.