Skip to content

armgit / cua

Public Imported from GitHub
0

Scale computer-use 2.0 with open-source drivers, cross-OS sandboxes, and benchmarks for training, evaluation, and data generation.

fix(cua-driver): reap finished daemon connection tasks (#4980)

injaneitye3b40126cdeb
diff --git a/libs/cua-driver/rust/crates/cua-driver/src/serve.rs b/libs/cua-driver/rust/crates/cua-driver/src/serve.rs
index b89618839c..d97ca38eac 100644
--- a/libs/cua-driver/rust/crates/cua-driver/src/serve.rs
+++ b/libs/cua-driver/rust/crates/cua-driver/src/serve.rs
@@ -79,6 +79,16 @@ async fn resume_trusted_connection(
bind_trusted_connection(sdk, record.options, record.transport_session)
}
+fn reap_finished_connections(connection_tasks: &mut tokio::task::JoinSet<()>) {
+ while let Some(result) = connection_tasks.try_join_next() {
+ if let Err(error) = result {
+ if error.is_panic() {
+ tracing::warn!("service connection task panicked: {error}");
+ }
+ }
+ }
+}
+
async fn take_trusted_resume_record(
registry: &TrustedResumeRegistry,
credential: &str,
@@ -1000,6 +1010,7 @@ pub async fn run_serve(
tokio::select! {
result = listener.accept() => {
let (stream, _) = result?;
+ reap_finished_connections(&mut connection_tasks);
if let Err(error) = authenticate_unix_peer(&stream) {
tracing::warn!("service connection rejected before request parsing: {error}");
continue;
@@ -2995,6 +3006,42 @@ mod trusted_resume_tests {
}
}
+#[cfg(test)]
+mod connection_reaping_tests {
+ use super::reap_finished_connections;
+
+ #[tokio::test]
+ async fn finished_connection_tasks_are_released() {
+ let mut tasks = tokio::task::JoinSet::new();
+ for _ in 0..8 {
+ tasks.spawn(async {});
+ }
+ let live = tokio::sync::Notify::new();
+ let live = std::sync::Arc::new(live);
+ let waiter = live.clone();
+ tasks.spawn(async move { waiter.notified().await });
+ for _ in 0..16 {
+ tokio::task::yield_now().await;
+ }
+
+ reap_finished_connections(&mut tasks);
+
+ assert_eq!(tasks.len(), 1, "only the still-running connection remains");
+ live.notify_one();
+ }
+
+ #[tokio::test]
+ async fn a_panicked_connection_task_is_reaped_without_panicking_the_daemon() {
+ let mut tasks = tokio::task::JoinSet::new();
+ tasks.spawn(async { panic!("connection task panic") });
+ for _ in 0..16 {
+ tokio::task::yield_now().await;
+ }
+ reap_finished_connections(&mut tasks);
+ assert!(tasks.is_empty());
+ }
+}
+
#[cfg(test)]
mod service_authorization_status_tests {
use super::service_authorization_status;