Skip to content

Local UI to run and train LLMs and diffusion models. Supports GGUF, MLX, Qwen3.8, DeepSeek-V4.1, MiniMax-H3, Gemma 4, FLUX and more.

Draw the probe port from below the ephemeral range (#11086)

Daniel Hanae6bc9ef536c
diff --git a/studio/src-tauri/src/commands.rs b/studio/src-tauri/src/commands.rs
index 97272e6db02..8a6443253f8 100644
--- a/studio/src-tauri/src/commands.rs
+++ b/studio/src-tauri/src/commands.rs
@@ -1558,13 +1558,33 @@ mod tests {
async fn a_port_with_nothing_on_it_reads_as_death_not_a_stall() {
// The other half: a backend that really exited leaves a closed port, and that must
// still be declared dead at three strikes.
+ let client = crate::loopback_http::client(Duration::from_secs(5)).unwrap();
+
+ // Binding port 0 and dropping the listener frees the port, it does not reserve it, and
+ // every other test in this binary binds port 0 too. The allocator can hand this one
+ // straight to one of them between the drop and the probe, and the request is answered
+ // rather than refused.
+ //
+ // So draw from below the ephemeral range instead of retrying: 32768 on Linux, 49152 on
+ // macOS and Windows, all above this. A sibling cannot be given a port from here, which
+ // is the difference that matters. Retrying a port that answered would be actively
+ // harmful, because answering means the probe just consumed someone's connection, and
+ // the one-shot fixture in loopback_http accepts exactly once before its own test sends
+ // the request it cares about. That does not fix the flake, it relocates it.
+ //
+ // A port here being occupied is a real listener on the machine, not a race, so walk a
+ // small window and let the probe assert on the one that was free.
let port = {
- let listener = TcpListener::bind(("127.0.0.1", 0)).await.unwrap();
- let port = listener.local_addr().unwrap().port();
- drop(listener);
- port
+ let mut free = None;
+ for candidate in 20_000..20_064u16 {
+ if let Ok(listener) = TcpListener::bind(("127.0.0.1", candidate)).await {
+ drop(listener);
+ free = Some(candidate);
+ break;
+ }
+ }
+ free.expect("every port in the probe window was already bound")
};
- let client = crate::loopback_http::client(Duration::from_secs(5)).unwrap();
let error = client
.get(format!("http://127.0.0.1:{port}/api/liveness"))