Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
35 changes: 19 additions & 16 deletions crates/devstack-hub/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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 {
Expand Down
34 changes: 34 additions & 0 deletions crates/devstack/tests/hub.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down Expand Up @@ -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]
Expand Down
Loading