From 6cce3926ae852556d28dcb2ce34da7270d0c1002 Mon Sep 17 00:00:00 2001 From: Sebastian Gieseler Date: Fri, 23 Oct 2026 10:46:39 +0200 Subject: [PATCH] fix(hub): a hub that is still starting is no longer taken for another user's program The hub bound its port before its setup, of which a first `docker version` alone may take 5 s (its timeout) on a busy machine or while Docker Desktop starts. The port took connections it did not answer; the CLI, which waits 3 s for `/hub/ping`, took the hub for another user's program: `devstack hub status`, `devstack ui`, `devstack open` said "port ... answers, but not as a devstack hub of this user" and gave up. Seen as `the_token_goes_only_to_this_users_hub` failing on a loaded Windows machine. The hub now binds its port, and writes hub.json, after its setup: what accepts a connection answers it. The state lock and the token still come first, so `who_answers`' reading of an older hub stays true. A test (unix: a `docker` on PATH that takes 4 s) starts a hub and asks `hub status` as soon as the port takes connections. Co-Authored-By: Claude Opus 5.5 --- CHANGELOG.md | 11 +++++++++++ crates/devstack-hub/src/lib.rs | 35 ++++++++++++++++++---------------- crates/devstack/tests/hub.rs | 34 +++++++++++++++++++++++++++++++++ 3 files changed, 64 insertions(+), 16 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e4a6b70d..ada99e83 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -25,6 +25,17 @@ and listed with their assets on the header names each service's port here and one that is not the owner's. A lane no longer shared with this machine is disconnected. +### Fixed + +- **A hub that is still starting is no longer taken for another user's + program.** The hub bound its port before its setup, of which a first + `docker version` alone may take 5 s on a busy machine or while Docker + Desktop starts; the port took connections it did not answer, and a CLI + command in that time (`devstack ui`, `devstack hub status`, `devstack + open`), which waits 3 s for the hub's answer, said "port … answers, but + not as a devstack hub of this user" and gave up. The hub now binds its + port last: what accepts a connection answers it. + ## [0.19.0] - 2026-10-22 A restart of the stack carries its lanes over to the next daemon diff --git a/crates/devstack-hub/src/lib.rs b/crates/devstack-hub/src/lib.rs index 99c21aa7..8d9437a0 100644 --- a/crates/devstack-hub/src/lib.rs +++ b/crates/devstack-hub/src/lib.rs @@ -355,22 +355,6 @@ pub fn serve() -> Result<(), String> { let token = devstack_daemon::token::load_or_create().map_err(|e| format!("token: {e}"))?; let rt = tokio::runtime::Runtime::new().map_err(|e| e.to_string())?; rt.block_on(async move { - let v4 = tokio::net::TcpListener::bind(("127.0.0.1", port)) - .await - .map_err(|e| format!("port {port}: {e}"))?; - let v6 = tokio::net::TcpListener::bind(("::1", port)).await.ok(); - let info = HubInfo { - pid: std::process::id(), - port, - version: VERSION.into(), - started: now_ms(), - }; - std::fs::write( - dir.join("hub.json"), - serde_json::to_vec_pretty(&info).unwrap(), - ) - .map_err(|e| e.to_string())?; - let (tx, rx) = watch::channel(Vec::new()); let start_errors: StartErrors = Arc::default(); let errors = start_errors.clone(); @@ -428,6 +412,25 @@ pub fn serve() -> Result<(), String> { shutdown: shutdown.clone(), notify, }; + // The port last: what accepts a connection answers it. Bound before + // the setup above (a first `docker version` alone may take 5 s), the + // port took connections it did not answer, and the CLI, which asks + // `/hub/ping` for 3 s, took this hub for another user's program. + let v4 = tokio::net::TcpListener::bind(("127.0.0.1", port)) + .await + .map_err(|e| format!("port {port}: {e}"))?; + let v6 = tokio::net::TcpListener::bind(("::1", port)).await.ok(); + let info = HubInfo { + pid: std::process::id(), + port, + version: VERSION.into(), + started: now_ms(), + }; + std::fs::write( + dir.join("hub.json"), + serde_json::to_vec_pretty(&info).unwrap(), + ) + .map_err(|e| e.to_string())?; notify::start(state.clone()); let app = router(state); if let Some(v6) = v6 { diff --git a/crates/devstack/tests/hub.rs b/crates/devstack/tests/hub.rs index d307bf61..32b430c1 100644 --- a/crates/devstack/tests/hub.rs +++ b/crates/devstack/tests/hub.rs @@ -35,8 +35,14 @@ fn start() -> Hub { } fn start_in(home: tempfile::TempDir, port: u16) -> Hub { + start_env(home, port, &[]) +} + +/// A hub as [`start_in`] starts it, with `extra` in its environment. +fn start_env(home: tempfile::TempDir, port: u16, extra: &[(&str, std::ffi::OsString)]) -> Hub { let child = Command::new(BIN) .arg("hub") + .envs(extra.iter().map(|(k, v)| (k, v))) .env("DEVSTACK_HUB_PORT", port.to_string()) .env("LOCALAPPDATA", home.path()) .env("XDG_STATE_HOME", home.path()) @@ -395,6 +401,34 @@ fn ping_is_public_but_tells_only_the_landing_page() { assert!(!body.contains(&token(&h))); } +/// A hub whose port takes a connection answers it: its setup before (a first +/// `docker version` slow on a busy machine, up to 5 s) is over by then. Bound +/// first, it took connections it did not answer for seconds, and the CLI, +/// which waits 3 s for `/hub/ping`, said another user's program had the port. +#[cfg(unix)] +#[test] +fn a_hub_answers_once_its_port_takes_connections() { + use std::os::unix::fs::PermissionsExt; + let bin = tempfile::tempdir().unwrap(); + let docker = bin.path().join("docker"); + std::fs::write(&docker, "#!/bin/sh\nsleep 4\n").unwrap(); + std::fs::set_permissions(&docker, std::fs::Permissions::from_mode(0o755)).unwrap(); + let mut path = std::ffi::OsString::from(bin.path()); + path.push(":"); + path.push(std::env::var_os("PATH").unwrap_or_default()); + let h = start_env(tempfile::tempdir().unwrap(), free_port(), &[("PATH", path)]); + let out = Command::new(BIN) + .args(["hub", "status"]) + .env("DEVSTACK_HUB_PORT", h.port.to_string()) + .env("LOCALAPPDATA", h.home.path()) + .env("XDG_STATE_HOME", h.home.path()) + .env("DEVSTACK_NO_UPDATE_CHECK", "1") + .output() + .unwrap(); + let text = String::from_utf8_lossy(&out.stdout); + assert!(text.contains("hub running"), "{text}"); +} + /// H-3: the CLI sends the token only to a hub that proves it knows it. A /// program of another user on the hub's port gets nothing. #[test]