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.

ci: stop port races in the relay proxy tests and the portmap test (#5835)

Francesco Bonaccib77ec04ac075
diff --git a/libs/cua-spacesd/crates/cua-relay/tests/proxy.rs b/libs/cua-spacesd/crates/cua-relay/tests/proxy.rs
index 97a88f6e07..9f5a0fc9c8 100644
--- a/libs/cua-spacesd/crates/cua-relay/tests/proxy.rs
+++ b/libs/cua-spacesd/crates/cua-relay/tests/proxy.rs
@@ -19,6 +19,18 @@ const TOKEN: &str = "machine-secret-canary";
const ID: &str = "proxy-machine";
const CHILD: &str = "CUA_RELAY_PROXY_TEST_CHILD";
+/// Binds the fake proxy inside the child and points `var` at it before
+/// anything reads the variable. The parent never picks a port for the child
+/// to bind later: a port freed in between can be taken by a test running in
+/// parallel ("Address already in use"). The variable is set in the child
+/// only, before its runtime starts.
+fn bind_proxy(var: &str) -> std::net::TcpListener {
+ let listener = std::net::TcpListener::bind("127.0.0.1:0").unwrap();
+ listener.set_nonblocking(true).unwrap();
+ std::env::set_var(var, format!("http://{}", listener.local_addr().unwrap()));
+ listener
+}
+
#[test]
fn machine_join_uses_http_proxy() {
if std::env::var_os(CHILD).is_none() {
@@ -37,14 +49,7 @@ fn machine_join_uses_http_proxy() {
] {
child.env_remove(key);
}
- let proxy = std::net::TcpListener::bind("127.0.0.1:0").unwrap();
- let addr = proxy.local_addr().unwrap();
- drop(proxy);
- let result = child
- .env(CHILD, addr.to_string())
- .env("HTTP_PROXY", format!("http://{addr}"))
- .output()
- .unwrap();
+ let result = child.env(CHILD, "1").output().unwrap();
assert!(
result.status.success(),
"{}\n{}",
@@ -53,13 +58,14 @@ fn machine_join_uses_http_proxy() {
);
return;
}
+ let proxy = bind_proxy("HTTP_PROXY");
tokio::runtime::Runtime::new().unwrap().block_on(async {
let relay = Relay::new(RelayConfig { tokens: vec![TOKEN.into()], ..RelayConfig::default() });
let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
let addr = listener.local_addr().unwrap();
let serving = relay.clone();
let relay_task = tokio::spawn(async move { serving.serve(listener, std::future::pending()).await });
- let proxy = TcpListener::bind(std::env::var(CHILD).unwrap()).await.unwrap();
+ let proxy = TcpListener::from_std(proxy).unwrap();
let proxy_task = tokio::spawn(async move {
let (mut inbound, _) = proxy.accept().await.unwrap();
let mut head = Vec::new();
@@ -115,9 +121,6 @@ fn machine_join_uses_http_proxy() {
#[test]
fn proxy_tunnel_keeps_native_tls_verification() {
if std::env::var_os(CHILD).is_none() {
- let proxy = std::net::TcpListener::bind("127.0.0.1:0").unwrap();
- let addr = proxy.local_addr().unwrap();
- drop(proxy);
let mut child = Command::new(std::env::current_exe().unwrap());
child.args([
"--exact",
@@ -137,11 +140,7 @@ fn proxy_tunnel_keeps_native_tls_verification() {
] {
child.env_remove(key);
}
- let result = child
- .env(CHILD, addr.to_string())
- .env("HTTPS_PROXY", format!("http://{addr}"))
- .output()
- .unwrap();
+ let result = child.env(CHILD, "1").output().unwrap();
assert!(
result.status.success(),
"{}\n{}",
@@ -150,10 +149,9 @@ fn proxy_tunnel_keeps_native_tls_verification() {
);
return;
}
+ let proxy = bind_proxy("HTTPS_PROXY");
tokio::runtime::Runtime::new().unwrap().block_on(async {
- let proxy = TcpListener::bind(std::env::var(CHILD).unwrap())
- .await
- .unwrap();
+ let proxy = TcpListener::from_std(proxy).unwrap();
// This is a public test-only identity, never installed in a trust store.
let identity = native_tls::Identity::from_pkcs8(
include_bytes!("fixtures/untrusted-cert.pem"),
@@ -214,9 +212,6 @@ fn proxy_tunnel_keeps_native_tls_verification() {
#[allow(clippy::result_large_err)] // tungstenite callback fixes the error response type.
fn reconnect_through_proxy_rereads_token_and_checks_pinned_keys() {
if std::env::var_os(CHILD).is_none() {
- let proxy = std::net::TcpListener::bind("127.0.0.1:0").unwrap();
- let addr = proxy.local_addr().unwrap();
- drop(proxy);
let mut child = Command::new(std::env::current_exe().unwrap());
child.args([
"--exact",
@@ -236,11 +231,7 @@ fn reconnect_through_proxy_rereads_token_and_checks_pinned_keys() {
] {
child.env_remove(key);
}
- let result = child
- .env(CHILD, addr.to_string())
- .env("HTTP_PROXY", format!("http://{addr}"))
- .output()
- .unwrap();
+ let result = child.env(CHILD, "1").output().unwrap();
assert!(
result.status.success(),
"{}\n{}",
@@ -249,12 +240,11 @@ fn reconnect_through_proxy_rereads_token_and_checks_pinned_keys() {
);
return;
}
+ let proxy = bind_proxy("HTTP_PROXY");
tokio::runtime::Runtime::new().unwrap().block_on(async {
use base64::Engine as _;
use cua_relay::assertion::{RelayKey, JWKS_HEADER, OWNER_HEADER};
- let proxy = TcpListener::bind(std::env::var(CHILD).unwrap())
- .await
- .unwrap();
+ let proxy = TcpListener::from_std(proxy).unwrap();
let files = tempfile::tempdir().unwrap();
let token_path = files.path().join("token");
std::fs::write(&token_path, "first-token").unwrap();
diff --git a/libs/cua-spacesd/crates/cua-relay/tests/transport_failures.rs b/libs/cua-spacesd/crates/cua-relay/tests/transport_failures.rs
index 80dc6d2198..f3ff3bc53c 100644
--- a/libs/cua-spacesd/crates/cua-relay/tests/transport_failures.rs
+++ b/libs/cua-spacesd/crates/cua-relay/tests/transport_failures.rs
@@ -12,7 +12,6 @@ use tokio::{
use tokio_util::sync::CancellationToken;
const CHILD: &str = "CUA_TEST_TRANSPORT_FAILURE";
-const ADDRESS: &str = "CUA_TEST_PROXY_ADDRESS";
#[test]
fn session_failure_and_cancellation_contract() {
@@ -30,9 +29,6 @@ fn session_failure_and_cancellation_contract() {
"cancel-tls",
"cancel-ws",
] {
- let listener = std::net::TcpListener::bind("127.0.0.1:0").unwrap();
- let address = listener.local_addr().unwrap();
- drop(listener);
let mut child = Command::new(std::env::current_exe().unwrap());
child.args([
"--exact",
@@ -52,18 +48,15 @@ fn session_failure_and_cancellation_contract() {
] {
child.env_remove(key);
}
- let proxy = if mode == "bad-config" {
- "http://secret-user:[email protected]".to_owned()
- } else {
- format!("http://{address}")
- };
- let output = child
- .env(CHILD, mode)
- .env(ADDRESS, address.to_string())
- .env("HTTP_PROXY", &proxy)
- .env("HTTPS_PROXY", &proxy)
- .output()
- .unwrap();
+ // The child binds its own proxy and points the variables at it:
+ // a port picked here and rebound there can be taken in between
+ // by a test running in parallel ("Address already in use").
+ child.env(CHILD, mode);
+ if mode == "bad-config" {
+ let proxy = "http://secret-user:[email protected]";
+ child.env("HTTP_PROXY", proxy).env("HTTPS_PROXY", proxy);
+ }
+ let output = child.output().unwrap();
assert!(
output.status.success(),
"{mode}: {}\n{}",
@@ -73,11 +66,19 @@ fn session_failure_and_cancellation_contract() {
}
return;
}
+ let mode = std::env::var(CHILD).unwrap();
+ let proxy = std::net::TcpListener::bind("127.0.0.1:0").unwrap();
+ proxy.set_nonblocking(true).unwrap();
+ if mode != "bad-config" {
+ // Set in this child only, before its runtime starts.
+ let url = format!("http://{}", proxy.local_addr().unwrap());
+ std::env::set_var("HTTP_PROXY", &url);
+ std::env::set_var("HTTPS_PROXY", &url);
+ }
tokio::runtime::Runtime::new().unwrap().block_on(async {
- let mode = std::env::var(CHILD).unwrap();
let origin = TcpListener::bind("127.0.0.1:0").await.unwrap();
let address = origin.local_addr().unwrap();
- let proxy = TcpListener::bind(std::env::var(ADDRESS).unwrap()).await.unwrap();
+ let proxy = TcpListener::from_std(proxy).unwrap();
let stop = CancellationToken::new();
let stopped = stop.clone();
let (ready, waiting) = tokio::sync::oneshot::channel();
diff --git a/libs/cua-spacesd/crates/cua-spacesd-server/src/services/volume_portmap.rs b/libs/cua-spacesd/crates/cua-spacesd-server/src/services/volume_portmap.rs
index d556e6ef34..de54f06c6a 100644
--- a/libs/cua-spacesd/crates/cua-spacesd-server/src/services/volume_portmap.rs
+++ b/libs/cua-spacesd/crates/cua-spacesd-server/src/services/volume_portmap.rs
@@ -94,9 +94,21 @@ impl Portmap {
/// Serves on `127.0.0.1:port` (TCP and UDP) and points MOUNT and NFS at
/// `nfs`. `port` 0 picks one (tests).
pub async fn start(port: u16, nfs: Ports) -> std::io::Result<Portmap> {
- let tcp = TcpListener::bind(("127.0.0.1", port)).await?;
- let addr = tcp.local_addr()?;
- let udp = UdpSocket::bind(("127.0.0.1", addr.port())).await?;
+ // A fixed port binds once. Port 0 needs one number free for TCP and
+ // for UDP, and TCP and UDP have separate port spaces (Windows keeps
+ // separate excluded ranges, which refuse the bind with "permission
+ // denied"): when the UDP bind refuses the TCP pick, pick again.
+ let mut picks_left = if port == 0 { 32 } else { 1 };
+ let (tcp, udp, addr) = loop {
+ let tcp = TcpListener::bind(("127.0.0.1", port)).await?;
+ let addr = tcp.local_addr()?;
+ picks_left -= 1;
+ match UdpSocket::bind(("127.0.0.1", addr.port())).await {
+ Ok(udp) => break (tcp, udp, addr),
+ Err(e) if picks_left == 0 => return Err(e),
+ Err(_) => {}
+ }
+ };
let log = CallLog::default();
let l = log.clone();
let tcp_task = tokio::spawn(async move {