Skip to content

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

fix(embed): avoid holding agents lock during teardown

Steven Enamakel5984574a480c
diff --git a/crates/openhuman-embed/src/runtime/lifecycle.rs b/crates/openhuman-embed/src/runtime/lifecycle.rs
index 4d8a0ee9a5..16af5b4085 100644
--- a/crates/openhuman-embed/src/runtime/lifecycle.rs
+++ b/crates/openhuman-embed/src/runtime/lifecycle.rs
@@ -41,11 +41,6 @@ impl RemovalSlot<'_> {
impl Drop for RemovalSlot<'_> {
fn drop(&mut self) {
- let mut agents = self
- .runtime
- .agents
- .lock()
- .unwrap_or_else(|e| e.into_inner());
if self.purge && !self.mcp_cleanup_confirmed {
self.inner.teardown_without_mcp_cleanup("agent_removed");
let home = self.inner.layout.home.clone();
@@ -79,6 +74,15 @@ impl Drop for RemovalSlot<'_> {
log::warn!("[embed][runtime] cancelled removal purge failed: {error}");
}
}
+ // Teardown may drop user-owned state whose destructor blocks or
+ // re-enters Runtime. Keep the old Arc in the weak map as the ID
+ // reservation while teardown runs, but never hold the map lock across
+ // user destructors or cleanup scheduling.
+ let mut agents = self
+ .runtime
+ .agents
+ .lock()
+ .unwrap_or_else(|e| e.into_inner());
if agents
.get(&self.inner.id)
.and_then(std::sync::Weak::upgrade)