Skip to content

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

refactor(flows): improve callback lease handling in builder host

Steven Enamakel50bc925fcef8
diff --git a/crates/openhuman-core/src/flows/builder_host.rs b/crates/openhuman-core/src/flows/builder_host.rs
index ba9547965d..821377b2fc 100644
--- a/crates/openhuman-core/src/flows/builder_host.rs
+++ b/crates/openhuman-core/src/flows/builder_host.rs
@@ -175,18 +175,21 @@ impl SandboxRunner for FlowsHost {
config: self.config.clone(),
dispatch: crate::core::runtime::DispatchCtx::ambient(),
};
- let callback_lease = SandboxCallbackLease {
- runtime,
- path: Some(object_path),
- };
- runtime
- .spawn(async move {
- connection.request_name(SANDBOX_CALLBACK_BUS).await?;
- connection.serve_at(served_path, callback).await
+ // The registration task owns the lease it returns. If this caller is
+ // canceled while request_name/serve_at is pending, dropping its
+ // JoinHandle detaches the task; the returned lease is then dropped
+ // when registration completes and unregisters the callback.
+ let callback_lease = await_registration(runtime.spawn(async move {
+ connection.request_name(SANDBOX_CALLBACK_BUS).await?;
+ connection.serve_at(served_path.clone(), callback).await?;
+ Ok::<_, tinybus::Error>(SandboxCallbackLease {
+ runtime,
+ path: Some(served_path),
})
- .await
- .map_err(|error| error.to_string())?
- .map_err(|error| error.to_string())?;
+ }))
+ .await
+ .map_err(|error| error.to_string())?
+ .map_err(|error| error.to_string())?;
let response = async {
let proxy: Proxy = runtime
@@ -256,6 +259,12 @@ struct SandboxCallbackLease {
path: Option<tinybus::ObjectPath>,
}
+async fn await_registration<T>(
+ task: tokio::task::JoinHandle<Result<T, tinybus::Error>>,
+) -> Result<Result<T, tinybus::Error>, tokio::task::JoinError> {
+ task.await
+}
+
impl Drop for SandboxCallbackLease {
fn drop(&mut self) {
let Some(path) = self.path.take() else {
@@ -268,6 +277,10 @@ impl Drop for SandboxCallbackLease {
}
}
+#[cfg(test)]
+#[path = "builder_host_tests.rs"]
+mod tests;
+
#[async_trait]
impl BuilderGates for FlowsHost {
async fn validate(&self, graph: Value) -> FlowValidation {
diff --git a/crates/openhuman-core/src/flows/builder_host_tests.rs b/crates/openhuman-core/src/flows/builder_host_tests.rs
new file mode 100644
index 0000000000..a06e05344c
--- /dev/null
+++ b/crates/openhuman-core/src/flows/builder_host_tests.rs
@@ -0,0 +1,47 @@
+use super::*;
+
+use std::sync::atomic::{AtomicUsize, Ordering};
+use tokio::sync::{oneshot, Notify};
+
+struct DropNotice {
+ drops: Arc<AtomicUsize>,
+ completed: Arc<Notify>,
+}
+
+impl Drop for DropNotice {
+ fn drop(&mut self) {
+ self.drops.fetch_add(1, Ordering::SeqCst);
+ self.completed.notify_one();
+ }
+}
+
+#[tokio::test]
+async fn canceled_waiter_releases_callback_registered_after_cancellation() {
+ let drops = Arc::new(AtomicUsize::new(0));
+ let completed = Arc::new(Notify::new());
+ let (started_tx, started_rx) = oneshot::channel();
+ let (finish_tx, finish_rx) = oneshot::channel();
+ let registration_drops = drops.clone();
+ let registration_completed = completed.clone();
+
+ let registration = tokio::spawn(async move {
+ let _ = started_tx.send(());
+ let _ = finish_rx.await;
+ Ok::<_, tinybus::Error>(DropNotice {
+ drops: registration_drops,
+ completed: registration_completed,
+ })
+ });
+ let waiter = tokio::spawn(await_registration(registration));
+
+ started_rx.await.expect("registration started");
+ waiter.abort();
+ let _ = waiter.await;
+ assert_eq!(drops.load(Ordering::SeqCst), 0);
+
+ let _ = finish_tx.send(());
+ tokio::time::timeout(std::time::Duration::from_secs(1), completed.notified())
+ .await
+ .expect("late registration result is dropped and releases its lease");
+ assert_eq!(drops.load(Ordering::SeqCst), 1);
+}