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.

Studio tests: give managed engines ports another worker cannot be handed (#15915)

Daniel Hand035dfffbe1a
diff --git a/studio/backend/tests/conftest.py b/studio/backend/tests/conftest.py
index d96adf5c9fd..47b62bd78af 100644
--- a/studio/backend/tests/conftest.py
+++ b/studio/backend/tests/conftest.py
@@ -629,6 +629,49 @@ def real_apple_metal_budget(monkeypatch):
)
+# Below the ephemeral range on Linux (32768+), macOS and Windows (49152+), one block per xdist worker.
+_ENGINE_PORT_BASE = 21000
+_ENGINE_PORTS_PER_WORKER = 250
+_engine_port_counter = itertools.count()
+
+
[email protected](autouse = True)
+def _engine_ports_no_other_worker_can_take(monkeypatch):
+ """managed_engine._free_port releases an ephemeral port that the engine binds later. In that
+ gap any bind(0) on the runner, such as another worker's test server, can be handed the same
+ port and answer /health, and a start that should time out returns ready. Each worker gets its
+ own ports below the ephemeral range instead, which bind(0) never hands out."""
+ module = sys.modules.get("core.inference.managed_engine")
+ if module is None:
+ return
+ import socket
+
+ worker = os.environ.get("PYTEST_XDIST_WORKER", "gw0")
+ index = int(worker[2:]) if worker[2:].isdigit() else 0
+ base = _ENGINE_PORT_BASE + (index % 40) * _ENGINE_PORTS_PER_WORKER
+
+ def _worker_port(limit = None):
+ for _ in range(_ENGINE_PORTS_PER_WORKER):
+ port = base + next(_engine_port_counter) % _ENGINE_PORTS_PER_WORKER
+ with socket.socket() as sock:
+ try:
+ sock.bind(("127.0.0.1", port))
+ except OSError:
+ continue
+ return port
+ raise RuntimeError("no free engine port left in this worker's block")
+
+ _worker_port.__wrapped__ = module._free_port
+ monkeypatch.setattr(module, "_free_port", _worker_port)
+
+
+def real_engine_free_port(monkeypatch):
+ """The real allocator, for the tests of the allocator itself."""
+ from core.inference import managed_engine
+ monkeypatch.setattr(managed_engine, "_free_port", managed_engine._free_port.__wrapped__)
+
+
@pytest.fixture(autouse = True)
def _not_on_metal(monkeypatch):
"""Keep co-residency verdicts off the host's device: on a Mac they take the Metal paths."""
diff --git a/studio/backend/tests/test_engine_ports.py b/studio/backend/tests/test_engine_ports.py
index 18805c60b73..8296350ba89 100644
--- a/studio/backend/tests/test_engine_ports.py
+++ b/studio/backend/tests/test_engine_ports.py
@@ -18,7 +18,9 @@ from core.inference import managed_engine
("sglang", [60000] * 100, None),
],
)
-def test_only_sglang_reserves_space_for_its_derived_port(monkeypatch, name, ports, expected):
+def test_only_sglang_reserves_space_for_its_derived_port(
+ monkeypatch, real_engine_free_port, name, ports, expected
+):
assigned = iter(ports)
class Socket:
@@ -51,3 +53,11 @@ def test_only_sglang_reserves_space_for_its_derived_port(monkeypatch, name, port
with pytest.raises(RuntimeError, match = message):
engine.start("model", 2048, [0], {})
assert selected == ([] if expected is None else [expected])
+
+
+def test_the_suite_hands_engines_ports_no_other_process_can_be_given():
+ """conftest keeps engine ports out of the ephemeral range: a port released there can be handed
+ to any bind(0) on the runner before the engine binds it, and that listener then answers /health."""
+ for _ in range(3):
+ port = managed_engine._free_port()
+ assert 21000 <= port < 31000, port
diff --git a/studio/backend/tests/test_managed_engines_rocm.py b/studio/backend/tests/test_managed_engines_rocm.py
index 0980fd53589..f56caa52bac 100644
--- a/studio/backend/tests/test_managed_engines_rocm.py
+++ b/studio/backend/tests/test_managed_engines_rocm.py
@@ -298,7 +298,9 @@ def test_status_lists_the_precisions_the_build_loads(amd_host, monkeypatch, tmp_
assert "int4" in install.status("vllm")["precisions"]
-def test_engine_port_stays_below_the_limit_when_the_os_offers_high_ports(monkeypatch):
+def test_engine_port_stays_below_the_limit_when_the_os_offers_high_ports(
+ monkeypatch, real_engine_free_port
+):
# Windows ephemeral ports start at 49152; past 55535 every OS-chosen port was refused.
taken = {30001}