diff options
| author | aur-build <aur-build@localhost> | 2026-11-19 15:32:44 +0000 |
|---|---|---|
| committer | aur-build <aur-build@localhost> | 2026-11-19 15:32:44 +0000 |
| commit | ec80c312b80ba51271fff0548c6d1e293d373455 (patch) | |
| tree | 41bfc268c01f2a14b88fa90a71f08e855471cedf /src/sandbox/remote.rs | |
| parent | 32eaaeae9e0fe2c2285fb79efa0e579150d8bd83 (diff) | |
| download | aur-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 'src/sandbox/remote.rs')
| -rw-r--r-- | src/sandbox/remote.rs | 73 |
1 files changed, 57 insertions, 16 deletions
diff --git a/src/sandbox/remote.rs b/src/sandbox/remote.rs index e68ea0f..96b1ee6 100644 --- a/src/sandbox/remote.rs +++ b/src/sandbox/remote.rs @@ -19,10 +19,11 @@ //! manifest/tar sync from the design sketch; that is deliberately not here //! yet, and nothing should pretend otherwise. +use std::collections::HashMap; use std::io::BufReader; use std::net::TcpListener; use std::sync::atomic::{AtomicU64, Ordering}; -use std::sync::{Arc, Mutex}; +use std::sync::{Arc, Mutex, OnceLock}; use crossbeam_channel::Receiver; @@ -40,25 +41,65 @@ pub struct RemoteBackend { queue: Receiver<std::net::TcpStream>, } +/// A listener shared by every backend built from the same `(addr, token)`. +struct Bound { + local: String, + rx: Receiver<std::net::TcpStream>, +} + +/// One bound listener per `(address, token)` per process. +/// +/// The listener is a property of the *process*, not of a build. Binding it per +/// build — which is what happened while `Ctx::new` constructed a backend for +/// every build — meant a second concurrent build hit `EADDRINUSE` on a fixed +/// `listen` and failed for a reason that had nothing to do with its package. +/// Sharing the queue is also exactly the semantics `remote` already has: an +/// agent dials in when the operator boots one, and whichever build is waiting +/// takes it. +/// +/// The token is part of the key on purpose. Two different tokens cannot share a +/// listener, and reusing the first one's listener for a second, different token +/// would be a fail-open surprise; forcing a real bind makes the mistake loud. +fn registry() -> &'static Mutex<HashMap<(String, String), Bound>> { + static REGISTRY: OnceLock<Mutex<HashMap<(String, String), Bound>>> = OnceLock::new(); + REGISTRY.get_or_init(|| Mutex::new(HashMap::new())) +} + +fn bound_listener(addr: &str, token: &str) -> Result<Bound> { + let key = (addr.to_string(), token.to_string()); + // A poisoned registry means a panic in some other thread's bind; the map + // itself is still sound, so recover rather than wedge every later build — + // the same posture as the publish lock. + let mut reg = registry().lock().unwrap_or_else(|p| p.into_inner()); + if let Some(b) = reg.get(&key) { + return Ok(Bound { local: b.local.clone(), rx: b.rx.clone() }); + } + let listener = TcpListener::bind(addr)?; + let local = listener.local_addr()?.to_string(); + let (tx, rx) = crossbeam_channel::unbounded(); + std::thread::spawn(move || { + for stream in listener.incoming() { + match stream { + Ok(s) => { + let _ = tx.send(s); + } + Err(_) => break, + } + } + }); + let bound = Bound { local, rx }; + reg.insert(key, Bound { local: bound.local.clone(), rx: bound.rx.clone() }); + Ok(bound) +} + impl RemoteBackend { - /// Bind the agent listener. `addr` may name port 0; [`Self::local_addr`] + /// Bind the agent listener — or share the one this process already bound for + /// the same `(addr, token)`. `addr` may name port 0; [`Self::local_addr`] /// reports what actually got bound, which is how a test (or a supervisor) /// hands the address to the agent without a port negotiation dance. pub fn bind(cfg: &Config, addr: &str, token: &str) -> Result<Self> { - let listener = TcpListener::bind(addr)?; - let local = listener.local_addr()?.to_string(); - let (tx, rx) = crossbeam_channel::unbounded(); - std::thread::spawn(move || { - for stream in listener.incoming() { - match stream { - Ok(s) => { - let _ = tx.send(s); - } - Err(_) => break, - } - } - }); - Ok(Self { cfg: cfg.clone(), token: token.to_string(), addr: local, queue: rx }) + let bound = bound_listener(addr, token)?; + Ok(Self { cfg: cfg.clone(), token: token.to_string(), addr: bound.local, queue: bound.rx }) } pub fn local_addr(&self) -> &str { |
