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.

Scope the llama.cpp update test's Popen patch to the installer (#10105)

Daniel Han454a5aba029d
diff --git a/studio/backend/tests/test_llama_cpp_update.py b/studio/backend/tests/test_llama_cpp_update.py
index 50b99d32594..896cd8ac1fd 100644
--- a/studio/backend/tests/test_llama_cpp_update.py
+++ b/studio/backend/tests/test_llama_cpp_update.py
@@ -11,6 +11,8 @@ the apply flow (job lifecycle, installer invocation, post-swap re-read).
from __future__ import annotations
import json
+import os
+import subprocess
import sys
import time
from pathlib import Path
@@ -24,6 +26,7 @@ if str(_BACKEND) not in sys.path:
import utils.llama_cpp_freshness as freshness # noqa: E402
import utils.llama_cpp_update as upd # noqa: E402
+import utils.process_lifetime as process_lifetime # noqa: E402
MARKER = "UNSLOTH_PREBUILT_INFO.json"
@@ -59,6 +62,14 @@ class _FakeInstallerPopen:
pass
+# Bound before any patch, so a delegated spawn reaches the real one.
+_REAL_POPEN = subprocess.Popen
+
+
+def _installer_command(cmd) -> bool:
+ return any("install_llama_prebuilt" in str(part) for part in cmd)
+
+
def _patch_installer_popen(
monkeypatch,
*,
@@ -66,19 +77,33 @@ def _patch_installer_popen(
lines = None,
on_start = None,
captured_kwargs = None,
+ spawned = None,
):
- monkeypatch.setattr(
- upd.subprocess,
- "Popen",
- lambda cmd, **kw: _FakeInstallerPopen(
+ """Replace Popen for the installer only; everything else gets the real one.
+
+ subprocess is shared, so this patch catches every spawn in the process, and
+ _run_llama_phase adopts the installer pid right after starting it: on macOS
+ that identity lookup shells out to `ps`, which would otherwise reach on_start
+ and overwrite the installer argv a caller captured (#8170). Delegating rather
+ than faking keeps that lookup working, since a stand-in owes subprocess.run
+ the whole protocol and _pid_identity swallows whatever it does not get.
+ """
+
+ def _popen(cmd, **kw):
+ if spawned is not None:
+ spawned.append(list(cmd))
+ if not _installer_command(cmd):
+ return _REAL_POPEN(cmd, **kw)
+ return _FakeInstallerPopen(
cmd,
returncode = returncode,
lines = lines,
on_start = on_start,
captured_kwargs = captured_kwargs,
**kw,
- ),
- )
+ )
+
+ monkeypatch.setattr(upd.subprocess, "Popen", _popen)
def _write_install(
@@ -472,6 +497,43 @@ def test_start_update_happy_path(monkeypatch, tmp_path):
assert popen_kwargs["env"]["UNSLOTH_PROGRESS_PERCENT_STEP"] == "5"
+def test_a_second_spawn_during_the_update_does_not_overwrite_the_installer_argv(
+ monkeypatch, tmp_path
+):
+ """The installer is not the only thing the phase spawns.
+
+ _run_llama_phase adopts the installer pid, and on a host with no /proc that
+ identity lookup runs `ps`. Forced here because CI runs this file on Linux
+ only, so the macOS ordering is otherwise never exercised.
+ """
+ install_dir = tmp_path / "llama.cpp"
+ binary = _write_install(install_dir, "b9493")
+ monkeypatch.setattr(upd, "_find_binary", lambda: binary)
+ monkeypatch.setattr(upd, "_installer_script", lambda: tmp_path / "install_llama_prebuilt.py")
+ monkeypatch.setattr(freshness, "_fetch_latest_release_tag", lambda repo, timeout = 5.0: "b9518")
+ monkeypatch.setattr(process_lifetime.sys, "platform", "darwin", raising = False)
+
+ captured: dict = {}
+ spawned: list = []
+
+ def _on_start(cmd):
+ captured["cmd"] = cmd
+ _write_install(install_dir, "b9518")
+
+ _patch_installer_popen(monkeypatch, on_start = _on_start, spawned = spawned)
+
+ job = _run_start_update_to_completion()
+ assert job["state"] == "success", job
+ # Without the ps spawn the ordering this guards cannot happen, so a green
+ # run that never reached it would prove nothing. It has to complete, not
+ # just start: a stand-in missing part of the Popen protocol raises into
+ # _pid_identity's bare except and disables the very lookup being exercised.
+ assert any(cmd and cmd[0] == "ps" for cmd in spawned), spawned
+ assert process_lifetime._pid_identity(os.getpid()), "the ps lookup did not complete"
+ assert "--install-dir" in captured["cmd"], captured["cmd"]
+ assert str(install_dir) in captured["cmd"], captured["cmd"]
+
+
@pytest.mark.parametrize(
"marker_fields, expected_choice",
[