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-compat): keep sweeping a Space a dead gateway's create makes late (#6544)

injaneity24bf09959b36
diff --git a/libs/cua/crates/cua-compat/src/journal.rs b/libs/cua/crates/cua-compat/src/journal.rs
index bad3b8e10a..1dda07ff1e 100644
--- a/libs/cua/crates/cua-compat/src/journal.rs
+++ b/libs/cua/crates/cua-compat/src/journal.rs
@@ -246,6 +246,13 @@ impl Journal {
> CREATE_SETTLE
}
+ /// Restarts the [`Self::settled`] clock of `name`: a Space of that name
+ /// was just found (and ended), so a create still running in the daemon
+ /// may make it again.
+ pub fn unsettle(&self, name: &str) {
+ self.judged.lock().expect("journal").remove(name);
+ }
+
/// True the first time it is asked for `name` (to log a failure once).
pub fn first_failure(&self, name: &str) -> bool {
self.failed
@@ -305,8 +312,12 @@ impl Journaled {
for (name, who) in self.journal.orphans(&recorded) {
match self.inner.delete(&who, &name).await {
Ok(true) => {
+ // Kept, not resolved: the daemon may still be finishing
+ // the create a dead gateway asked for (a fork runs to
+ // completion) and record the Space after this delete.
+ // It goes once deletes have found nothing for a while.
tracing::info!("compat: ended the unowned Space {name}");
- self.journal.resolve(&name);
+ self.journal.unsettle(&name);
ended += 1;
}
Ok(false) | Err(BackendError::NotFound(_)) => {
@@ -556,9 +567,38 @@ mod tests {
claimed.lock().unwrap().insert("kept".into());
assert_eq!(b.sweep().await, 1);
assert_eq!(mock.names().await, ["kept"]);
- // Deleting through the wrapper resolves the entry.
+ // Deleting through the wrapper resolves the entry; the swept one is
+ // kept until deletes have found nothing for a while.
b.delete(&who, "kept").await.unwrap();
- assert!(j.owned_names().is_empty());
+ assert_eq!(j.owned_names(), ["lost"]);
+ }
+
+ /// A dead gateway's create still finishing in the daemon: the sweep ends
+ /// what is there, the create then records the Space again, and the next
+ /// sweep ends that too (the name is not forgotten after one delete).
+ #[tokio::test]
+ async fn a_space_made_again_after_its_sweep_is_swept_again() {
+ let d = tempfile::tempdir().unwrap();
+ let p = d.path().join("owned.jsonl");
+ Journal::open(Some(p.clone()))
+ .unwrap()
+ .begin("x", &Identity::default());
+ // The gateway restarts: an entry of an earlier process is judged now.
+ let mock = Arc::new(MockBackend::new());
+ let j = Journal::open(Some(p)).unwrap();
+ let b = Journaled::new(
+ mock.clone(),
+ j.clone(),
+ Some(Arc::new(|| Box::pin(async { HashSet::new() }))),
+ );
+ let who = Identity::default();
+ mock.create(&who, spec("x")).await.unwrap();
+ assert_eq!(b.sweep().await, 1);
+ assert!(mock.names().await.is_empty());
+ mock.create(&who, spec("x")).await.unwrap();
+ assert_eq!(b.sweep().await, 1, "the late Space is ended too");
+ assert!(mock.names().await.is_empty());
+ assert_eq!(j.owned_names(), ["x"], "kept until it settles");
}
#[tokio::test]
diff --git a/libs/cua/crates/cua-vmm/src/qemu/mod.rs b/libs/cua/crates/cua-vmm/src/qemu/mod.rs
index a266ec83f7..6bdaac5586 100644
--- a/libs/cua/crates/cua-vmm/src/qemu/mod.rs
+++ b/libs/cua/crates/cua-vmm/src/qemu/mod.rs
@@ -766,6 +766,10 @@ impl QemuRuntime {
)?;
let dir = self.dir(name);
std::fs::create_dir_all(&dir)?;
+ // A create dropped mid-way (its client cancelled) runs neither arm
+ // below: the directory it made goes with the future, or a disk with
+ // no state file stays where nothing lists or deletes it.
+ let mut made = DirGuard(Some(dir.clone()));
let arch = spec_like.effective_arch();
let disk = dir.join("disk.qcow2");
let res: Result<QemuState> = async {
@@ -835,16 +839,10 @@ impl QemuRuntime {
})
}
.await;
- match res {
- Ok(st) => {
- self.save(&st)?;
- Ok(st)
- }
- Err(e) => {
- let _ = std::fs::remove_dir_all(&dir);
- Err(e)
- }
- }
+ let st = res?;
+ self.save(&st)?;
+ made.0 = None;
+ Ok(st)
}
/// Write the cloud-init seed for this run: SSH key and/or raw user-data,
@@ -3791,6 +3789,24 @@ fn admission_file(memory: &Memory) -> Option<PathBuf> {
#[cfg(test)]
mod tests {
+ /// A VM create dropped before its state file was written takes its
+ /// directory with it; one that finished keeps it.
+ #[test]
+ fn a_dropped_create_leaves_no_directory() {
+ let d = tempfile::tempdir().unwrap();
+ let dropped = d.path().join("a");
+ std::fs::create_dir_all(&dropped).unwrap();
+ std::fs::write(dropped.join("disk.qcow2"), b"x").unwrap();
+ drop(super::DirGuard(Some(dropped.clone())));
+ assert!(!dropped.exists());
+ let kept = d.path().join("b");
+ std::fs::create_dir_all(&kept).unwrap();
+ let mut g = super::DirGuard(Some(kept.clone()));
+ g.0 = None;
+ drop(g);
+ assert!(kept.exists());
+ }
+
#[test]
fn the_gateway_sockets_live_in_a_short_folder_of_their_own() {
use super::gateway_run_dir;
@@ -5540,6 +5556,18 @@ impl LaunchGuard {
}
}
+/// Removes a new VM's directory when dropped armed (its create failed or
+/// was dropped before the state file was written).
+struct DirGuard(Option<PathBuf>);
+
+impl Drop for DirGuard {
+ fn drop(&mut self) {
+ if let Some(d) = self.0.take() {
+ let _ = std::fs::remove_dir_all(d);
+ }
+ }
+}
+
impl Drop for LaunchGuard {
fn drop(&mut self) {
if !self.armed || (self.pid.is_none() && self.tpm.is_none()) {