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): stop macOS EINVAL flake in daemon client and settle capture/menu tests on conditions (#5558)

Francesco Bonacci44f47953a422
diff --git a/libs/cua-driver/rust/crates/cua-driver-core/src/daemon.rs b/libs/cua-driver/rust/crates/cua-driver-core/src/daemon.rs
index aca2eb7f33..730433e0a0 100644
--- a/libs/cua-driver/rust/crates/cua-driver-core/src/daemon.rs
+++ b/libs/cua-driver/rust/crates/cua-driver-core/src/daemon.rs
@@ -226,7 +226,11 @@ pub fn send_request(socket_path: &str, request: &DaemonRequest) -> anyhow::Resul
let mut stream = UnixStream::connect(socket_path)
.map_err(|error| anyhow::anyhow!("connect to {socket_path}: {error}"))?;
- stream.set_write_timeout(Some(Duration::from_secs(5)))?;
+ // macOS rejects setsockopt with EINVAL once the peer has closed its end,
+ // even when the peer's reply is still buffered for us to read. Treat that
+ // as "peer already gone": the read below still drains buffered data and
+ // reports a real close, instead of failing with a bare "Invalid argument".
+ set_timeout_unless_peer_closed(stream.set_write_timeout(Some(Duration::from_secs(5))))?;
let mut writer = stream.try_clone()?;
let line = serde_json::to_string(request)? + "\n";
@@ -248,7 +252,7 @@ pub fn send_request(socket_path: &str, request: &DaemonRequest) -> anyhow::Resul
buffer.len()
);
}
- stream.set_read_timeout(Some(remaining.min(READ_POLL)))?;
+ set_timeout_unless_peer_closed(stream.set_read_timeout(Some(remaining.min(READ_POLL))))?;
match stream.read(&mut chunk) {
Ok(0) if buffer.is_empty() => {
anyhow::bail!("daemon closed connection without response");
@@ -269,6 +273,14 @@ pub fn send_request(socket_path: &str, request: &DaemonRequest) -> anyhow::Resul
Ok(serde_json::from_str(&response_line)?)
}
+#[cfg(unix)]
+fn set_timeout_unless_peer_closed(result: std::io::Result<()>) -> std::io::Result<()> {
+ match result {
+ Err(error) if error.kind() == std::io::ErrorKind::InvalidInput => Ok(()),
+ other => other,
+ }
+}
+
#[cfg(not(unix))]
pub fn send_request(socket_path: &str, request: &DaemonRequest) -> anyhow::Result<DaemonResponse> {
#[cfg(target_os = "windows")]
diff --git a/libs/cua-driver/rust/crates/cua-driver-e2e/tests/capture_contract_test.rs b/libs/cua-driver/rust/crates/cua-driver-e2e/tests/capture_contract_test.rs
index 71ac9ac94b..d00929d8b8 100644
--- a/libs/cua-driver/rust/crates/cua-driver-e2e/tests/capture_contract_test.rs
+++ b/libs/cua-driver/rust/crates/cua-driver-e2e/tests/capture_contract_test.rs
@@ -129,16 +129,17 @@ fn snapshot_settled(driver: &mut McpDriver, pid: u32, wid: u64, mode: &str) -> T
/// Snapshot with NO capture_mode / no extra args (the default path), retrying
/// until the tree settles.
fn snapshot_settled_default(driver: &mut McpDriver, pid: u32, wid: u64) -> ToolResponse {
+ // A cold window can serve its tree before its first frame is capturable,
+ // so settle on both halves of the default response, not the tree alone.
let args = serde_json::json!({ "pid": pid as i64, "window_id": wid });
- let mut resp = driver.call("get_window_state", args.clone());
- for _ in 0..16 {
- if tree_has_marker(&resp) {
- break;
+ let deadline = std::time::Instant::now() + Duration::from_secs(10);
+ loop {
+ let resp = driver.call("get_window_state", args.clone());
+ if (tree_has_marker(&resp) && has_image(&resp)) || std::time::Instant::now() >= deadline {
+ return resp;
}
- std::thread::sleep(Duration::from_millis(500));
- resp = driver.call("get_window_state", args.clone());
+ std::thread::sleep(Duration::from_millis(100));
}
- resp
}
/// Snapshot with `include_screenshot:false` (the perf opt-out), retrying until
diff --git a/libs/cua-driver/rust/crates/cua-driver-e2e/tests/harness_appkit_test.rs b/libs/cua-driver/rust/crates/cua-driver-e2e/tests/harness_appkit_test.rs
index 8181afa3bd..e2e712e3a7 100644
--- a/libs/cua-driver/rust/crates/cua-driver-e2e/tests/harness_appkit_test.rs
+++ b/libs/cua-driver/rust/crates/cua-driver-e2e/tests/harness_appkit_test.rs
@@ -1097,8 +1097,14 @@ fn harness_appkit_invoke_menu_live_path() {
invoked.text()
);
assert_eq!(invoked.action_effect(), Some("unverifiable"));
- std::thread::sleep(Duration::from_millis(300));
- let post = snapshot_elements(driver, pid, wid);
+ let deadline = std::time::Instant::now() + Duration::from_secs(5);
+ let mut post = snapshot_elements(driver, pid, wid);
+ while !post.tree_text().contains("menu_action=window_arrange_left")
+ && std::time::Instant::now() < deadline
+ {
+ std::thread::sleep(Duration::from_millis(50));
+ post = snapshot_elements(driver, pid, wid);
+ }
assert!(
post.tree_text().contains("menu_action=window_arrange_left"),
"menu action did not reach fixture: {}",
diff --git a/libs/cua-driver/rust/crates/cua-driver-sdk/src/lib.rs b/libs/cua-driver/rust/crates/cua-driver-sdk/src/lib.rs
index 776cd63ec9..5e5a831016 100644
--- a/libs/cua-driver/rust/crates/cua-driver-sdk/src/lib.rs
+++ b/libs/cua-driver/rust/crates/cua-driver-sdk/src/lib.rs
@@ -2038,8 +2038,14 @@ mod tests {
#[cfg(unix)]
fn serve_once(response: Value) -> (tempfile::TempDir, String, std::thread::JoinHandle<Value>) {
- let directory = tempfile::tempdir().unwrap();
- let socket = directory.path().join("driver.sock");
+ // Short path under /tmp: sun_path holds ~104 bytes on macOS and a CI
+ // TMPDIR can eat most of it. The listener is bound before returning,
+ // so the client needs no readiness wait.
+ let directory = tempfile::Builder::new()
+ .prefix("cd")
+ .tempdir_in("/tmp")
+ .unwrap();
+ let socket = directory.path().join("d.sock");
let listener = UnixListener::bind(&socket).unwrap();
let handle = std::thread::spawn(move || {
let (metadata_stream, _) = listener.accept().unwrap();