summaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
authoraur-build <aur-build@localhost>2026-11-19 15:32:44 +0000
committeraur-build <aur-build@localhost>2026-11-19 15:32:44 +0000
commitec80c312b80ba51271fff0548c6d1e293d373455 (patch)
tree41bfc268c01f2a14b88fa90a71f08e855471cedf /tests
parent32eaaeae9e0fe2c2285fb79efa0e579150d8bd83 (diff)
downloadaur-build-ec80c312b80ba51271fff0548c6d1e293d373455.tar.gz
aur-build-ec80c312b80ba51271fff0548c6d1e293d373455.zip
sandbox: the agent listener belongs to the process, not to a build
`Ctx::new` constructed a backend for every build, and constructing a remote backend bound the agent listener. With a fixed `listen` — which is what any real deployment configures, since the operator has to tell the agents where to dial — the second concurrent build died of EADDRINUSE. A build failing for a reason that had nothing to do with its package is the worst kind of flake: it looks like the package's fault. `RemoteBackend::bind` now shares one bound listener per `(address, token)` for the life of the process, and hands each backend a clone of the dial-in queue. That is also what `remote` already meant: an agent dials in when the operator boots one and whichever build is waiting takes it. The token is part of the key deliberately — two tokens cannot share a listener, and quietly reusing the first one's would be a fail-open surprise, so the second bind is attempted and fails loudly instead. Backend construction moved out of `Ctx` into `sandbox::backend_for`, so "which backend" has exactly one answer and the validation guard travels with it. Tests cover the shared listener, the shared queue (the *second* backend takes the dial-in), the two-token refusal, and two concurrent `Ctx`es on one address — with an assertion that a raw second bind still fails, so the test cannot rot into proving nothing.
Diffstat (limited to 'tests')
-rw-r--r--tests/remote_agent.rs107
1 files changed, 107 insertions, 0 deletions
diff --git a/tests/remote_agent.rs b/tests/remote_agent.rs
index 74a227b..22e803a 100644
--- a/tests/remote_agent.rs
+++ b/tests/remote_agent.rs
@@ -94,3 +94,110 @@ fn blobs_round_trip_over_the_wire_and_a_bad_token_is_refused() {
assert!(err.to_string().contains("token"), "{err}");
let _ = bad.wait();
}
+
+// ---------------------------------------------------------------------------
+// One listener per process.
+//
+// `Ctx::new` used to construct a backend for every build, and constructing a
+// remote backend bound the agent listener. So a second concurrent build on a
+// fixed `listen` died of EADDRINUSE — a build failing for a reason that had
+// nothing to do with its package. The listener is a property of the process now.
+// ---------------------------------------------------------------------------
+
+fn remote_cfg(listen: &str, token: &str) -> aur_build::config::Config {
+ aur_build::config::Config {
+ sandbox: aur_build::config::Sandbox::Remote {
+ listen: listen.into(),
+ token: token.into(),
+ shared_workspace: true,
+ },
+ ..aur_build::config::Config::default()
+ }
+}
+
+/// A port nothing is holding, so two backends can be asked for the *same* fixed
+/// address — which is the only case where double-binding shows up.
+fn free_port() -> u16 {
+ let l = std::net::TcpListener::bind("127.0.0.1:0").unwrap();
+ let port = l.local_addr().unwrap().port();
+ drop(l);
+ port
+}
+
+#[test]
+fn two_builds_share_one_agent_listener() {
+ let addr = format!("127.0.0.1:{}", free_port());
+ let cfg = remote_cfg(&addr, "shared-token");
+ let a = aur_build::sandbox::backend_for(&cfg).unwrap();
+ let b = aur_build::sandbox::backend_for(&cfg).unwrap();
+ let (sa, sb) = (&a.status().unwrap()[0].id, &b.status().unwrap()[0].id);
+ assert_eq!(sa, sb, "both must report the one listener: {sa} vs {sb}");
+ assert!(sa.contains(&addr), "and it must be the configured address: {sa}");
+ // Why the sharing matters, asserted rather than assumed: a raw second bind
+ // on a listening address is EADDRINUSE (Rust binds and listens together, so
+ // SO_REUSEADDR does not rescue it). If this ever passes, the test above is
+ // vacuous and the regression it guards is back.
+ assert!(
+ std::net::TcpListener::bind(&addr).is_err(),
+ "a second bind on {addr} must fail, or nothing here proves anything"
+ );
+}
+
+/// Sharing the listener means sharing the queue of dial-ins, which is exactly
+/// the semantics `remote` already has: an agent dials in when the operator
+/// boots one, and whichever build is waiting takes it.
+#[test]
+fn a_dial_in_goes_to_whichever_backend_is_waiting() {
+ let addr = format!("127.0.0.1:{}", free_port());
+ let cfg = remote_cfg(&addr, "shared-queue");
+ let _first = aur_build::sandbox::backend_for(&cfg).unwrap();
+ let second = aur_build::sandbox::backend_for(&cfg).unwrap();
+
+ let mut agent = std::process::Command::new(agent_bin())
+ .args(["--hub", &addr, "--token", "shared-queue", "--ttl", "30"])
+ .spawn()
+ .expect("spawn sandbox-agent");
+
+ let spec = SandboxSpec::new("extra-x86_64", "task", Duration::from_secs(10));
+ let sbx = second.acquire(&spec);
+ let _ = agent.kill();
+ assert!(sbx.is_ok(), "the second backend must be able to take the dial-in: {:?}", sbx.err());
+}
+
+/// Two tokens cannot share a listener, and quietly reusing the first one's would
+/// be a fail-open surprise — so the second bind is attempted and fails loudly.
+#[test]
+fn a_different_token_on_the_same_address_is_refused_not_shared() {
+ let addr = format!("127.0.0.1:{}", free_port());
+ assert!(aur_build::sandbox::backend_for(&remote_cfg(&addr, "one")).is_ok());
+ let second = aur_build::sandbox::backend_for(&remote_cfg(&addr, "two"));
+ assert!(second.is_err(), "two tokens on one address must not silently share a listener");
+}
+
+/// The contract in its literal shape: two workers, two builds, one listener.
+/// Each worker builds a `Ctx`, and building a `Ctx` used to bind the listener —
+/// so the second worker's build died of EADDRINUSE before it looked at its
+/// package at all.
+#[test]
+fn two_concurrent_builds_do_not_fight_over_the_listener() {
+ let addr = format!("127.0.0.1:{}", free_port());
+ let (mut env, _fa, _fb) = env_with_two_fixtures("plain-make", "install-hooks");
+ env.cfg.max_concurrent_builds = 2;
+ env.cfg.sandbox = aur_build::config::Sandbox::Remote {
+ listen: addr.clone(),
+ token: "concurrent".into(),
+ shared_workspace: true,
+ };
+
+ let mut ids = Vec::new();
+ for name in ["plain-make", "install-hooks"] {
+ let c = approve_fixture(&env, name);
+ ids.push(pipeline::enqueue_for_candidate(&env.cfg, &env.db, &c).unwrap());
+ }
+ assert_eq!(ids.len(), 2);
+
+ let first = Ctx::new(&env.cfg, &env.db, &ids[0]);
+ let second = Ctx::new(&env.cfg, &env.db, &ids[1]);
+ assert!(first.is_ok(), "first build: {:?}", first.err());
+ assert!(second.is_ok(), "second concurrent build must not lose the port race");
+}