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 opening a console window for the embedded daemon on Windows (#5136)

injaneitya9d912817b30
diff --git a/libs/cua-driver/rust/crates/cua-driver-sdk/src/embedded.rs b/libs/cua-driver/rust/crates/cua-driver-sdk/src/embedded.rs
index a80221bdf3..fa743005ae 100644
--- a/libs/cua-driver/rust/crates/cua-driver-sdk/src/embedded.rs
+++ b/libs/cua-driver/rust/crates/cua-driver-sdk/src/embedded.rs
@@ -441,6 +441,8 @@ impl EmbeddedCuaDriverHost {
Stdio::null()
})
.kill_on_drop(true);
+ #[cfg(target_os = "windows")]
+ prevent_console_window(&mut command);
for variable in safe_environment(&self.options.environment) {
command.env(variable.name, variable.value);
}
@@ -1140,6 +1142,18 @@ async fn terminate_startup_child(child: &mut Child, liveness: &mut ChildStdin) {
}
}
+#[cfg(target_os = "windows")]
+fn prevent_console_window(command: &mut Command) {
+ const CREATE_NO_WINDOW: u32 = 0x0800_0000;
+ #[link(name = "kernel32")]
+ unsafe extern "system" {
+ fn GetConsoleCP() -> u32;
+ }
+ if unsafe { GetConsoleCP() } == 0 {
+ command.creation_flags(CREATE_NO_WINDOW);
+ }
+}
+
#[cfg(test)]
mod tests {
use super::*;
@@ -1515,4 +1529,76 @@ mod tests {
.iter()
.any(|v| v.name.eq_ignore_ascii_case("CUA_DRIVER_KEY_GAP_MS") && v.value == "3"));
}
+
+ #[cfg(target_os = "windows")]
+ #[tokio::test]
+ async fn daemon_console_window_follows_the_host_console() {
+ use std::os::windows::process::CommandExt as _;
+
+ const ROLE: &str = "CUA_DRIVER_SDK_TEST_CONSOLE_ROLE";
+ const HOST_PID: &str = "CUA_DRIVER_SDK_TEST_CONSOLE_HOST_PID";
+ const DETACHED_PROCESS: u32 = 0x0000_0008;
+ const CREATE_NO_WINDOW: u32 = 0x0800_0000;
+ #[link(name = "kernel32")]
+ unsafe extern "system" {
+ fn GetConsoleWindow() -> *mut std::ffi::c_void;
+ fn GetConsoleProcessList(list: *mut u32, count: u32) -> u32;
+ }
+ let rerun = [
+ "--exact",
+ "embedded::tests::daemon_console_window_follows_the_host_console",
+ "--nocapture",
+ ];
+ let assert_probe_passed = |output: std::process::Output| {
+ let stdout = String::from_utf8_lossy(&output.stdout);
+ assert!(
+ output.status.success() && stdout.contains("1 passed"),
+ "{stdout}{}",
+ String::from_utf8_lossy(&output.stderr)
+ );
+ };
+
+ match std::env::var(ROLE).as_deref() {
+ Ok("probe-without-console") => {
+ let window = unsafe { GetConsoleWindow() };
+ assert!(
+ window.is_null(),
+ "a console window was opened for the daemon"
+ );
+ }
+ Ok("probe-with-console") => {
+ let host: u32 = std::env::var(HOST_PID).unwrap().parse().unwrap();
+ let mut attached = [0u32; 64];
+ let count = unsafe { GetConsoleProcessList(attached.as_mut_ptr(), 64) } as usize;
+ assert!(
+ attached[..count.min(64)].contains(&host),
+ "the daemon left its host's console"
+ );
+ }
+ Ok(host @ ("host-without-console" | "host-with-console")) => {
+ let mut probe = Command::new(std::env::current_exe().unwrap());
+ probe
+ .args(rerun)
+ .env(ROLE, host.replace("host-", "probe-"))
+ .env(HOST_PID, std::process::id().to_string())
+ .stdin(Stdio::null());
+ prevent_console_window(&mut probe);
+ assert_probe_passed(probe.output().await.unwrap());
+ }
+ _ => {
+ for (host, flags) in [
+ ("host-without-console", DETACHED_PROCESS),
+ ("host-with-console", CREATE_NO_WINDOW),
+ ] {
+ let output = std::process::Command::new(std::env::current_exe().unwrap())
+ .args(rerun)
+ .env(ROLE, host)
+ .creation_flags(flags)
+ .output()
+ .unwrap();
+ assert_probe_passed(output);
+ }
+ }
+ }
+ }
}