Skip to main content

bind_dynamic_port

Function bind_dynamic_port 

Source
pub async fn bind_dynamic_port() -> Result<TcpListener>
Expand description

Bind a TcpListener to 127.0.0.1, dynamically selecting a port.

Why: the historic default 7070 is convenient for clients but a stale process or a second daemon must not produce a noisy failure. Walking DEFAULT_HTTP_PORT..DEFAULT_HTTP_PORT+DYNAMIC_PORT_RANGE first preserves backwards compatibility for the common case; OS-assigned fallback (:0) guarantees the daemon always comes up even when every preferred port is busy. What: returns the first successful TcpListener (7070..=7079, then OS-assigned); caller inspects local_addr() to learn the chosen port. Test: bind_dynamic_port_returns_listener confirms it always binds some port even after another listener occupies the preferred one.