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: reuse the cached TLS context for llama-server health probes (#15792)

Daniel Han4e63c8fcd64f
diff --git a/studio/backend/core/inference/llama_cpp.py b/studio/backend/core/inference/llama_cpp.py
index ae9b94535c1..3736f053a3c 100644
--- a/studio/backend/core/inference/llama_cpp.py
+++ b/studio/backend/core/inference/llama_cpp.py
@@ -35220,7 +35220,8 @@ class LlamaCppBackend:
try:
# trust_env=False: an HTTP proxy can 503 for 127.0.0.1 and hang the load.
- resp = httpx.get(url, timeout = 2.0, trust_env = False)
+ # Shared context: httpx otherwise loads the CA bundle on every probe.
+ resp = httpx.get(url, timeout = 2.0, trust_env = False, verify = _local_ssl_context())
if resp.status_code == 200:
# A cancel during the 2s probe must not publish the dropped model.
if cancelled is not None and cancelled():
diff --git a/studio/backend/tests/test_llama_cpp_health_probe_tls.py b/studio/backend/tests/test_llama_cpp_health_probe_tls.py
new file mode 100644
index 00000000000..9844911eba0
--- /dev/null
+++ b/studio/backend/tests/test_llama_cpp_health_probe_tls.py
@@ -0,0 +1,91 @@
+# SPDX-License-Identifier: AGPL-3.0-only
+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
+
+"""Health probes reuse one TLS context instead of loading the CA bundle per probe."""
+
+from __future__ import annotations
+
+import http.server
+import ssl
+import sys
+import threading
+from pathlib import Path
+
+import pytest
+
+_BACKEND_DIR = str(Path(__file__).resolve().parent.parent)
+if _BACKEND_DIR not in sys.path:
+ sys.path.insert(0, _BACKEND_DIR)
+
+httpx = pytest.importorskip("httpx")
+if not hasattr(httpx, "Client"):
+ pytest.skip(
+ reason = "test_compute_buffer.py and others replace sys.modules['httpx'] with a stub; this test needs the real client",
+ allow_module_level = True,
+ )
+
+from core.inference.llama_cpp import LlamaCppBackend # noqa: E402
+
+
+class _Server(http.server.ThreadingHTTPServer):
+ daemon_threads = True
+ requests = 0
+ connections = 0
+ cookies_seen: list = []
+
+
+class _Handler(http.server.BaseHTTPRequestHandler):
+ protocol_version = "HTTP/1.1"
+
+ def setup(self):
+ self.server.connections += 1
+ super().setup()
+
+ def do_GET(self):
+ srv = self.server
+ srv.requests += 1
+ srv.cookies_seen.append(self.headers.get("Cookie"))
+ ok = srv.requests > 4
+ body = b"ok" if ok else b"loading"
+ self.send_response(200 if ok else 503)
+ self.send_header("Set-Cookie", f"probe={srv.requests}")
+ self.send_header("Content-Length", str(len(body)))
+ self.end_headers()
+ self.wfile.write(body)
+
+ def log_message(self, *a):
+ pass
+
+
+def server():
+ srv = _Server(("127.0.0.1", 0), _Handler)
+ srv.cookies_seen = []
+ threading.Thread(target = srv.serve_forever, daemon = True).start()
+ yield srv
+ srv.shutdown()
+ srv.server_close()
+
+
+def test_probes_share_one_tls_context(server, monkeypatch):
+ built = []
+ real_new = ssl.SSLContext.__new__
+
+ def counting_new(cls, *a, **kw):
+ built.append(cls)
+ return real_new(cls, *a, **kw)
+
+ monkeypatch.setattr(ssl.SSLContext, "__new__", counting_new)
+ monkeypatch.setattr(LlamaCppBackend, "_health_listener_is_ours", lambda self, p, port: True)
+ b = LlamaCppBackend()
+ b._port = server.server_address[1]
+ b._process = type("_Proc", (), {"returncode": None, "poll": lambda self: None})()
+ b._stdout_thread = None
+ b._stdout_lines = []
+
+ assert b._wait_for_health(timeout = 10.0, interval = 0.01) is True
+ assert server.requests == 5
+ assert len(built) <= 1, f"{len(built)} TLS contexts for {server.requests} probes"
+ # Unchanged from a bare httpx.get: a fresh connection and cookie jar per probe.
+ assert server.connections == 5
+ assert server.cookies_seen == [None] * 5
diff --git a/studio/backend/tests/test_offline_gguf_cache_fallback.py b/studio/backend/tests/test_offline_gguf_cache_fallback.py
index 8ea635558b6..19ecd491118 100644
--- a/studio/backend/tests/test_offline_gguf_cache_fallback.py
+++ b/studio/backend/tests/test_offline_gguf_cache_fallback.py
@@ -2563,6 +2563,7 @@ class TestWaitForHealthRetriesOnReadError:
url,
timeout = None,
trust_env = None,
+ verify = None,
):
calls["n"] += 1
if calls["n"] == 1: