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-daemon): name the holder's pid when a second daemon is refused on Windows (#6402)

injaneityc4b67c4a5fec
diff --git a/libs/cua/crates/cua-daemon/src/lib.rs b/libs/cua/crates/cua-daemon/src/lib.rs
index c2fa46b750..638aa847c3 100644
--- a/libs/cua/crates/cua-daemon/src/lib.rs
+++ b/libs/cua/crates/cua-daemon/src/lib.rs
@@ -556,10 +556,24 @@ pub struct InstanceLock {
pub struct InstanceHeld {
/// The lock file.
pub path: PathBuf,
- /// The pid written in it.
+ /// The pid its holder wrote.
pub pid: Option<u32>,
}
+/// Where the holder of the lock at `lock` writes its pid. A Windows lock is
+/// mandatory: no other handle can read the locked file, so the pid goes in
+/// `daemon.lock.pid` beside it. Elsewhere the lock is advisory and the pid
+/// is in the lock file itself.
+fn instance_pid_path(lock: &std::path::Path) -> PathBuf {
+ if cfg!(windows) {
+ let mut p = lock.as_os_str().to_owned();
+ p.push(".pid");
+ PathBuf::from(p)
+ } else {
+ lock.to_path_buf()
+ }
+}
+
impl std::fmt::Display for InstanceHeld {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self.pid {
@@ -586,17 +600,17 @@ impl InstanceLock {
path: &std::path::Path,
wait: std::time::Duration,
) -> std::result::Result<Self, InstanceHeld> {
- use std::io::{Seek, Write};
if let Some(dir) = path.parent() {
let _ = std::fs::create_dir_all(dir);
}
+ let pid_path = instance_pid_path(path);
let held = || InstanceHeld {
path: path.to_path_buf(),
- pid: std::fs::read_to_string(path)
+ pid: std::fs::read_to_string(&pid_path)
.ok()
.and_then(|s| s.trim().parse().ok()),
};
- let mut file = match std::fs::OpenOptions::new()
+ let file = match std::fs::OpenOptions::new()
.read(true)
.write(true)
.create(true)
@@ -624,10 +638,7 @@ impl InstanceLock {
}
}
}
- let _ = file.set_len(0);
- let _ = file.rewind();
- let _ = write!(file, "{}", std::process::id());
- let _ = file.flush();
+ let _ = std::fs::write(&pid_path, std::process::id().to_string());
Ok(Self { _file: Some(file) })
}
}