Make Codex and Claude Code subscriptions go further: a local model router that gives routine agent work to economical models and keeps your strongest model for the hard parts.
Rust ai-agentsai-gatewayclaude-codecodexcoding-agentcost-optimizationdeepseekllm-gatewayllm-routerlocal-firstmodel-routingrusttask-routingtauri
test: prevent subscription startup listener port races (#181)
澄潭795edc04b0f0
diff --git a/Cargo.lock b/Cargo.lockindex 3013dd18..653ea267 100644--- a/Cargo.lock+++ b/Cargo.lock@@ -2314,6 +2314,7 @@ dependencies = ["base64 0.22.1","bytes","command-fds",+ "fs2","futures","getrandom 0.3.4","hiroute-application",diff --git a/crates/daemon/Cargo.toml b/crates/daemon/Cargo.tomlindex caaa3dac..17baf23a 100644--- a/crates/daemon/Cargo.toml+++ b/crates/daemon/Cargo.toml@@ -53,6 +53,7 @@ name = "hirouted"path = "src/control/bin.rs"[dev-dependencies]+fs2.workspace = truehiroute-client-core = { path = "../client-core" }tokio = { workspace = true, features = ["test-util"] }tokio-util.workspace = truediff --git a/crates/daemon/src/role_all/subscription_startup_tests.rs b/crates/daemon/src/role_all/subscription_startup_tests.rsindex ab07069c..e7d79134 100644--- a/crates/daemon/src/role_all/subscription_startup_tests.rs+++ b/crates/daemon/src/role_all/subscription_startup_tests.rs@@ -1,8 +1,9 @@//! Provider-local source errors must not become daemon startup failures.use super::*;-use std::net::TcpListener;use std::process::Command;+use crate::test_support::ListenerPortLease;+fn private_root() -> tempfile::TempDir {let builder = tempfile::Builder::new();#[cfg(unix)]@@ -29,11 +30,16 @@ fn invalid_subscription_source_is_local_to_its_provider() {"proxy-invalid",] {let root = private_root();+ let listener = ListenerPortLease::reserve().unwrap();let mut child = Command::new(std::env::current_exe().unwrap());child.args(["--exact", TEST, "--nocapture"]).env_clear().env(CHILD, case)+ .env(+ "HIROUTE_SUBSCRIPTION_STARTUP_LISTEN",+ listener.address.to_string(),+ ).env("HOME", root.path()).env("PATH", "/usr/bin:/bin").env("CODEX_HOME", root.path().join(".codex"))@@ -79,9 +85,12 @@ fn invalid_subscription_source_is_local_to_its_provider() {proxy.mark_applied(&desired).unwrap();std::fs::write(root.join("subscription-proxy.json"), b"{broken").unwrap();}- let socket = TcpListener::bind("127.0.0.1:0").unwrap();- let listen = socket.local_addr().unwrap();- drop(socket);+ // The parent holds the address lease through child shutdown, outside the host's+ // outbound ephemeral range. Only the real Gateway owns the listening socket.+ let listen = std::env::var("HIROUTE_SUBSCRIPTION_STARTUP_LISTEN")+ .unwrap()+ .parse()+ .unwrap();let config = RoleAllConfig::new(root.join("storage"),root.join("runtime"),diff --git a/crates/daemon/src/test_support.rs b/crates/daemon/src/test_support.rsindex d2a38f8c..a4b826a1 100644--- a/crates/daemon/src/test_support.rs+++ b/crates/daemon/src/test_support.rs@@ -1,5 +1,8 @@//! Isolate tests that compose production discovery without mutating process-global HOME.+mod listener_port;+pub(crate) use listener_port::ListenerPortLease;+pub(crate) fn isolated_agent_home(test_name: &str) -> bool {isolated_agent_home_inner(test_name, false, None)}diff --git a/crates/daemon/src/test_support/listener_port.rs b/crates/daemon/src/test_support/listener_port.rsnew file mode 100644index 00000000..5d599c66--- /dev/null+++ b/crates/daemon/src/test_support/listener_port.rs@@ -0,0 +1,234 @@+//! Keep fixture listeners separate from outbound clients and other fixture processes.+use std::fs::{File, OpenOptions};+use std::io;+use std::net::{Ipv4Addr, SocketAddr, TcpListener};+use std::path::{Path, PathBuf};++pub(crate) struct ListenerPortLease {+ pub(crate) address: SocketAddr,+ _lease: File,+}++impl ListenerPortLease {+ pub(crate) fn reserve() -> io::Result<Self> {+ let directory = lease_directory()?;+ let (first, last) = ephemeral_range()?;+ let candidates: Vec<_> = (10000..30000)+ .filter(|port| !(first..=last).contains(port))+ .collect();+ let mut random = [0; 2];+ getrandom::fill(&mut random)+ .map_err(|_| io::Error::other("cannot randomize fixture ports"))?;+ let start = usize::from(u16::from_ne_bytes(random));+ Self::reserve_from(+ &directory,+ candidates+ .iter()+ .cycle()+ .skip(start)+ .take(candidates.len().min(128))+ .copied(),+ )+ }++ fn reserve_from(directory: &Path, ports: impl IntoIterator<Item = u16>) -> io::Result<Self> {+ for port in ports {+ let mut options = OpenOptions::new();+ options.create(true).truncate(false).read(true).write(true);+ #[cfg(unix)]+ {+ use std::os::unix::fs::OpenOptionsExt;+ options.mode(0o600).custom_flags(nix::libc::O_NOFOLLOW);+ }+ let lease = options.open(directory.join(format!("{port}.lock")))?;+ match fs2::FileExt::try_lock_exclusive(&lease) {+ Ok(()) => {}+ Err(error)+ if error.raw_os_error() == fs2::lock_contended_error().raw_os_error() =>+ {+ continue;+ }+ Err(error) => return Err(error),+ }+ let address = SocketAddr::from((Ipv4Addr::LOCALHOST, port));+ match TcpListener::bind(address) {+ Ok(probe) => drop(probe),+ Err(error) if error.kind() == io::ErrorKind::AddrInUse => continue,+ Err(error) => return Err(error),+ }+ // The real Gateway binds the socket. Keep the file lease until it has stopped;+ // never unlink lock files, since another process may already have opened one.+ return Ok(Self {+ address,+ _lease: lease,+ });+ }+ Err(io::Error::new(+ io::ErrorKind::AddrNotAvailable,+ "no unoccupied fixture listener address",+ ))+ }+}++fn lease_directory() -> io::Result<PathBuf> {+ // Match publication_product.py on Unix, across worktrees and private runner TMPDIRs.+ #[cfg(unix)]+ let directory = PathBuf::from(format!(+ "/tmp/hiroute-product-listener-ports-{}",+ nix::unistd::getuid()+ ));+ #[cfg(windows)]+ let directory = PathBuf::from(+ std::env::var_os("LOCALAPPDATA")+ .ok_or_else(|| io::Error::other("LOCALAPPDATA is unavailable"))?,+ )+ .join("hiroute-product-listener-ports");+ let builder = std::fs::DirBuilder::new();+ #[cfg(unix)]+ let builder = {+ use std::os::unix::fs::DirBuilderExt;+ let mut builder = builder;+ builder.mode(0o700);+ builder+ };+ match builder.create(&directory) {+ Ok(()) => {}+ Err(error) if error.kind() == io::ErrorKind::AlreadyExists => {}+ Err(error) => return Err(error),+ }+ let metadata = std::fs::symlink_metadata(&directory)?;+ let valid = metadata.is_dir() && !metadata.file_type().is_symlink();+ #[cfg(unix)]+ let valid = {+ use std::os::unix::fs::MetadataExt;+ valid+ && metadata.uid() == nix::unistd::getuid().as_raw()+ && metadata.mode() & 0o777 == 0o700+ };+ if !valid {+ return Err(io::Error::other(+ "fixture listener lease directory is not private and owned",+ ));+ }+ Ok(directory)+}++#[cfg(any(target_os = "linux", target_os = "macos", windows))]+fn ephemeral_range() -> io::Result<(u16, u16)> {+ #[cfg(target_os = "linux")]+ let bounds = std::fs::read_to_string("/proc/sys/net/ipv4/ip_local_port_range")?;+ #[cfg(target_os = "macos")]+ let bounds = {+ let output = std::process::Command::new("/usr/sbin/sysctl")+ .args([+ "-n",+ "net.inet.ip.portrange.first",+ "net.inet.ip.portrange.last",+ ])+ .output()?;+ if !output.status.success() {+ return Err(io::Error::other(+ "cannot read outbound ephemeral port range",+ ));+ }+ String::from_utf8_lossy(&output.stdout).into_owned()+ };+ #[cfg(windows)]+ let bounds = {+ let executable =+ hiroute_platform::windows::launcher::system_executable(Path::new("netsh.exe"))?;+ let output = std::process::Command::new(executable)+ .args(["int", "ipv4", "show", "dynamicport", "tcp"])+ .output()?;+ let numbers: Vec<u32> = String::from_utf8_lossy(&output.stdout)+ .lines()+ .filter_map(|line| line.split_once(':')?.1.trim().parse().ok())+ .collect();+ if !output.status.success() || numbers.len() != 2 || numbers[1] == 0 {+ return Err(io::Error::other(+ "cannot read outbound ephemeral port range",+ ));+ }+ format!("{} {}", numbers[0], numbers[0] + numbers[1] - 1)+ };+ let ports: Vec<u16> = bounds+ .split_whitespace()+ .map(str::parse)+ .collect::<Result<_, _>>()+ .map_err(io::Error::other)?;+ match ports.as_slice() {+ [first, last] if first <= last => Ok((*first, *last)),+ _ => Err(io::Error::other("invalid outbound ephemeral port range")),+ }+}++#[cfg(not(any(target_os = "linux", target_os = "macos", windows)))]+fn ephemeral_range() -> io::Result<(u16, u16)> {+ Err(io::Error::new(+ io::ErrorKind::Unsupported,+ "unknown outbound ephemeral port range",+ ))+}++#[test]+fn listener_lease_avoids_outbound_ephemeral_ports() {+ let lease = ListenerPortLease::reserve().unwrap();+ let (first, last) = ephemeral_range().unwrap();+ assert!(!(first..=last).contains(&lease.address.port()));+}++#[test]+fn listener_lease_survives_socket_handoff_and_releases_on_drop() {+ const TEST: &str =+ "test_support::listener_port::listener_lease_survives_socket_handoff_and_releases_on_drop";+ const CHILD_PORT: &str = "HIROUTE_TEST_LEASED_LISTENER_PORT";+ if let Ok(port) = std::env::var(CHILD_PORT) {+ assert_eq!(+ ListenerPortLease::reserve_from(&lease_directory().unwrap(), [port.parse().unwrap()])+ .err()+ .unwrap()+ .kind(),+ io::ErrorKind::AddrNotAvailable+ );+ return;+ }+ let global = ListenerPortLease::reserve().unwrap();+ let address = global.address;+ let child_tmp = super::private_tempdir();+ let output = std::process::Command::new(std::env::current_exe().unwrap())+ .args(["--exact", TEST, "--nocapture"])+ .env(CHILD_PORT, address.port().to_string())+ .env("TMPDIR", child_tmp.path())+ .env("TMP", child_tmp.path())+ .env("TEMP", child_tmp.path())+ .output()+ .unwrap();+ assert!(+ output.status.success(),+ "{}\n{}",+ String::from_utf8_lossy(&output.stdout),+ String::from_utf8_lossy(&output.stderr)+ );+ assert!(String::from_utf8_lossy(&output.stdout).contains("1 passed"));++ // Keep the global lease while exercising release in a private lock namespace,+ // so another fixture cannot take this address during the release assertion.+ let directory = super::private_tempdir();+ let lease = ListenerPortLease::reserve_from(directory.path(), [address.port()]).unwrap();+ let gateway_socket = TcpListener::bind(address).unwrap();+ drop(gateway_socket);+ assert_eq!(+ ListenerPortLease::reserve_from(directory.path(), [address.port()])+ .err()+ .unwrap()+ .kind(),+ io::ErrorKind::AddrNotAvailable+ );+ drop(lease);+ assert_eq!(+ ListenerPortLease::reserve_from(directory.path(), [address.port()])+ .unwrap()+ .address,+ address+ );+}diff --git a/docs/code-map/testing.md b/docs/code-map/testing.mdindex 4692f10a..42d0eb90 100644--- a/docs/code-map/testing.md+++ b/docs/code-map/testing.md@@ -64,6 +64,10 @@ The publication fixture leases its listener address across daemon restarts; donot substitute a released `bind(0)` probe. Its protected-input channel cannot beattached to an existing Desktop. Prepare that instance first, then use real UImutations. Main-Agent delegation must separately invoke the actual public CLI.+The isolated subscription startup test uses the [listener port lease](../../crates/daemon/src/test_support/listener_port.rs)+outside the host's outbound ephemeral range; the parent keeps that lease through+the isolated child's Gateway shutdown. On Unix it shares the publication fixture's+per-user address-lock namespace across worktrees and runner temporary directories.The [shared product session](../../crates/product-session/src) owns confirmation,exact-digest authority and command orchestration tests. Its permit-drop and