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: only trust llama-server's /health when our own child holds the port (#14706)
Daniel Han32081f4efb5d
diff --git a/studio/backend/core/inference/llama_cpp.py b/studio/backend/core/inference/llama_cpp.pyindex 8e808bfdd08..a07f70a22a1 100644--- a/studio/backend/core/inference/llama_cpp.py+++ b/studio/backend/core/inference/llama_cpp.py@@ -998,6 +998,10 @@ _LOG_LEVEL_TOKEN_RE = re.compile(rf"^{_ANSI}(?:\d+\.\d+\.\d+\.\d+{_ANSI} )?{_ANS_STDOUT_TRIM_AT = 20000_STDOUT_TAIL_KEEP = 5000_STDOUT_HEAD_MAX = 5000+# Bind addresses whose listener accepts the health probe's 127.0.0.1 connection.+_LOOPBACK_REACHABLE_BIND_IPS = frozenset(+ {"127.0.0.1", "0.0.0.0", "::", "::ffff:127.0.0.1", "::ffff:0.0.0.0"}+)def _llama_line_is_warning_or_error(line: str, line_lower: str) -> bool:@@ -19766,6 +19770,83 @@ class LlamaCppBackend:s.bind(("127.0.0.1", 0))return s.getsockname()[1]+ def _health_listener_is_ours(self, process) -> bool:+ """False when the port answering /health is demonstrably not held by our child's tree.++ _find_free_port releases the port before llama-server binds it, so another local user could+ answer /health and receive every prompt plus the per-launch key. Their socket shows no pid+ on Linux/macOS, so our own (readable) tree not listening counts as foreign. Unreadable+ ownership keeps today's behaviour."""+ port = getattr(self, "_port", None)+ pid = getattr(process, "pid", None)+ if not isinstance(pid, int) or not isinstance(port, int):+ return True+ # A child that already exited cannot be the server that answered.+ poll = getattr(process, "poll", None)+ if callable(poll) and poll() is not None:+ return False+ try:+ import psutil+ from core.inference.sd_cpp_server import _has_ancestor+ except Exception: # noqa: BLE001 -- optional dependency+ return True+ try:+ root = psutil.Process(pid)+ tree = [root, *root.children(recursive = True)]+ except psutil.NoSuchProcess:+ return False+ except Exception: # noqa: BLE001 -- access denied / platform quirk+ return True++ def _listens(conn) -> bool:+ laddr = getattr(conn, "laddr", None)+ return (+ bool(laddr)+ and getattr(laddr, "port", None) == port+ and getattr(laddr, "ip", None) in _LOOPBACK_REACHABLE_BIND_IPS+ and conn.status == psutil.CONN_LISTEN+ )++ tree_readable = True+ for member in tree:+ try:+ read = getattr(member, "net_connections", None) or member.connections+ if any(_listens(c) for c in read(kind = "inet")):+ return True+ except psutil.NoSuchProcess:+ continue+ except Exception: # noqa: BLE001 -- access denied / platform quirk+ tree_readable = False+ tree_pids = {m.pid for m in tree}+ foreign = None+ try:+ # Scan every listener: a foreign one listed before ours must not refuse our start.+ for conn in psutil.net_connections(kind = "inet"):+ if not _listens(conn) or conn.pid is None:+ continue+ if conn.pid in tree_pids or _has_ancestor(conn.pid, pid):+ return True+ foreign = conn.pid+ except Exception as exc: # noqa: BLE001 -- permissions / platform quirks+ logger.debug("llama-server port ownership check unavailable: %s", exc)+ if foreign is not None:+ logger.error(+ "llama-server port %s is held by pid %s, not our child %s; refusing to use it",+ port,+ foreign,+ pid,+ )+ return False+ if tree_readable:+ logger.error(+ "llama-server port %s answered /health but our child %s is not listening on it; "+ "refusing to use it",+ port,+ pid,+ )+ return False+ return True+# ── Stdout drain (prevents pipe deadlock on Windows) ─────────def _drain_stdout(self):@@ -38780,6 +38861,8 @@ class LlamaCppBackend:logger.info("llama-server became healthy while the app was shutting down")self._health_wait_cancelled = Truereturn False+ if not self._health_listener_is_ours(process):+ return Falsereturn Trueexcept (httpx.ConnectError,diff --git a/studio/backend/core/inference/sd_cpp_server.py b/studio/backend/core/inference/sd_cpp_server.pyindex 959d5841ac1..258ad7d97a4 100644--- a/studio/backend/core/inference/sd_cpp_server.py+++ b/studio/backend/core/inference/sd_cpp_server.py@@ -349,7 +349,9 @@ class SdCppServer:logger.error("sd-server exited early during load (code %s)", code)return Falsetry:- if self._client.get(url, timeout = 2.0).status_code == 200 and self._port_is_ours():+ # Connection: close keeps a probe answered by a squatter out of the generation pool.+ resp = self._client.get(url, headers = {"Connection": "close"}, timeout = 2.0)+ if resp.status_code == 200 and self._port_is_ours():return Trueexcept (*_TRANSPORT_ERRORS, httpx.TimeoutException):pass@@ -373,10 +375,41 @@ class SdCppServer:proc = self._processif proc is None or proc.pid is None:return True+ # A child that already exited cannot be the server that answered.+ poll = getattr(proc, "poll", None)+ if callable(poll) and poll() is not None:+ return Falsetry:import psutilexcept Exception: # noqa: BLE001 -- optional dependencyreturn True+ # Another user's socket shows no pid on Linux/macOS: our readable tree not listening = foreign.+ tree_says_foreign = False+ try:+ if not isinstance(proc, subprocess.Popen):+ raise TypeError("not a child we spawned")+ root = psutil.Process(proc.pid)+ tree = [root, *root.children(recursive = True)]+ readable = True+ for member in tree:+ try:+ read = getattr(member, "net_connections", None) or member.connections+ for conn in read(kind = "inet"):+ laddr = getattr(conn, "laddr", None)+ if (+ laddr+ and getattr(laddr, "port", None) == self.port+ and conn.status == psutil.CONN_LISTEN+ ):+ return True+ except psutil.NoSuchProcess:+ continue+ except Exception: # noqa: BLE001 -- access denied / platform quirk+ readable = False+ tree_says_foreign = readable+ except Exception as exc: # noqa: BLE001 -- platform quirk: fall back below+ if isinstance(exc, getattr(psutil, "NoSuchProcess", ())):+ return Falsetry:for conn in psutil.net_connections(kind = "inet"):laddr = getattr(conn, "laddr", None)@@ -399,6 +432,14 @@ class SdCppServer:return Falseexcept Exception as exc: # noqa: BLE001 -- permissions / platform quirkslogger.debug("sd-server port ownership check unavailable: %s", exc)+ if tree_says_foreign:+ logger.error(+ "sd-server readiness port %s answered but our child %s is not listening on it; "+ "refusing to use it",+ self.port,+ proc.pid,+ )+ return Falsereturn Truedef _drain_stdout(self, proc: subprocess.Popen) -> None:diff --git a/studio/backend/tests/test_llama_cpp_wait_for_health.py b/studio/backend/tests/test_llama_cpp_wait_for_health.pyindex 4c5a6ccb299..040693a5f2e 100644--- a/studio/backend/tests/test_llama_cpp_wait_for_health.py+++ b/studio/backend/tests/test_llama_cpp_wait_for_health.py@@ -1537,6 +1537,8 @@ class TestHealthWaitMeasuresStalls:timeout = 0.6,):monkeypatch.setattr(LlamaCppBackend, "_STARTUP_PROGRESS_SAMPLE_S", 0.1)+ # /health is mocked and the worker never listens, so the port-owner check doesn't apply.+ monkeypatch.setattr(LlamaCppBackend, "_health_listener_is_ours", lambda self, process: True)b = _make_backend()b._process = subprocess.Popen([sys.executable, "-c", *argv])started = time.monotonic()diff --git a/studio/backend/tests/test_llama_server_port_owner.py b/studio/backend/tests/test_llama_server_port_owner.pynew file mode 100644index 00000000000..c66f5dc0dbf--- /dev/null+++ b/studio/backend/tests/test_llama_server_port_owner.py@@ -0,0 +1,200 @@+# SPDX-License-Identifier: AGPL-3.0-only+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0++"""A /health 200 only counts when our llama-server child holds the port.++_find_free_port releases its socket before llama-server binds, so another local user can take+the port and answer /health; chat traffic and the per-launch bearer would then go to them.+"""++from __future__ import annotations++import http.server+import subprocess+import sys+import threading+import types as _types+import urllib.error+import urllib.request+from pathlib import Path+from types import SimpleNamespace++import pytest++_BACKEND_DIR = str(Path(__file__).resolve().parent.parent)+if _BACKEND_DIR not in sys.path:+ sys.path.insert(0, _BACKEND_DIR)++_loggers_stub = _types.ModuleType("loggers")+_loggers_stub.get_logger = lambda name: __import__("logging").getLogger(name)+sys.modules.setdefault("loggers", _loggers_stub)+sys.modules.setdefault("structlog", _types.ModuleType("structlog"))++psutil = pytest.importorskip("psutil")++import core.inference.llama_cpp as llama_cpp # noqa: E402+from core.inference.llama_cpp import LlamaCppBackend # noqa: E402++_CHILD_SERVER = r"""+import http.server, sys+class H(http.server.BaseHTTPRequestHandler):+ def do_GET(self):+ self.send_response(200); self.end_headers(); self.wfile.write(b'{"status":"ok"}')+ def log_message(self, *a): pass+s = http.server.HTTPServer(("127.0.0.1", 0), H)+print(s.server_address[1], flush=True)+s.serve_forever()+"""+++class _Health(http.server.BaseHTTPRequestHandler):+ def do_GET(self):+ self.send_response(200)+ self.end_headers()+ self.wfile.write(b'{"status":"ok"}')++ def log_message(self, *a):+ pass+++def _urllib_get(+ url,+ timeout = 2.0,+ **_kw,+):+ try:+ with urllib.request.urlopen(url, timeout = timeout) as r:+ return SimpleNamespace(status_code = r.status)+ except urllib.error.HTTPError as e:+ return SimpleNamespace(status_code = e.code)+ except OSError as e:+ raise llama_cpp.httpx.ConnectError(str(e))+++def probe(monkeypatch):+ monkeypatch.setattr(llama_cpp.httpx, "get", _urllib_get)+++def foreign_listener():+ srv = http.server.HTTPServer(("127.0.0.1", 0), _Health)+ t = threading.Thread(target = srv.serve_forever, daemon = True)+ t.start()+ yield srv.server_address[1]+ srv.shutdown()+ srv.server_close()+++def idle_child():+ p = subprocess.Popen([sys.executable, "-c", "import time; time.sleep(60)"])+ yield p+ p.kill()+ p.wait()+++def _backend(port: int, process) -> LlamaCppBackend:+ b = LlamaCppBackend.__new__(LlamaCppBackend)+ b._port = port+ b._stdout_thread = None+ b._stdout_lines = []+ b._process = process+ return b+++def test_foreign_listener_on_our_port_is_not_healthy(probe, foreign_listener, idle_child):+ b = _backend(foreign_listener, idle_child)+ assert b._wait_for_health(timeout = 3.0, interval = 0.05) is False+++def test_hidden_owner_with_our_child_not_listening_is_not_healthy(+ probe, foreign_listener, idle_child, monkeypatch+):+ real = psutil.net_connections++ def hidden(kind = "inet"):+ return [c._replace(pid = None) for c in real(kind = kind)]++ monkeypatch.setattr(psutil, "net_connections", hidden)+ b = _backend(foreign_listener, idle_child)+ assert b._wait_for_health(timeout = 3.0, interval = 0.05) is False+++def test_our_own_child_listening_is_healthy(probe):+ p = subprocess.Popen([sys.executable, "-c", _CHILD_SERVER], stdout = subprocess.PIPE, text = True)+ try:+ port = int(p.stdout.readline())+ b = _backend(port, p)+ assert b._wait_for_health(timeout = 10.0, interval = 0.05) is True+ finally:+ p.kill()+ p.wait()+++def test_unknowable_ownership_keeps_todays_behaviour(+ probe, foreign_listener, idle_child, monkeypatch+):+ def denied(*_a, **_kw):+ raise psutil.AccessDenied()++ monkeypatch.setattr(psutil.Process, "net_connections", denied, raising = False)+ monkeypatch.setattr(psutil.Process, "connections", denied, raising = False)+ monkeypatch.setattr(psutil, "net_connections", denied)+ b = _backend(foreign_listener, idle_child)+ assert b._wait_for_health(timeout = 3.0, interval = 0.05) is True+++def test_a_descendant_holding_the_port_is_ours(probe):+ # Wrapped launches (e.g. the diffusion shim) listen from a grandchild, not the direct child.+ launcher = (+ "import subprocess, sys\n"+ "p = subprocess.Popen([sys.executable, '-c', sys.argv[1]], stdout = sys.stdout)\n"+ "p.wait()\n"+ )+ p = subprocess.Popen(+ [sys.executable, "-c", launcher, _CHILD_SERVER], stdout = subprocess.PIPE, text = True+ )+ try:+ port = int(p.stdout.readline())+ b = _backend(port, p)+ assert b._wait_for_health(timeout = 10.0, interval = 0.05) is True+ finally:+ for child in psutil.Process(p.pid).children(recursive = True):+ child.kill()+ p.kill()+ p.wait()+++def test_an_exited_child_is_never_the_server_that_answered(foreign_listener):+ # A child that could not bind (the port was taken) exits; the 200 then came from the squatter.+ p = subprocess.Popen([sys.executable, "-c", "pass"])+ p.wait()+ b = _backend(foreign_listener, p)+ assert b._health_listener_is_ours(p) is False+++def test_a_foreign_listener_listed_before_ours_does_not_refuse_us(monkeypatch):+ # Own tree unreadable, so the system-wide scan decides; its order must not matter.+ p = subprocess.Popen([sys.executable, "-c", _CHILD_SERVER], stdout = subprocess.PIPE, text = True)+ try:+ port = int(p.stdout.readline())++ def denied(*_a, **_kw):+ raise psutil.AccessDenied()++ def conn(pid, ip):+ return SimpleNamespace(+ laddr = SimpleNamespace(ip = ip, port = port), status = psutil.CONN_LISTEN, pid = pid+ )++ monkeypatch.setattr(psutil.Process, "net_connections", denied, raising = False)+ monkeypatch.setattr(psutil.Process, "connections", denied, raising = False)+ monkeypatch.setattr(+ psutil, "net_connections", lambda kind = "inet": [conn(1, "::"), conn(p.pid, "127.0.0.1")]+ )+ b = _backend(port, p)+ assert b._health_listener_is_ours(p) is True+ finally:+ p.kill()+ p.wait()diff --git a/studio/backend/tests/test_sd_cpp_server.py b/studio/backend/tests/test_sd_cpp_server.pyindex 27d3613b68b..01a56a4aaf6 100644--- a/studio/backend/tests/test_sd_cpp_server.py+++ b/studio/backend/tests/test_sd_cpp_server.py@@ -107,15 +107,18 @@ class _FakeClient:self._get = get or (lambda url: _Resp(200, {}))self._post = post or (lambda url, json: _Resp(202, {"id": "job1"}))self.get_urls = []+ self.get_headers = []self.post_calls = []self.closed = Falsedef get(self,url,+ headers = None,timeout = None,):self.get_urls.append(url)+ self.get_headers.append(headers)return self._get(url)def post(@@ -154,11 +157,12 @@ def test_start_becomes_ready_when_capabilities_200(patched, caplog):caplog.set_level("INFO", logger = srv.__name__)popen = _FakePopen(lines = ["loading model", "listening on: http://127.0.0.1:1"])patched.setattr(srv.subprocess, "Popen", lambda *a, **k: popen)- s = _server_with(- popen, _FakeClient(get = lambda url: _Resp(200, {"model": {"path": "/m/z.gguf"}}))- )+ client = _FakeClient(get = lambda url: _Resp(200, {"model": {"path": "/m/z.gguf"}}))+ s = _server_with(popen, client)s.start(_FILES, startup_timeout = 5.0)assert s.is_alive() is True+ # Readiness probes must not leave a pooled connection for generation to reuse.+ assert client.get_headers and all(h == {"Connection": "close"} for h in client.get_headers)assert s.port is not Nonemessages = [record.getMessage() for record in caplog.records if record.name == srv.__name__]start = next(message for message in messages if message.startswith("starting sd-server:"))@@ -529,3 +533,52 @@ def test_readiness_check_is_best_effort(patched):),)assert s._port_is_ours() is True+++def test_readiness_refuses_a_hidden_owner_when_our_child_is_not_listening():+ import http.server+ import subprocess+ import sys+ import threading++ psutil = pytest.importorskip("psutil")++ class _Ok(http.server.BaseHTTPRequestHandler):+ def do_GET(self):+ self.send_response(200)+ self.end_headers()++ def log_message(self, *a):+ pass++ squatter = http.server.HTTPServer(("127.0.0.1", 0), _Ok)+ threading.Thread(target = squatter.serve_forever, daemon = True).start()+ child = subprocess.Popen([sys.executable, "-c", "import time; time.sleep(30)"])+ try:+ s = srv.SdCppServer.__new__(srv.SdCppServer)+ s._process = child+ s.port = squatter.server_address[1]+ real = psutil.net_connections+ psutil.net_connections = lambda kind = "inet": [c._replace(pid = None) for c in real(kind = kind)]+ try:+ assert s._port_is_ours() is False+ finally:+ psutil.net_connections = real+ finally:+ child.kill()+ child.wait()+ squatter.shutdown()+ squatter.server_close()+++def test_readiness_refuses_a_child_that_already_exited():+ # The child died (e.g. it could not bind) while a squatter answered the probe.+ import subprocess+ import sys++ child = subprocess.Popen([sys.executable, "-c", "pass"])+ child.wait()+ s = srv.SdCppServer.__new__(srv.SdCppServer)+ s._process = child+ s.port = 1234+ assert s._port_is_ours() is False