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: pass the llama-server key through the env when Windows cannot open its file (#13133)

Daniel Han57a175692dec
diff --git a/studio/backend/core/inference/llama_cpp.py b/studio/backend/core/inference/llama_cpp.py
index cdcc0958c77..d64d2dea36c 100644
--- a/studio/backend/core/inference/llama_cpp.py
+++ b/studio/backend/core/inference/llama_cpp.py
@@ -7693,6 +7693,27 @@ def _write_direct_stream_key(key: str, previous: "Optional[Path]" = None) -> "Pa
return path
+def _llama_server_key_via_env() -> bool:
+ """True where llama-server cannot open a key file under Studio's auth directory.
+
+ On Windows llama-server re-encodes argv to UTF-8 and opens --api-key-file with a narrow std::ifstream,
+ which reads the path in the ANSI code page (common/arg.cpp), so a non-ASCII profile path never opens (#13123).
+ """
+ from utils.paths.storage_roots import auth_root
+ return sys.platform == "win32" and not str(auth_root()).isascii()
+
+
+def _llama_server_key_launch(
+ key: str, previous: "Optional[Path]" = None
+) -> "tuple[list[str], dict[str, str], Optional[Path]]":
+ """argv and child env that hand ``key`` to llama-server, plus the key file when one was written."""
+ if _llama_server_key_via_env():
+ # Like the 0600 file, a process environment is readable only by this user, and it stays off the command line.
+ return [], {"LLAMA_API_KEY": key}, None
+ path = _write_direct_stream_key(key, previous)
+ return ["--api-key-file", str(path)], {}, path
+
+
def _extra_args_have_tensor_split(
extra_args: Optional[Iterable[str]], env: Optional[Mapping[str, str]]
) -> bool:
@@ -28802,11 +28823,14 @@ class LlamaCppBackend:
"This llama-server build has no --api-key-file; starting it without an API key."
)
_key_on = False
+ _key_env: dict[str, str] = {}
if _key_on:
self._api_key = _secrets.token_urlsafe(32)
- # Through a file, not argv: a command line is readable by every process of this Unix user, and the auth directory is not.
- self._api_key_file = _write_direct_stream_key(self._api_key, self._api_key_file)
- cmd.extend(["--api-key-file", str(self._api_key_file)])
+ # Through a file or the child env, not argv: a command line is readable by every process of this Unix user, and the auth directory is not.
+ _key_argv, _key_env, self._api_key_file = _llama_server_key_launch(
+ self._api_key, self._api_key_file
+ )
+ cmd.extend(_key_argv)
else:
self._api_key = None
@@ -29395,6 +29419,8 @@ class LlamaCppBackend:
"dropped inherited %s: managed by Unsloth Studio",
", ".join(_denied_scrubbed),
)
+ # After the scrub, which drops an inherited LLAMA_API_KEY.
+ env.update(_key_env)
# Record what the child will ACTUALLY run with (env defaults plus
# last-wins argv), not just what Unsloth emitted, so the reload
# hint also catches a user-supplied --mlock / --no-mmap.
diff --git a/studio/backend/core/inference/stt_mtmd_sidecar.py b/studio/backend/core/inference/stt_mtmd_sidecar.py
index a1d7e5deedf..9b56f4ec0ce 100644
--- a/studio/backend/core/inference/stt_mtmd_sidecar.py
+++ b/studio/backend/core/inference/stt_mtmd_sidecar.py
@@ -976,19 +976,20 @@ class MtmdSttSidecar:
from core.inference.llama_cpp import (
LlamaCppBackend,
_llama_server_api_key_enabled,
- _write_direct_stream_key,
+ _llama_server_key_launch,
)
api_key = key_file = None
+ key_env: dict = {}
# A custom build without --api-key-file would exit on the flag; it runs keyless as before.
if _llama_server_api_key_enabled() and LlamaCppBackend.probe_server_capabilities(
binary
).get("supports_api_key_file", True):
# Per-launch key against permissive CORS; read once at startup, so deleted once ready.
api_key = secrets.token_urlsafe(32)
- key_file = _write_direct_stream_key(api_key)
+ key_argv, key_env, key_file = _llama_server_key_launch(api_key)
# Right after the binary: --no-mmproj-offload has to stay last.
- cmd[1:1] = ["--api-key-file", str(key_file)]
+ cmd[1:1] = key_argv
process = subprocess.Popen(
cmd,
# nothing reads these, and an undrained pipe blocks llama-server mid-startup once its logs fill the
@@ -998,7 +999,7 @@ class MtmdSttSidecar:
stdin = subprocess.DEVNULL,
# bundled libs and pip CUDA runtimes on the loader path, secrets scrubbed, as the chat backend spawns
# the same binary
- env = _llama_server_child_env(binary),
+ env = {**_llama_server_child_env(binary), **key_env},
# Die with Unsloth, so a crash never orphans a server on the GPU.
**child_popen_kwargs(),
)
diff --git a/studio/backend/core/rag/embed_llama_server.py b/studio/backend/core/rag/embed_llama_server.py
index b924d96de9a..b6853f3ef63 100644
--- a/studio/backend/core/rag/embed_llama_server.py
+++ b/studio/backend/core/rag/embed_llama_server.py
@@ -949,7 +949,7 @@ class LlamaServerBackend:
from core.inference.llama_cpp import (
LlamaCppBackend,
_llama_server_api_key_enabled,
- _write_direct_stream_key,
+ _llama_server_key_launch,
)
self._api_key = key_file = None
@@ -960,8 +960,9 @@ class LlamaServerBackend:
import secrets
self._api_key = secrets.token_urlsafe(32)
- key_file = _write_direct_stream_key(self._api_key)
- cmd[1:1] = ["--api-key-file", str(key_file)]
+ key_argv, key_env, key_file = _llama_server_key_launch(self._api_key)
+ cmd[1:1] = key_argv
+ env.update(key_env)
self._stdout_lines = []
# One flag at every spawn. No _graceful_shutdown step stops this backend, so an
# encode still resolving or downloading its model as the app quits would
diff --git a/studio/backend/core/systemone/native_worker.py b/studio/backend/core/systemone/native_worker.py
index b8ff36d544b..5d6ea78982b 100644
--- a/studio/backend/core/systemone/native_worker.py
+++ b/studio/backend/core/systemone/native_worker.py
@@ -314,7 +314,7 @@ class NativeClefAgent:
cancelled: Callable[[], bool] | None = None,
clef_answers: bool = True,
):
- from core.inference.llama_cpp import LlamaCppBackend
+ from core.inference.llama_cpp import LlamaCppBackend, _llama_server_key_via_env
from utils.process_lifetime import (
adopt_pid,
child_popen_kwargs,
@@ -352,7 +352,13 @@ class NativeClefAgent:
self._log_path: Path | None = None
if is_process_shutting_down():
raise NativeError("Studio is shutting down; llama.cpp was not started.")
- self._key_file = _write_key(self._key)
+ if _llama_server_key_via_env():
+ key_argv = []
+ env["LLAMA_API_KEY"] = self._key
+ else:
+ self._key_file = _write_key(self._key)
+ # Through a file, not argv: a command line is readable by every process of this user.
+ key_argv = ["--api-key-file", str(self._key_file)]
self.command = [
binary,
"-m",
@@ -364,9 +370,7 @@ class NativeClefAgent:
"127.0.0.1",
"--port",
str(self.port),
- # Through a file, not argv: a command line is readable by every process of this user.
- "--api-key-file",
- str(self._key_file),
+ *key_argv,
"--parallel",
"1",
# A decision reads every token in one ubatch: -ub bounds the longest state served.
diff --git a/studio/backend/tests/test_llama_server_api_key_default.py b/studio/backend/tests/test_llama_server_api_key_default.py
index 28340d753c3..3dfaf58a738 100644
--- a/studio/backend/tests/test_llama_server_api_key_default.py
+++ b/studio/backend/tests/test_llama_server_api_key_default.py
@@ -64,6 +64,39 @@ def test_stale_key_files_are_swept_on_a_new_launch(monkeypatch, tmp_path):
assert recent.read_text() == "live" and current.read_text() == "new"
+ "platform,dirname,via_env",
+ [
+ ("win32", "Galambos András", True),
+ ("win32", "张伟", True),
+ ("win32", "Galambos Andras", False),
+ ("linux", "Galambos András", False),
+ ("darwin", "张伟", False),
+ ],
+)
+def test_key_goes_through_env_only_where_windows_cannot_open_the_file(
+ monkeypatch, tmp_path, platform, dirname, via_env
+):
+ import utils.paths.storage_roots as roots
+
+ auth = tmp_path / dirname / "auth"
+ monkeypatch.setattr(roots, "auth_root", lambda: auth)
+ monkeypatch.setattr(llama_cpp.sys, "platform", platform)
+ argv, env, path = llama_cpp._llama_server_key_launch("k")
+ if via_env:
+ # #13123: Windows llama-server opens --api-key-file in the ANSI code page.
+ assert (argv, env, path) == ([], {"LLAMA_API_KEY": "k"}, None)
+ assert not auth.exists()
+ else:
+ assert argv == ["--api-key-file", str(path)] and env == {}
+ assert path.parent == auth and path.read_text() == "k"
+
+
+def test_launch_sets_the_env_key_after_the_inherited_one_is_scrubbed():
+ src = Path(llama_cpp.__file__).read_text(encoding = "utf-8")
+ assert src.index("_denied_scrubbed = scrub_denied_env(env)") < src.index("env.update(_key_env)")
+
+
def test_tool_guard_still_refuses_the_per_launch_key_file():
from core.inference import tools