Skip to content

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

fix(web_chat): capture runtime handle for surface cleanup

Steven Enamakelfa622fcc7b20
diff --git a/crates/openhuman-core/src/web_chat/surface.rs b/crates/openhuman-core/src/web_chat/surface.rs
index 16ec783fed..51637e7f33 100644
--- a/crates/openhuman-core/src/web_chat/surface.rs
+++ b/crates/openhuman-core/src/web_chat/surface.rs
@@ -29,6 +29,7 @@ struct SurfaceState {
handle: Option<WebTurnHandle>,
closed: bool,
cleanup_done: Arc<tokio::sync::Notify>,
+ cleanup_runtime: Option<tokio::runtime::Handle>,
request_id: String,
}
@@ -68,6 +69,7 @@ impl Surface {
handle: None,
closed: false,
cleanup_done: Arc::new(tokio::sync::Notify::new()),
+ cleanup_runtime: Some(tokio::runtime::Handle::current()),
request_id: ids.request_id.clone(),
})),
ctx: ctx.clone(),
@@ -93,6 +95,7 @@ impl Surface {
handle: None,
closed: false,
cleanup_done: Arc::new(tokio::sync::Notify::new()),
+ cleanup_runtime: None,
request_id: ids.request_id.clone(),
})),
ctx: ctx.clone(),
@@ -278,13 +281,14 @@ impl Drop for SurfaceState {
self.cleanup_done.notify_one();
return;
};
- let Ok(runtime) = tokio::runtime::Handle::try_current() else {
+ let Some(runtime) = self.cleanup_runtime.as_ref() else {
log::warn!(
- "[web-channel] dropped projection turn without an async runtime request_id={}",
+ "[web-channel] dropped projection turn without a cleanup runtime request_id={}",
self.request_id
);
return;
};
+ let runtime = runtime.clone();
let cleanup_done = Arc::clone(&self.cleanup_done);
let request_id = self.request_id.clone();
runtime.spawn(async move {
diff --git a/crates/openhuman-core/src/web_chat/surface_tests.rs b/crates/openhuman-core/src/web_chat/surface_tests.rs
index 4c522304c3..f34ccda4ff 100644
--- a/crates/openhuman-core/src/web_chat/surface_tests.rs
+++ b/crates/openhuman-core/src/web_chat/surface_tests.rs
@@ -115,7 +115,7 @@ async fn module_projected_cancellation_closes_the_turn() {
assert!(state.handle.is_none());
}
-#[tokio::test]
+#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn dropping_a_surface_ends_its_module_owned_turn() {
let surface = Surface::new(
&DispatchCtx::ambient(),
@@ -140,20 +140,20 @@ async fn dropping_a_surface_ends_its_module_owned_turn() {
let left = surface.clone();
let right = surface.clone();
drop(surface);
- let barrier = Arc::new(tokio::sync::Barrier::new(3));
+ let barrier = Arc::new(std::sync::Barrier::new(3));
let left_barrier = Arc::clone(&barrier);
let right_barrier = Arc::clone(&barrier);
- let left_drop = tokio::spawn(async move {
- left_barrier.wait().await;
+ let left_drop = std::thread::spawn(move || {
+ left_barrier.wait();
drop(left);
});
- let right_drop = tokio::spawn(async move {
- right_barrier.wait().await;
+ let right_drop = std::thread::spawn(move || {
+ right_barrier.wait();
drop(right);
});
- barrier.wait().await;
- left_drop.await.unwrap();
- right_drop.await.unwrap();
+ barrier.wait();
+ left_drop.join().unwrap();
+ right_drop.join().unwrap();
tokio::time::timeout(std::time::Duration::from_secs(5), cleanup_done.notified())
.await
.expect("drop cleanup completes");