Skip to content

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

refactor(agent): extract guard release into complete_with_guard

Steven Enamakel4268df364e9b
diff --git a/crates/openhuman-embed/src/agent/mcp_cleanup.rs b/crates/openhuman-embed/src/agent/mcp_cleanup.rs
index 9d04aae03e..3ff76b5d63 100644
--- a/crates/openhuman-embed/src/agent/mcp_cleanup.rs
+++ b/crates/openhuman-embed/src/agent/mcp_cleanup.rs
@@ -63,10 +63,7 @@ pub(crate) fn defer_pending(
None => Ok(()),
};
let result = result.and_then(|()| {
- // Confirmed module cleanup no longer needs the process/workspace
- // owner. Filesystem purge must not extend its retention lifetime.
- drop(guard.take());
- finish_removal(&registry, &id, &pending)
+ complete_with_guard(&mut guard, || finish_removal(&registry, &id, &pending))
});
match result {
Ok(()) => {
@@ -91,6 +88,14 @@ pub(crate) fn defer_pending(
}
}
+fn complete_with_guard<T>(
+ guard: &mut Option<Arc<crate::runtime::CoreGuard>>,
+ complete: impl FnOnce() -> T,
+) -> T {
+ drop(guard.take());
+ complete()
+}
+
impl AgentInner {
pub(crate) fn reserve_cleanup(&self, purge: bool) -> Option<PendingCleanup> {
let cleanup = self
diff --git a/crates/openhuman-embed/src/agent/mcp_cleanup_tests.rs b/crates/openhuman-embed/src/agent/mcp_cleanup_tests.rs
index e773ba5453..06167e7d23 100644
--- a/crates/openhuman-embed/src/agent/mcp_cleanup_tests.rs
+++ b/crates/openhuman-embed/src/agent/mcp_cleanup_tests.rs
@@ -65,3 +65,41 @@ fn an_already_released_reservation_does_not_purge_a_home() {
.unwrap();
assert!(home.exists());
}
+
+#[tokio::test]
+async fn the_last_runtime_guard_keeps_admission_reserved_through_cleanup_completion() {
+ let runtime = crate::Runtime::builder()
+ .config(openhuman_core::config::Config::default())
+ .workspace(crate::Workspace::Ephemeral)
+ .build()
+ .await
+ .expect("a real isolated runtime can be built");
+ let guard = runtime.guard();
+ let weak = Arc::downgrade(&guard);
+ drop(runtime);
+ let (entered_tx, entered_rx) = std::sync::mpsc::channel();
+ let (release_tx, release_rx) = std::sync::mpsc::channel();
+ let worker = std::thread::spawn(move || {
+ let mut guard = Some(guard);
+ complete_with_guard(&mut guard, || {
+ entered_tx.send(()).unwrap();
+ release_rx.recv().unwrap();
+ });
+ });
+ entered_rx.recv().unwrap();
+ let guard_is_live = weak.upgrade().is_some();
+ let admission_is_reserved =
+ crate::runtime::RUNTIME_LIVE.load(std::sync::atomic::Ordering::Acquire);
+ release_tx.send(()).unwrap();
+ worker.join().unwrap();
+ assert!(
+ guard_is_live,
+ "completion must retain the last real workspace/runtime guard"
+ );
+ assert!(
+ admission_is_reserved,
+ "another runtime must remain excluded while cleanup completion is blocked"
+ );
+ assert!(weak.upgrade().is_none());
+ assert!(!crate::runtime::RUNTIME_LIVE.load(std::sync::atomic::Ordering::Acquire));
+}