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: search for llama-server once per Decision API settings answer (#16617)

Daniel Hana6c7e49a541c
diff --git a/studio/backend/core/systemone/native_worker.py b/studio/backend/core/systemone/native_worker.py
index efea4be23a9..c9a36377016 100644
--- a/studio/backend/core/systemone/native_worker.py
+++ b/studio/backend/core/systemone/native_worker.py
@@ -16,7 +16,9 @@ import subprocess
import sys
import threading
import time
-from collections.abc import Mapping
+from collections.abc import Iterator, Mapping
+from contextlib import contextmanager
+from contextvars import ContextVar
from pathlib import Path
from typing import Any, Callable
@@ -78,10 +80,31 @@ class NativeContextOverflow(NativeInputError):
)
+# A found binary, kept for one settings answer, which would otherwise search for it once per catalog entry.
+_FOUND_BINARY: ContextVar = ContextVar("systemone_found_binary", default = None)
+
+
+@contextmanager
+def reuse_found_binary() -> Iterator[None]:
+ token = _FOUND_BINARY.set([])
+ try:
+ yield
+ finally:
+ _FOUND_BINARY.reset(token)
+
+
def resolve_binary() -> str | None:
+ found = _FOUND_BINARY.get()
+ if found:
+ return found[0]
from core.inference.llama_cpp import LlamaCppBackend
+
binary = LlamaCppBackend._find_llama_server_binary()
- return (LlamaCppBackend._exec_path_for_launch(binary) or binary) if binary else None
+ binary = (LlamaCppBackend._exec_path_for_launch(binary) or binary) if binary else None
+ # Only a hit: a miss may log why, and each lookup still logs it.
+ if found is not None and binary is not None:
+ found.append(binary)
+ return binary
def request_gap(questions: Mapping[str, Any]) -> str | None:
diff --git a/studio/backend/routes/settings.py b/studio/backend/routes/settings.py
index 91cba6ed799..e74042ffde6 100644
--- a/studio/backend/routes/settings.py
+++ b/studio/backend/routes/settings.py
@@ -1736,6 +1736,12 @@ def _clef_availability(checkpoint, reason: Optional[str]) -> dict:
def _systemone_response(request: Request) -> SystemOneSettingsResponse:
+ from core.systemone.native_worker import reuse_found_binary
+ with reuse_found_binary():
+ return _systemone_answer(request)
+
+
+def _systemone_answer(request: Request) -> SystemOneSettingsResponse:
from pathlib import Path
from core.systemone import catalog, laya_runtime
diff --git a/studio/backend/tests/test_systemone_found_binary.py b/studio/backend/tests/test_systemone_found_binary.py
new file mode 100644
index 00000000000..4ea124d3d36
--- /dev/null
+++ b/studio/backend/tests/test_systemone_found_binary.py
@@ -0,0 +1,86 @@
+# SPDX-License-Identifier: AGPL-3.0-only
+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
+
+from types import SimpleNamespace
+
+import pytest
+
+from core.inference.llama_cpp import LlamaCppBackend
+from core.systemone import native_worker
+
+
+def searches(monkeypatch):
+ calls = []
+ answer = {"binary": "/opt/llama.cpp/llama-server"}
+
+ def find(**kwargs):
+ calls.append(kwargs)
+ return answer["binary"]
+
+ monkeypatch.setattr(LlamaCppBackend, "_find_llama_server_binary", staticmethod(find))
+ monkeypatch.setattr(LlamaCppBackend, "_exec_path_for_launch", staticmethod(lambda b: b))
+ return SimpleNamespace(calls = calls, answer = answer)
+
+
+def test_every_lookup_searches_outside_a_settings_answer(searches):
+ assert [native_worker.resolve_binary() for _ in range(3)] == ["/opt/llama.cpp/llama-server"] * 3
+ assert len(searches.calls) == 3
+
+
+def test_a_found_binary_is_searched_for_once_per_answer(searches):
+ with native_worker.reuse_found_binary():
+ assert [native_worker.resolve_binary() for _ in range(5)] == [
+ "/opt/llama.cpp/llama-server"
+ ] * 5
+ assert len(searches.calls) == 1
+ searches.answer["binary"] = "/new/llama-server"
+ with native_worker.reuse_found_binary():
+ assert native_worker.resolve_binary() == "/new/llama-server"
+ assert len(searches.calls) == 2
+
+
+def test_a_miss_searches_every_time(searches):
+ searches.answer["binary"] = None
+ with native_worker.reuse_found_binary():
+ assert [native_worker.resolve_binary() for _ in range(3)] == [None] * 3
+ assert len(searches.calls) == 3
+
+
+def test_the_scope_ends_with_the_answer_even_on_error(searches):
+ with pytest.raises(RuntimeError):
+ with native_worker.reuse_found_binary():
+ native_worker.resolve_binary()
+ raise RuntimeError("boom")
+ native_worker.resolve_binary()
+ native_worker.resolve_binary()
+ assert len(searches.calls) == 3
+ assert native_worker._FOUND_BINARY.get() is None
+
+
+def test_nested_scopes_and_threads_search_on_their_own(searches):
+ import threading
+ with native_worker.reuse_found_binary():
+ native_worker.resolve_binary()
+ with native_worker.reuse_found_binary():
+ native_worker.resolve_binary()
+ native_worker.resolve_binary()
+ native_worker.resolve_binary()
+ thread = threading.Thread(target = native_worker.resolve_binary)
+ thread.start()
+ thread.join()
+ assert len(searches.calls) == 3
+
+
+def test_settings_answer_searches_once_and_is_unchanged(searches, monkeypatch):
+ from routes import settings
+
+ request = SimpleNamespace(
+ app = SimpleNamespace(state = SimpleNamespace(server_port = 8888)),
+ scope = {"server": ("127.0.0.1", 8888)},
+ )
+ answer = settings._systemone_response(request).model_dump_json()
+ assert len(searches.calls) == 1
+ searches.calls.clear()
+ assert settings._systemone_answer(request).model_dump_json() == answer
+ assert len(searches.calls) > 1