Skip to content

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

fix(embed): release the runtime slot when a build is cancelled mid-boot

Steven Enamakel3e5010142811
diff --git a/crates/openhuman-embed/src/runtime/build.rs b/crates/openhuman-embed/src/runtime/build.rs
index 20b5c1b487..6526fe3f3a 100644
--- a/crates/openhuman-embed/src/runtime/build.rs
+++ b/crates/openhuman-embed/src/runtime/build.rs
@@ -31,6 +31,22 @@ impl Drop for SessionStoreCleanup {
}
}
+/// Releases [`RUNTIME_LIVE`] unless disarmed: held across
+/// [`RuntimeBuilder::build`]'s boot so a failed or cancelled build never
+/// leaves the process slot claimed.
+struct SlotClaim {
+ armed: bool,
+}
+
+impl Drop for SlotClaim {
+ fn drop(&mut self) {
+ if self.armed {
+ RUNTIME_LIVE.store(false, std::sync::atomic::Ordering::Release);
+ log::debug!("[embed][runtime] build did not complete; process slot released");
+ }
+ }
+}
+
impl RuntimeBuilder {
/// Build the core and return a runtime ready to host agents.
///
@@ -47,14 +63,17 @@ impl RuntimeBuilder {
return Err(RuntimeError::AlreadyRunning);
}
// From here on every early return must release the slot, or a failed
- // build would permanently poison the process against retrying.
- match self.build_inner().await {
- Ok(runtime) => Ok(runtime),
- Err(e) => {
- RUNTIME_LIVE.store(false, std::sync::atomic::Ordering::Release);
- Err(e)
- }
+ // build would permanently poison the process against retrying. The
+ // claim is a drop guard rather than a match on the result so that a
+ // *cancelled* build (the future dropped mid-boot, as the desktop
+ // shell's startup timeout does to its server task) releases it too.
+ let mut claim = SlotClaim { armed: true };
+ let result = self.build_inner().await;
+ if result.is_ok() {
+ // The runtime's `CoreGuard` owns the slot from here.
+ claim.armed = false;
}
+ result
}
/// Refuse combinations the chosen [`ConfigSource`] cannot honour.
diff --git a/crates/openhuman-embed/tests/cancelled_build.rs b/crates/openhuman-embed/tests/cancelled_build.rs
new file mode 100644
index 0000000000..b22a5cf5d3
--- /dev/null
+++ b/crates/openhuman-embed/tests/cancelled_build.rs
@@ -0,0 +1,38 @@
+//! A cancelled `RuntimeBuilder::build` must release the process's runtime
+//! slot. The desktop shell's startup timeout drops its server task mid-boot;
+//! a leaked slot would make every later build fail with `AlreadyRunning`.
+//! Its own test binary: the slot is process-wide.
+
+use openhuman_embed::{RuntimeBuilder, RuntimeError};
+
+#[test]
+fn a_cancelled_build_releases_the_slot() {
+ std::thread::Builder::new()
+ .stack_size(16 * 1024 * 1024)
+ .spawn(|| {
+ tokio::runtime::Builder::new_multi_thread()
+ .enable_all()
+ .build()
+ .expect("tokio runtime")
+ .block_on(async {
+ // A zero deadline polls the build once, then drops it.
+ let first = tokio::time::timeout(
+ std::time::Duration::ZERO,
+ RuntimeBuilder::new().build(),
+ )
+ .await;
+ drop(first);
+
+ match RuntimeBuilder::new().build().await {
+ Ok(runtime) => drop(runtime),
+ Err(RuntimeError::AlreadyRunning) => {
+ panic!("a cancelled build left the runtime slot claimed")
+ }
+ Err(other) => panic!("second build failed: {other}"),
+ }
+ });
+ })
+ .expect("test thread")
+ .join()
+ .expect("test thread should not panic");
+}