Skip to content

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

fix(flows): make host registration cancellation-safe

Steven Enamakela70d4af84305
diff --git a/crates/openhuman-core/src/flows/builder_host.rs b/crates/openhuman-core/src/flows/builder_host.rs
index 4ea4109439..83bcd473ca 100644
--- a/crates/openhuman-core/src/flows/builder_host.rs
+++ b/crates/openhuman-core/src/flows/builder_host.rs
@@ -319,7 +319,7 @@ impl CopilotCapsHost {
/// A served Copilot host callback. Its address is passed in the invocation
/// envelope and remains fixed for the entire tool call.
pub(crate) struct HostRegistration {
- path: String,
+ _registration: module::ServedObjectRegistration,
address: CopilotHost,
}
@@ -329,12 +329,6 @@ impl HostRegistration {
}
}
-impl Drop for HostRegistration {
- fn drop(&mut self) {
- module::unserve(std::mem::take(&mut self.path));
- }
-}
-
pub(crate) async fn register(
config: Arc<Config>,
context: DispatchCtx,
@@ -348,13 +342,14 @@ pub(crate) async fn register(
context,
runtime: tokio::runtime::Handle::try_current().ok(),
};
- module::serve(path.clone(), host.clone()).await?;
- if let Err(error) = module::serve(path.clone(), CopilotCapsHost(host)).await {
- module::unserve(path);
- return Err(error);
- }
+ let registration = module::serve_pair(
+ path.clone(),
+ host.clone(),
+ CopilotCapsHost(host),
+ )
+ .await?;
Ok(HostRegistration {
- path: path.clone(),
+ _registration: registration,
address: CopilotHost {
bus_name: module::HOST_BUS_NAME.to_string(),
object_path: path,
diff --git a/crates/openhuman-core/src/modules/tinyflows.rs b/crates/openhuman-core/src/modules/tinyflows.rs
index 63ade4aa96..c3cfac0dcf 100644
--- a/crates/openhuman-core/src/modules/tinyflows.rs
+++ b/crates/openhuman-core/src/modules/tinyflows.rs
@@ -179,6 +179,64 @@ mod imp {
served
}
+ /// Serve two interfaces at one object path and return an RAII token for
+ /// their shared registration. The token is created on the module runtime
+ /// task, so if the caller is cancelled while this operation is in flight,
+ /// the detached task's output drops the token and unregisters the object.
+ pub(crate) async fn serve_pair(
+ path: String,
+ first: impl tinybus::service::Interface + 'static,
+ second: impl tinybus::service::Interface + 'static,
+ ) -> Result<ServedObjectRegistration, String> {
+ serve_pair_after_first(path, first, second, async {}).await
+ }
+
+ async fn serve_pair_after_first<F>(
+ path: String,
+ first: impl tinybus::service::Interface + 'static,
+ second: impl tinybus::service::Interface + 'static,
+ after_first: F,
+ ) -> Result<ServedObjectRegistration, String>
+ where
+ F: std::future::Future<Output = ()> + Send + 'static,
+ {
+ let runtime = runtime::runtime()
+ .await
+ .map_err(|error| format!("the module bus is unavailable: {error}"))?;
+ let connection = runtime.connection().clone();
+ runtime
+ .spawn(async move {
+ let object_path = ObjectPath::new(&path).map_err(|error| error.to_string())?;
+ connection
+ .request_name(HOST_BUS_NAME)
+ .await
+ .map_err(|error| error.to_string())?;
+ connection
+ .serve_at(object_path.clone(), first)
+ .await
+ .map_err(|error| error.to_string())?;
+ after_first.await;
+ if let Err(error) = connection.serve_at(object_path.clone(), second).await {
+ let _ = connection.unserve(&object_path).await;
+ return Err(error.to_string());
+ }
+ Ok(ServedObjectRegistration { path })
+ })
+ .await
+ .map_err(|error| format!("serving the host object did not finish: {error}"))?
+ }
+
+ /// Owns the registration of interfaces served at one path.
+ pub(crate) struct ServedObjectRegistration {
+ path: String,
+ }
+
+ impl Drop for ServedObjectRegistration {
+ fn drop(&mut self) {
+ unserve(std::mem::take(&mut self.path));
+ }
+ }
+
/// Stop serving `path`. Fire and forget: a call already admitted finishes.
/// Without a runtime to run on (a sync drop at exit) the object is left;
/// the process is ending anyway.
@@ -211,7 +269,10 @@ mod imp {
mod fixture;
#[cfg(feature = "modules")]
-pub(crate) use imp::{copilot_invoke_proxy, copilot_proxy, proxy, run_proxy, serve, unserve};
+pub(crate) use imp::{
+ copilot_invoke_proxy, copilot_proxy, proxy, run_proxy, serve, serve_pair,
+ ServedObjectRegistration, unserve,
+};
#[cfg(not(feature = "modules"))]
mod imp {