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.

Fix Studio port fallback on Windows (#9449)

imherrocfa31f27b89b
diff --git a/studio/backend/run.py b/studio/backend/run.py
index 6b27e517058..6e6f7535130 100644
--- a/studio/backend/run.py
+++ b/studio/backend/run.py
@@ -877,7 +877,14 @@ def _is_port_free(host: str, port: int) -> bool:
seen.add(key)
probe = socket.socket(family, socktype, proto)
sockets.append(probe)
- probe.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
+ # On Windows, SO_REUSEADDR lets a second socket bind a listening
+ # port when the incumbent also enabled it. That makes this probe
+ # report the port as free, then uvicorn fails with WinError 10048
+ # instead of letting _resolve_port fall back to the next port.
+ # POSIX keeps the option so a recently closed listener does not
+ # strand the preferred port in TIME_WAIT.
+ if sys.platform != "win32":
+ probe.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
if family == socket.AF_INET6 and hasattr(socket, "IPV6_V6ONLY"):
probe.setsockopt(socket.IPPROTO_IPV6, socket.IPV6_V6ONLY, 1)
probe.bind(sockaddr)
diff --git a/studio/backend/tests/test_studio_pid_files.py b/studio/backend/tests/test_studio_pid_files.py
index 0e9593f7253..03b4ad1de84 100644
--- a/studio/backend/tests/test_studio_pid_files.py
+++ b/studio/backend/tests/test_studio_pid_files.py
@@ -432,6 +432,21 @@ def test_a_hostname_resolves_the_same_way_the_bind_does(tmp_path):
assert run._addresses_collide(recorded, "localhost", 8889) is True
[email protected](sys.platform != "win32", reason = "Windows socket semantics")
+def test_windows_reuseaddr_listener_is_not_reported_as_a_free_port():
+ # Python HTTP servers commonly enable SO_REUSEADDR. On Windows, putting the
+ # same option on the probe lets its bind succeed even while that server is
+ # listening; uvicorn then fails later with WinError 10048 instead of using
+ # the existing 8888-8908 fallback.
+ with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as listener:
+ listener.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
+ listener.bind(("127.0.0.1", 0))
+ listener.listen()
+ port = listener.getsockname()[1]
+
+ assert run._is_port_free("127.0.0.1", port) is False
+
+
def test_a_hostname_records_every_address_it_resolves_to(tmp_path):
# `localhost` binds 127.0.0.1 AND ::1. Recording only the first lets a later
# launch on the other literal miss us and start a duplicate.
@@ -442,7 +457,10 @@ def test_a_hostname_records_every_address_it_resolves_to(tmp_path):
assert run._addresses_collide(recorded, literal, 8889) is True
-def test_port_probe_checks_every_resolved_bind_address(monkeypatch):
[email protected]("platform", ["win32", "linux"])
[email protected]("occupied", [False, True])
+def test_port_probe_checks_every_resolved_bind_address(monkeypatch, platform, occupied):
+ monkeypatch.setattr(run, "sys", SimpleNamespace(platform = platform))
bind_attempts = []
sockets = []
@@ -450,14 +468,15 @@ def test_port_probe_checks_every_resolved_bind_address(monkeypatch):
def __init__(self, family):
self.family = family
self.closed = False
+ self.options = []
sockets.append(self)
- def setsockopt(self, *_args):
- pass
+ def setsockopt(self, *args):
+ self.options.append(args)
def bind(self, sockaddr):
bind_attempts.append((self.family, sockaddr))
- if self.family == socket.AF_INET6:
+ if occupied and self.family == socket.AF_INET6:
raise OSError("address already in use")
def close(self):
@@ -467,8 +486,8 @@ def test_port_probe_checks_every_resolved_bind_address(monkeypatch):
socket,
"getaddrinfo",
lambda *_args, **_kwargs: [
- (socket.AF_INET, socket.SOCK_STREAM, 6, "", ("0.0.0.0", 8888)),
- (socket.AF_INET6, socket.SOCK_STREAM, 6, "", ("::", 8888, 0, 0)),
+ (socket.AF_INET, socket.SOCK_STREAM, 6, "", ("127.0.0.1", 8888)),
+ (socket.AF_INET6, socket.SOCK_STREAM, 6, "", ("::1", 8888, 0, 0)),
],
)
monkeypatch.setattr(
@@ -477,12 +496,18 @@ def test_port_probe_checks_every_resolved_bind_address(monkeypatch):
lambda family, _socktype, _proto: _ProbeSocket(family),
)
- assert run._is_port_free("dual-wildcard.test", 8888) is False
+ assert run._is_port_free("dual-stack.test", 8888) is (not occupied)
assert bind_attempts == [
- (socket.AF_INET, ("0.0.0.0", 8888)),
- (socket.AF_INET6, ("::", 8888, 0, 0)),
+ (socket.AF_INET, ("127.0.0.1", 8888)),
+ (socket.AF_INET6, ("::1", 8888, 0, 0)),
]
assert all(probe.closed for probe in sockets)
+ for probe in sockets:
+ assert ((socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) in probe.options) is (
+ platform != "win32"
+ )
+ if probe.family == socket.AF_INET6 and hasattr(socket, "IPV6_V6ONLY"):
+ assert (socket.IPPROTO_IPV6, socket.IPV6_V6ONLY, 1) in probe.options
def test_a_multi_address_record_matches_either_literal(tmp_path):