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.

Tests: stop the live no-torch server test from uninstalling torch from the Studio venv (#10955)

oobaboogaf1ff13e94ab1
diff --git a/pyproject.toml b/pyproject.toml
index b2ab1949a74..f4b87cfd06d 100644
--- a/pyproject.toml
+++ b/pyproject.toml
@@ -1676,4 +1676,5 @@ markers = [
# `-m 'gpu or slow'`, and `pytest -m gpu tests/` gets them back locally.
# A run that names one of these files directly will report "no tests ran"
# until you pass `-m gpu` -- that is the cost of the default being fast.
-addopts = "-m 'not gpu and not slow'"
+# Live Studio tests are opt-in: `pytest -m server`.
+addopts = "-m 'not gpu and not slow and not server'"
diff --git a/tests/python/test_e2e_no_torch_sandbox.py b/tests/python/test_e2e_no_torch_sandbox.py
index 41c4e71bc8a..d2850e718ee 100644
--- a/tests/python/test_e2e_no_torch_sandbox.py
+++ b/tests/python/test_e2e_no_torch_sandbox.py
@@ -925,6 +925,39 @@ def _server_port() -> int:
server = pytest.mark.server
+# from_name as well as discover: only 3.11+ routes named lookups through discover, and
+# Distribution.name is absent on 3.9.
+_HIDE_TORCH_SITECUSTOMIZE = textwrap.dedent(
+ """
+ import importlib.metadata as _metadata
+ import sys
+
+ _HIDDEN = ("torch", "torchvision", "torchaudio")
+ for _name in _HIDDEN:
+ sys.modules[_name] = None
+
+ _discover = _metadata.Distribution.discover.__func__
+ _from_name = _metadata.Distribution.from_name.__func__
+
+ def _hidden(dist):
+ try:
+ return (dist.metadata["Name"] or "").lower() in _HIDDEN
+ except Exception:
+ return False
+
+ def _discover_without_torch(cls, **kwargs):
+ return (d for d in _discover(cls, **kwargs) if not _hidden(d))
+
+ def _from_name_without_torch(cls, name):
+ if name.lower() in _HIDDEN:
+ raise _metadata.PackageNotFoundError(name)
+ return _from_name(cls, name)
+
+ _metadata.Distribution.discover = classmethod(_discover_without_torch)
+ _metadata.Distribution.from_name = classmethod(_from_name_without_torch)
+ """
+)
+
@server
class TestLiveServerStartup:
@@ -937,7 +970,7 @@ class TestLiveServerStartup:
pytest.skip("Unsloth venv not found at ~/.unsloth/studio/unsloth_studio")
@pytest.fixture(scope = "class")
- def server_process(self):
+ def server_process(self, tmp_path_factory):
"""Start the studio backend server without torch, yield (proc, port), then stop."""
py = _studio_venv_python()
if py is None:
@@ -946,31 +979,31 @@ class TestLiveServerStartup:
port = _server_port()
backend_dir = BACKEND_DIR
- check = subprocess.run(
- [str(py), "-c", "import torch; print(torch.__version__)"],
+ # Preserve packages in the user's Studio venv.
+ hide_dir = tmp_path_factory.mktemp("hide_torch")
+ (hide_dir / "sitecustomize.py").write_text(_HIDE_TORCH_SITECUSTOMIZE, encoding = "utf-8")
+
+ env = os.environ.copy()
+ env["PYTHONPATH"] = os.pathsep.join([str(hide_dir), str(backend_dir)])
+
+ # A sitecustomize that never loaded otherwise reads as a puzzling chat_only failure.
+ hidden = subprocess.run(
+ [
+ str(py),
+ "-c",
+ "import importlib.metadata as m, importlib.util as u, sys\n"
+ "try: m.version('torch'); sys.exit('metadata visible')\n"
+ "except m.PackageNotFoundError: pass\n"
+ "assert u.find_spec('torch') is None, 'spec visible'",
+ ],
+ env = env,
capture_output = True,
+ timeout = 60,
)
- torch_was_installed = check.returncode == 0
- torch_version = check.stdout.decode().strip() if torch_was_installed else None
-
- if torch_was_installed:
- subprocess.run(
- [
- str(py),
- "-m",
- "pip",
- "uninstall",
- "-y",
- "torch",
- "torchvision",
- "torchaudio",
- ],
- capture_output = True,
- timeout = 120,
+ if hidden.returncode != 0:
+ pytest.fail(
+ f"torch is still visible to the server process: {hidden.stderr.decode(errors = 'replace')}"
)
-
- env = os.environ.copy()
- env["PYTHONPATH"] = str(backend_dir)
proc = subprocess.Popen(
[str(py), str(backend_dir / "run.py"), "--port", str(port)],
env = env,
@@ -995,20 +1028,6 @@ class TestLiveServerStartup:
if not ready:
stdout, stderr = proc.communicate(timeout = 5)
- if torch_was_installed and torch_version:
- subprocess.run(
- [
- str(py),
- "-m",
- "pip",
- "install",
- f"torch=={torch_version}",
- "torchvision",
- "torchaudio",
- ],
- capture_output = True,
- timeout = 300,
- )
server_output = stdout.decode(errors = "replace") + stderr.decode(errors = "replace")
pytest.skip(f"Server failed to start within 30 seconds. Output:\n{server_output}")
@@ -1021,21 +1040,6 @@ class TestLiveServerStartup:
proc.kill()
proc.wait(timeout = 5)
- if torch_was_installed and torch_version:
- subprocess.run(
- [
- str(py),
- "-m",
- "pip",
- "install",
- f"torch=={torch_version}",
- "torchvision",
- "torchaudio",
- ],
- capture_output = True,
- timeout = 300,
- )
-
def test_server_starts_without_torch(self, server_process):
"""Server responds to /api/health with chat_only: true."""
import json