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.
Python agentaichatgptdeepseekfine-tuninggemmaimage-generationllamallmllmsopenaipythonqwenreinforcement-learningself-hostedstable-diffusiontext-to-speechttsuiunsloth
Studio tests: a separate engine port block for every xdist worker past 40 (#16245)
Daniel Han3a4600d7ffd4
diff --git a/studio/backend/tests/conftest.py b/studio/backend/tests/conftest.pyindex a46bcb7fa59..529bb40d658 100644--- a/studio/backend/tests/conftest.py+++ b/studio/backend/tests/conftest.py@@ -654,12 +654,53 @@ 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_PORT_SPAN = 10000_ENGINE_PORTS_PER_WORKER = 250_engine_port_counter = itertools.count()+_engine_port_slots = {}+++def _engine_port_block(slot, worker_count):+ """(first port, size); past 40 workers blocks shrink so each stays disjoint inside the span.+ Without a controller slot a replacement gwN (N >= count) uses the span's spare blocks first."""+ count = max(worker_count, 1)+ size = min(_ENGINE_PORTS_PER_WORKER, _ENGINE_PORT_SPAN // count)+ if size < 1:+ raise RuntimeError(f"{count} xdist workers do not fit {_ENGINE_PORT_SPAN} engine ports")+ return _ENGINE_PORT_BASE + (slot % (_ENGINE_PORT_SPAN // size)) * size, size++[email protected](optionalhook = True)+def pytest_configure_node(node):+ """xdist controller: a replacement worker gets a new gwN, so hand it the lowest slot no live+ worker holds (the crashed one's) instead of deriving the slot from N."""+ taken = set(_engine_port_slots.values())+ slot = next(i for i in itertools.count() if i not in taken)+ _engine_port_slots[node.gateway.id] = slot+ node.workerinput["engine_port_slot"] = slot++[email protected](optionalhook = True)+def pytest_testnodedown(node, error):+ _engine_port_slots.pop(node.gateway.id, None)+++def engine_port_block():+ return _engine_port_block+++def engine_port_slot_hooks(monkeypatch):+ """The controller-side slot hooks, on an empty slot table."""+ monkeypatch.setitem(globals(), "_engine_port_slots", {})+ return pytest_configure_node, pytest_testnodedown++@pytest.fixture(autouse = True)-def _engine_ports_no_other_worker_can_take(monkeypatch):+def _engine_ports_no_other_worker_can_take(monkeypatch, request):"""managed_engine._free_port releases an ephemeral port that the engine binds later. In thatgap any bind(0) on the runner, such as another worker's test server, can be handed the sameport and answer /health, and a start that should time out returns ready. Each worker gets its@@ -669,13 +710,16 @@ def _engine_ports_no_other_worker_can_take(monkeypatch):returnimport 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+ count = os.environ.get("PYTEST_XDIST_WORKER_COUNT", "")+ slot = getattr(request.config, "workerinput", {}).get("engine_port_slot")+ if slot is None:+ worker = os.environ.get("PYTEST_XDIST_WORKER", "gw0")[2:]+ slot = int(worker) if worker.isdigit() else 0+ base, size = _engine_port_block(slot, int(count) if count.isdigit() else 1)def _worker_port(limit = None):- for _ in range(_ENGINE_PORTS_PER_WORKER):- port = base + next(_engine_port_counter) % _ENGINE_PORTS_PER_WORKER+ for _ in range(size):+ port = base + next(_engine_port_counter) % sizewith socket.socket() as sock:try:sock.bind(("127.0.0.1", port))diff --git a/studio/backend/tests/test_engine_ports.py b/studio/backend/tests/test_engine_ports.pyindex 8296350ba89..3ad97adb672 100644--- a/studio/backend/tests/test_engine_ports.py+++ b/studio/backend/tests/test_engine_ports.py@@ -61,3 +61,52 @@ def test_the_suite_hands_engines_ports_no_other_process_can_be_given():for _ in range(3):port = managed_engine._free_port()assert 21000 <= port < 31000, port++[email protected]("workers", [1, 4, 16, 40, 41, 64, 192, 512])+def test_every_xdist_worker_gets_its_own_engine_port_block(engine_port_block, workers):+ """A shared block lets one worker bind a port another just released and answer its /health."""+ taken = set()+ for index in range(workers):+ base, size = engine_port_block(index, workers)+ block = set(range(base, base + size))+ assert size >= 1 and not (block & taken), (index, base, size)+ assert 21000 <= base and base + size <= 31000, (index, base, size)+ taken |= block++[email protected]("workers, crashed", [(2, 1), (4, 0), (40, 17), (64, 63)])+def test_a_replacement_xdist_worker_takes_the_crashed_workers_slot(+ engine_port_block, engine_port_slot_hooks, workers, crashed+):+ """xdist names any crash's replacement gw<count>; it must take the dead worker's block, not a survivor's."""+ import types++ configure_node, node_down = engine_port_slot_hooks++ def node(worker_id):+ return types.SimpleNamespace(gateway = types.SimpleNamespace(id = worker_id), workerinput = {})++ def start(worker_id):+ started = node(worker_id)+ configure_node(started)+ return started.workerinput["engine_port_slot"]++ slots = {f"gw{index}": start(f"gw{index}") for index in range(workers)}+ node_down(node(f"gw{crashed}"), None)+ del slots[f"gw{crashed}"]+ replacement = start(f"gw{workers}")+ assert replacement == crashed+ base, size = engine_port_block(replacement, workers)+ for slot in slots.values():+ other, other_size = engine_port_block(slot, workers)+ assert base + size <= other or other + other_size <= base, (slot, base, other)++[email protected]("workers", [2, 4, 16])+def test_a_replacement_without_a_controller_slot_avoids_every_survivor(engine_port_block, workers):+ """`pytest studio -n N` never loads the slot hooks on the controller, so gw<count> falls back to its id."""+ base, size = engine_port_block(workers, workers)+ for index in range(workers):+ other, other_size = engine_port_block(index, workers)+ assert base + size <= other or other + other_size <= base, (index, base, other)