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): end an old cua daemon that never exits after an app update (#5191)

ddupont7bdb2a2fed02
diff --git a/apps/cua-spaces/src-tauri/Cargo.lock b/apps/cua-spaces/src-tauri/Cargo.lock
index 3c3bb7e0cc..755e53440c 100644
--- a/apps/cua-spaces/src-tauri/Cargo.lock
+++ b/apps/cua-spaces/src-tauri/Cargo.lock
@@ -1360,6 +1360,7 @@ dependencies = [
"humantime",
"hyper",
"hyper-util",
+ "libc",
"pbjson-types",
"prost",
"prost-types",
diff --git a/examples/streaming/rust/Cargo.lock b/examples/streaming/rust/Cargo.lock
index 172ed5c347..6b70af6922 100644
--- a/examples/streaming/rust/Cargo.lock
+++ b/examples/streaming/rust/Cargo.lock
@@ -1331,6 +1331,7 @@ dependencies = [
"http-body-util",
"humantime",
"hyper-util",
+ "libc",
"pbjson-types",
"prost",
"prost-types",
diff --git a/libs/cua-spacesd/tests/spaces-e2e/Cargo.lock b/libs/cua-spacesd/tests/spaces-e2e/Cargo.lock
index 4aab54c296..9b4f93b255 100644
--- a/libs/cua-spacesd/tests/spaces-e2e/Cargo.lock
+++ b/libs/cua-spacesd/tests/spaces-e2e/Cargo.lock
@@ -2099,6 +2099,7 @@ dependencies = [
"humantime",
"hyper",
"hyper-util",
+ "libc",
"pbjson-types",
"prost",
"prost-types",
diff --git a/libs/cua/Cargo.lock b/libs/cua/Cargo.lock
index 9dfde22e34..a3cdf8a2f8 100644
--- a/libs/cua/Cargo.lock
+++ b/libs/cua/Cargo.lock
@@ -2472,6 +2472,7 @@ dependencies = [
"humantime",
"hyper",
"hyper-util",
+ "libc",
"pbjson-types",
"prost",
"prost-types",
diff --git a/libs/cua/crates/cua-cli/src/extension.rs b/libs/cua/crates/cua-cli/src/extension.rs
index 2be48ddedd..b7f97a3f9f 100644
--- a/libs/cua/crates/cua-cli/src/extension.rs
+++ b/libs/cua/crates/cua-cli/src/extension.rs
@@ -258,14 +258,28 @@ pub(crate) async fn missing_daemon_extensions() -> Vec<String> {
/// the app updated to 0.7.0 was left without its daemon).
pub(crate) const STOP_WAIT: std::time::Duration = std::time::Duration::from_secs(30);
+/// How long [`stop_daemon`] waits after each signal to a daemon that did
+/// not exit when asked.
+#[cfg(unix)]
+const TERMINATE_WAIT: std::time::Duration = std::time::Duration::from_secs(3);
+
/// Asks the running daemon to stop and waits (bounded by [`STOP_WAIT`])
-/// for it to go.
+/// for it to go; a cua daemon still running then is ended (SIGTERM, then
+/// SIGKILL).
pub(crate) async fn stop_daemon(discovery: &Path, pid: u32) -> Result<(), CuaError> {
+ stop_daemon_within(discovery, pid, STOP_WAIT).await
+}
+
+async fn stop_daemon_within(
+ discovery: &Path,
+ pid: u32,
+ wait: std::time::Duration,
+) -> Result<(), CuaError> {
if let Ok(client) = cua_daemon::client::existing_daemon().await {
let _ = client.shutdown().await;
}
let tick = std::time::Duration::from_millis(100);
- for _ in 0..(STOP_WAIT.as_millis() / tick.as_millis()) {
+ for _ in 0..(wait.as_millis() / tick.as_millis()) {
let gone = match cua_daemon::Discovery::read(discovery) {
Some(d) => d.pid != pid,
None => true,
@@ -276,9 +290,22 @@ pub(crate) async fn stop_daemon(discovery: &Path, pid: u32) -> Result<(), CuaErr
}
tokio::time::sleep(tick).await;
}
+ // Asked and waited for, it still runs: after an app update such a
+ // daemon (its listeners closed, its process idle) kept holding the
+ // Keyvault socket (#4950). End it, so this build's daemon replaces it.
+ #[cfg(unix)]
+ if tokio::task::spawn_blocking(move || cua_daemon::terminate_daemon(pid, TERMINATE_WAIT))
+ .await
+ .unwrap_or(false)
+ {
+ eprintln!("cua: the running cua daemon (pid {pid}) did not exit when asked; ended it");
+ if cua_daemon::client::live_address().is_none() {
+ return Ok(());
+ }
+ }
Err(CuaError::Timeout(format!(
"the running cua daemon (pid {pid}) did not stop within {} s",
- STOP_WAIT.as_secs()
+ wait.as_secs()
)))
}
@@ -314,4 +341,31 @@ mod tests {
process.0.wait().unwrap();
stop_daemon(&discovery, pid).await.unwrap();
}
+
+ /// The daemon of #4950: asked to stop, it closed its listeners but its
+ /// process never exited. It ignores SIGTERM here, so SIGKILL ends it.
+ #[tokio::test]
+ async fn a_daemon_that_never_exits_is_ended() {
+ use std::os::unix::fs::PermissionsExt;
+ let _env = crate::util::test_env::isolated();
+ let home = tempfile::tempdir().unwrap();
+ let exe = home.path().join("cua");
+ std::fs::write(&exe, "#!/bin/sh\ntrap '' TERM\nwhile :; do sleep 1; done\n").unwrap();
+ std::fs::set_permissions(&exe, std::fs::Permissions::from_mode(0o755)).unwrap();
+ let mut child = Command::new(&exe)
+ .args(["daemon", "start", "--foreground"])
+ .spawn()
+ .unwrap();
+ let pid = child.id();
+ // Reaped as soon as it dies, so a killed one is not left a zombie.
+ let reaper = std::thread::spawn(move || child.wait().unwrap());
+ tokio::time::sleep(Duration::from_millis(300)).await;
+
+ let discovery = home.path().join("daemon.json");
+ stop_daemon_within(&discovery, pid, Duration::from_millis(300))
+ .await
+ .expect("the stuck daemon is ended");
+ assert!(!reaper.join().unwrap().success());
+ assert!(!cua_daemon::pid_alive(pid));
+ }
}
diff --git a/libs/cua/crates/cua-daemon/Cargo.toml b/libs/cua/crates/cua-daemon/Cargo.toml
index 5a9727cd48..3ddb7e26e1 100644
--- a/libs/cua/crates/cua-daemon/Cargo.toml
+++ b/libs/cua/crates/cua-daemon/Cargo.toml
@@ -79,6 +79,9 @@ tempfile = { workspace = true, optional = true }
axum = { workspace = true, optional = true }
tower-http = { workspace = true, optional = true }
+[target.'cfg(unix)'.dependencies]
+libc = "0.2"
+
[dev-dependencies]
cua-host = { workspace = true, features = ["testing"] }
cua-spaces = { path = "../cua-spaces", features = ["mcp-client"] }
diff --git a/libs/cua/crates/cua-daemon/src/lib.rs b/libs/cua/crates/cua-daemon/src/lib.rs
index fc18838aa0..1094023892 100644
--- a/libs/cua/crates/cua-daemon/src/lib.rs
+++ b/libs/cua/crates/cua-daemon/src/lib.rs
@@ -675,6 +675,114 @@ pub fn pid_alive(pid: u32) -> bool {
pid != 0 && cua_vmm::host::pid_alive(pid)
}
+/// Whether `command` (a process's command line) runs a cua daemon:
+/// `<dir>/cua daemon start ...` (or the Cua Spaces `cua-spaces-cli`).
+#[cfg(unix)]
+fn is_daemon_command(command: &str) -> bool {
+ let Some(at) = command.find(" daemon start") else {
+ return false;
+ };
+ // The executable path may hold spaces ("Cua Spaces.app"): its last
+ // component is the program.
+ std::path::Path::new(command[..at].trim())
+ .file_name()
+ .and_then(|n| n.to_str())
+ .is_some_and(|n| matches!(n, "cua" | "cua-spaces-cli"))
+}
+
+/// Whether the live process `pid` is a cua daemon (by its command line),
+/// so a pid read earlier was not reused by another program.
+#[cfg(unix)]
+pub fn is_daemon_process(pid: u32) -> bool {
+ std::process::Command::new("ps")
+ .args(["-o", "command=", "-p", &pid.to_string()])
+ .stderr(std::process::Stdio::null())
+ .output()
+ .is_ok_and(|o| o.status.success() && is_daemon_command(&String::from_utf8_lossy(&o.stdout)))
+}
+
+/// Ends the cua daemon process `pid` that did not exit when asked: SIGTERM,
+/// then SIGKILL, waiting up to `grace` after each. A daemon that closed
+/// its listeners on `Shutdown` but never exited kept holding the Keyvault
+/// socket after an app update (trycua/cua#4950). Never signals this
+/// process or one that is not a cua daemon. Returns whether `pid` is gone.
+/// Blocking; call it off the async runtime.
+#[cfg(unix)]
+pub fn terminate_daemon(pid: u32, grace: std::time::Duration) -> bool {
+ if pid == 0 || pid == std::process::id() || pid > i32::MAX as u32 {
+ return false;
+ }
+ if !pid_alive(pid) {
+ return true;
+ }
+ if !is_daemon_process(pid) {
+ return false;
+ }
+ for signal in [libc::SIGTERM, libc::SIGKILL] {
+ // SAFETY: plain kill(2) on a positive pid checked above.
+ unsafe { libc::kill(pid as libc::pid_t, signal) };
+ let deadline = std::time::Instant::now() + grace;
+ while std::time::Instant::now() < deadline {
+ if !pid_alive(pid) {
+ return true;
+ }
+ std::thread::sleep(std::time::Duration::from_millis(50));
+ }
+ }
+ !pid_alive(pid)
+}
+
+/// The pid of the process listening on the Unix socket at `path`
+/// (`LOCAL_PEERPID` on macOS, `SO_PEERCRED` on Linux); `None` when nothing
+/// listens or the OS does not say.
+#[cfg(unix)]
+pub fn socket_listener_pid(path: &std::path::Path) -> Option<u32> {
+ use std::os::fd::AsRawFd;
+ let stream = std::os::unix::net::UnixStream::connect(path).ok()?;
+ let fd = stream.as_raw_fd();
+ #[cfg(target_vendor = "apple")]
+ {
+ let mut pid: libc::pid_t = 0;
+ let mut len = std::mem::size_of::<libc::pid_t>() as libc::socklen_t;
+ // SAFETY: getsockopt into a properly sized pid_t on a live socket.
+ let rc = unsafe {
+ libc::getsockopt(
+ fd,
+ libc::SOL_LOCAL,
+ libc::LOCAL_PEERPID,
+ (&mut pid as *mut libc::pid_t).cast(),
+ &mut len,
+ )
+ };
+ (rc == 0 && pid > 0).then_some(pid as u32)
+ }
+ #[cfg(any(target_os = "linux", target_os = "android"))]
+ {
+ let mut cred = libc::ucred {
+ pid: 0,
+ uid: 0,
+ gid: 0,
+ };
+ let mut len = std::mem::size_of::<libc::ucred>() as libc::socklen_t;
+ // SAFETY: getsockopt into a properly sized ucred on a live socket.
+ let rc = unsafe {
+ libc::getsockopt(
+ fd,
+ libc::SOL_SOCKET,
+ libc::SO_PEERCRED,
+ (&mut cred as *mut libc::ucred).cast(),
+ &mut len,
+ )
+ };
+ (rc == 0 && cred.pid > 0).then_some(cred.pid as u32)
+ }
+ #[cfg(not(any(target_vendor = "apple", target_os = "linux", target_os = "android")))]
+ {
+ let _ = fd;
+ None
+ }
+}
+
/// Whether a server accepts connections on the Unix socket at `path`
/// (a missing file or a refused connect is not listening).
#[cfg(unix)]
@@ -769,6 +877,89 @@ pub fn random_token() -> String {
mod tests {
use super::*;
+ #[cfg(unix)]
+ #[test]
+ fn only_a_cua_daemon_command_line_counts() {
+ assert!(is_daemon_command(
+ "/Applications/Cua Spaces.app/Contents/Resources/native/cua daemon start --foreground --loopback 127.0.0.1:0\n"
+ ));
+ assert!(is_daemon_command(
+ "/usr/local/bin/cua-spaces-cli daemon start --foreground"
+ ));
+ assert!(!is_daemon_command("/usr/bin/vim daemon start"));
+ assert!(!is_daemon_command(
+ "/Applications/Cua Spaces.app/Contents/MacOS/cua daemon stop"
+ ));
+ assert!(!is_daemon_command(""));
+ }
+
+ /// A process that looks like a daemon and ignores SIGTERM, as the
+ /// daemon that never exited after `Shutdown` did (#4950); reaped by a
+ /// thread so a killed one does not linger as a zombie.
+ #[cfg(unix)]
+ fn stubborn_daemon(dir: &std::path::Path) -> (u32, std::sync::mpsc::Receiver<()>) {
+ use std::os::unix::fs::PermissionsExt;
+ let exe = dir.join("cua");
+ std::fs::write(&exe, "#!/bin/sh\ntrap '' TERM\nwhile :; do sleep 1; done\n").unwrap();
+ std::fs::set_permissions(&exe, std::fs::Permissions::from_mode(0o755)).unwrap();
+ let mut child = std::process::Command::new(&exe)
+ .args(["daemon", "start", "--foreground"])
+ .spawn()
+ .unwrap();
+ let pid = child.id();
+ let (tx, rx) = std::sync::mpsc::channel();
+ std::thread::spawn(move || {
+ let _ = child.wait();
+ let _ = tx.send(());
+ });
+ // Until the shell has set its trap.
+ std::thread::sleep(std::time::Duration::from_millis(300));
+ (pid, rx)
+ }
+
+ #[cfg(unix)]
+ #[test]
+ fn a_daemon_that_ignores_sigterm_is_killed() {
+ let dir = tempfile::tempdir().unwrap();
+ let (pid, exited) = stubborn_daemon(dir.path());
+ assert!(is_daemon_process(pid));
+ assert!(terminate_daemon(pid, std::time::Duration::from_secs(2)));
+ exited
+ .recv_timeout(std::time::Duration::from_secs(5))
+ .expect("the stubborn daemon exited");
+ }
+
+ #[cfg(unix)]
+ #[test]
+ fn terminate_never_signals_another_program() {
+ let mut other = std::process::Command::new("sleep")
+ .arg("30")
+ .spawn()
+ .unwrap();
+ assert!(!is_daemon_process(other.id()));
+ assert!(!terminate_daemon(
+ other.id(),
+ std::time::Duration::from_millis(100)
+ ));
+ assert!(other.try_wait().unwrap().is_none(), "left running");
+ assert!(!terminate_daemon(
+ std::process::id(),
+ std::time::Duration::from_millis(100)
+ ));
+ other.kill().unwrap();
+ other.wait().unwrap();
+ }
+
+ #[cfg(unix)]
+ #[test]
+ fn the_listener_of_a_socket_is_found() {
+ let dir = tempfile::tempdir().unwrap();
+ let path = dir.path().join("s.sock");
+ assert_eq!(socket_listener_pid(&path), None);
+ let _listener = std::os::unix::net::UnixListener::bind(&path).unwrap();
+ assert_eq!(socket_listener_pid(&path), Some(std::process::id()));
+ }
+
#[cfg(feature = "spaces")]
#[test]
fn a_teleport_refusal_says_teleport_refused_once() {
diff --git a/libs/cua/crates/cua-spaces-ext/src/daemon/keyvault.rs b/libs/cua/crates/cua-spaces-ext/src/daemon/keyvault.rs
index 34094938b1..4a7bbcdbd9 100644
--- a/libs/cua/crates/cua-spaces-ext/src/daemon/keyvault.rs
+++ b/libs/cua/crates/cua-spaces-ext/src/daemon/keyvault.rs
@@ -989,6 +989,32 @@ pub async fn serve_socket(broker: Arc<Broker>, path: std::path::PathBuf) {
tracing::debug!(error = %e, "keyvault: socket busy, retrying");
tokio::time::sleep(std::time::Duration::from_millis(250)).await;
}
+ // Still held after 10 s: by a daemon this one replaced that
+ // never exited (it outlived an app update, #4950). This
+ // daemon is the one its cua home runs, so that one is ended.
+ Err(e) if tries == 40 => {
+ tries += 1;
+ let p = path.clone();
+ let ended = tokio::task::spawn_blocking(move || {
+ cua_daemon::socket_listener_pid(&p).filter(|&pid| {
+ pid != std::process::id()
+ && cua_daemon::terminate_daemon(
+ pid,
+ std::time::Duration::from_secs(3),
+ )
+ })
+ })
+ .await
+ .ok()
+ .flatten();
+ match ended {
+ Some(pid) => tracing::warn!(
+ pid,
+ "keyvault: ended an old cua daemon that still held the socket"
+ ),
+ None => tracing::debug!(error = %e, "keyvault: socket busy"),
+ }
+ }
Err(e) => {
tracing::warn!(error = %e, path = %path.display(), "keyvault: socket not served");
break None;
diff --git a/samples/openkoalabot-example-tauri/src-tauri/Cargo.lock b/samples/openkoalabot-example-tauri/src-tauri/Cargo.lock
index ec562e52e8..ed2c8b91c3 100644
--- a/samples/openkoalabot-example-tauri/src-tauri/Cargo.lock
+++ b/samples/openkoalabot-example-tauri/src-tauri/Cargo.lock
@@ -1351,6 +1351,7 @@ dependencies = [
"humantime",
"hyper",
"hyper-util",
+ "libc",
"pbjson-types",
"prost",
"prost-types",
diff --git a/tests/e2e/cua-sdk/rust/Cargo.lock b/tests/e2e/cua-sdk/rust/Cargo.lock
index 8d810a1c0a..36f14950cb 100644
--- a/tests/e2e/cua-sdk/rust/Cargo.lock
+++ b/tests/e2e/cua-sdk/rust/Cargo.lock
@@ -1331,6 +1331,7 @@ dependencies = [
"http-body-util",
"humantime",
"hyper-util",
+ "libc",
"pbjson-types",
"prost",
"prost-types",
diff --git a/tests/e2e/local-runtimes/Cargo.lock b/tests/e2e/local-runtimes/Cargo.lock
index 9fe23e4b3b..bea9e7a70c 100644
--- a/tests/e2e/local-runtimes/Cargo.lock
+++ b/tests/e2e/local-runtimes/Cargo.lock
@@ -786,6 +786,7 @@ dependencies = [
"http-body-util",
"humantime",
"hyper-util",
+ "libc",
"pbjson-types",
"prost",
"prost-types",