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.

Refuse a compiler cache path shlex cannot parse, not only one with a space (#11414)

Daniel Hanedadaf2e01e7
diff --git a/studio/backend/core/inference/diffusion_compile_cache.py b/studio/backend/core/inference/diffusion_compile_cache.py
index b10bcae615b..56d25624c1c 100644
--- a/studio/backend/core/inference/diffusion_compile_cache.py
+++ b/studio/backend/core/inference/diffusion_compile_cache.py
@@ -145,6 +145,35 @@ def _portable_mode() -> bool:
return False
+def _toolchain_path_unparseable(value: str) -> bool:
+ """storage_roots' test, imported per call like _default_root's.
+
+ The fallback repeats the whole rule rather than narrowing to whitespace, or one path would
+ be refused or accepted depending only on whether studio/backend was on sys.path.
+ test_the_import_fallback_matches_the_resolver sweeps both over every printable character.
+ """
+ try:
+ from utils.paths.storage_roots import toolchain_path_unparseable
+ except ImportError:
+ return (
+ any(ch.isspace() for ch in value)
+ or "'" in value
+ or '"' in value
+ or (os.name != "nt" and "\\" in value)
+ )
+ return toolchain_path_unparseable(value)
+
+
+def _parseable_cache_fallback(key: str, intended: str) -> str | None:
+ """storage_roots' ready-to-publish fallback, or None when this module is reached without
+ studio/backend on sys.path, where there is no safe directory to offer."""
+ try:
+ from utils.paths.storage_roots import parseable_cache_fallback
+ except ImportError:
+ return None
+ return parseable_cache_fallback(key, intended)
+
+
def _default_root() -> Path:
"""Resolved per call, not a module constant: this module is imported before startup sets
UNSLOTH_STUDIO_HOME, which storage_roots reads."""
@@ -367,9 +396,32 @@ def begin(
try:
cdir.mkdir(parents = True, exist_ok = True)
- ctx.prev_inductor_dir = os.environ.get("TORCHINDUCTOR_CACHE_DIR")
- ctx.prev_inductor_dir_set = True
- os.environ["TORCHINDUCTOR_CACHE_DIR"] = str(cdir / "inductor")
+ inductor_dir = str(cdir / "inductor")
+ # Startup applies this same test before pinning TORCHINDUCTOR_CACHE_DIR, and this
+ # assignment used to overwrite whatever it decided, so a Studio root the builders cannot
+ # parse came back on the first compiled diffusion run after looking fine at launch. The
+ # bundle still lives under cdir either way; only the Inductor pin is withheld.
+ if _toolchain_path_unparseable(inductor_dir):
+ # Leaving the pin alone is not neutral: startup publishes ONE parseable fallback for
+ # the process, so keeping it here would have save_cache_artifacts serialise that
+ # shared cache into every fingerprinted bundle. Ask for one keyed on THIS cdir so the
+ # per-key isolation survives; if none can be had safely the pin stays as it was.
+ isolated = _parseable_cache_fallback("TORCHINDUCTOR_CACHE_DIR", inductor_dir)
+ if isolated is None:
+ _warn(
+ logger,
+ f"compile-cache: leaving TORCHINDUCTOR_CACHE_DIR as it is: {inductor_dir} "
+ "holds a character the C++ builders cannot paste into a command line "
+ "unquoted, and no safe replacement is available",
+ )
+ else:
+ ctx.prev_inductor_dir = os.environ.get("TORCHINDUCTOR_CACHE_DIR")
+ ctx.prev_inductor_dir_set = True
+ os.environ["TORCHINDUCTOR_CACHE_DIR"] = isolated
+ else:
+ ctx.prev_inductor_dir = os.environ.get("TORCHINDUCTOR_CACHE_DIR")
+ ctx.prev_inductor_dir_set = True
+ os.environ["TORCHINDUCTOR_CACHE_DIR"] = inductor_dir
except Exception as exc: # noqa: BLE001
_warn(logger, f"could not set TORCHINDUCTOR_CACHE_DIR: {exc}")
diff --git a/studio/backend/tests/test_diffusion_compile_cache.py b/studio/backend/tests/test_diffusion_compile_cache.py
index 988ed675c36..00eccb92b26 100644
--- a/studio/backend/tests/test_diffusion_compile_cache.py
+++ b/studio/backend/tests/test_diffusion_compile_cache.py
@@ -343,6 +343,91 @@ def test_restore_inductor_dir(monkeypatch, tmp_path, fake_megacache):
assert os.environ["TORCHINDUCTOR_CACHE_DIR"] == "/tmp/prior-inductor" # restored
+def test_the_import_fallback_matches_the_resolver(monkeypatch):
+ """The fallback runs when a CLI entry point reaches this module without studio/backend on
+ sys.path. Narrowed to whitespace it made the same path refused or accepted depending only on
+ sys.path, which is routing that depends on import order. Swept so the copy cannot drift."""
+ import builtins
+ import string
+
+ from utils.paths.storage_roots import toolchain_path_unparseable
+
+ real_import = builtins.__import__
+
+ def no_storage_roots(name, *args, **kwargs):
+ if name == "utils.paths.storage_roots":
+ raise ImportError("simulated: studio/backend is not on sys.path")
+ return real_import(name, *args, **kwargs)
+
+ monkeypatch.setattr(builtins, "__import__", no_storage_roots)
+
+ disagreed = []
+ for char in list(string.printable) + [" ", "é"]:
+ path = f"/srv/unsloth{char}root/cache/diffusion_compile_cache/key/inductor"
+ if cc._toolchain_path_unparseable(path) != toolchain_path_unparseable(path):
+ disagreed.append(char)
+
+ assert disagreed == [], "the fallback and the resolver disagree on: " + ", ".join(
+ repr(c) for c in disagreed
+ )
+
+
+ "name",
+ [
+ pytest.param("my studio", id = "a space"),
+ pytest.param("o'brien", id = "an apostrophe"),
+ ],
+)
+def test_an_unparseable_cache_root_never_pins_the_unparseable_path(
+ name, monkeypatch, tmp_path, fake_megacache
+):
+ """Startup declines to pin TORCHINDUCTOR_CACHE_DIR into a root the C++ builders cannot
+ parse, and this assignment used to overwrite that decision on the first compiled diffusion
+ run. Checking the environment just after launch would not have caught it.
+
+ What replaces it has to stay PER KEY. Simply keeping whatever startup left would keep the
+ one process-wide fallback, and save_cache_artifacts then serialises that shared cache into
+ every fingerprinted bundle, so each model accumulates the others'."""
+ import os
+
+ root = tmp_path / name
+ monkeypatch.setenv(cc._ENV_MODE, "auto")
+ monkeypatch.setenv(cc._ENV_DIR, str(root))
+ monkeypatch.delenv("TORCHINDUCTOR_CACHE_DIR", raising = False)
+
+ ctx = cc.begin(transformer = _transformer(), **_BEGIN_KW)
+
+ published = os.environ.get("TORCHINDUCTOR_CACHE_DIR")
+ assert published != str(ctx.dir / "inductor")
+ if published is not None:
+ assert not cc._toolchain_path_unparseable(published)
+ assert not published.startswith(str(root))
+ # The bundle is unaffected either way: only the Inductor pin moves.
+ assert ctx.dir.is_dir() and ctx.dir.is_relative_to(root)
+
+
+def test_two_models_under_an_unparseable_root_do_not_share_one_inductor_cache(
+ monkeypatch, tmp_path, fake_megacache
+):
+ """The isolation the per-key directory exists for has to survive the substitution."""
+ import os
+
+ root = tmp_path / "o'brien"
+ monkeypatch.setenv(cc._ENV_MODE, "auto")
+ monkeypatch.setenv(cc._ENV_DIR, str(root))
+ monkeypatch.delenv("TORCHINDUCTOR_CACHE_DIR", raising = False)
+
+ first = cc.begin(transformer = _transformer(), **_BEGIN_KW)
+ one = os.environ.get("TORCHINDUCTOR_CACHE_DIR")
+ other = dict(_BEGIN_KW, shape_bucket = "512x512")
+ second = cc.begin(transformer = _transformer(), **other)
+ two = os.environ.get("TORCHINDUCTOR_CACHE_DIR")
+
+ assert first.key != second.key
+ assert one and two and one != two
+
+
# -------------------------------------------------------------------------- legacy root
def _seed_legacy_bundle(monkeypatch, tmp_path, fake_megacache) -> tuple:
"""Write a bundle under a fake pre-relocation root, then hand back an upgraded install:
diff --git a/studio/backend/tests/test_setup_cache_env_containment.py b/studio/backend/tests/test_setup_cache_env_containment.py
index 3d0894d65cf..9a8000f6187 100644
--- a/studio/backend/tests/test_setup_cache_env_containment.py
+++ b/studio/backend/tests/test_setup_cache_env_containment.py
@@ -10,9 +10,13 @@ import json
import os
import platform
import re
+import shlex
import shutil
+import stat
+import string
import subprocess
import sys
+import tempfile
from pathlib import Path
import pytest
@@ -118,12 +122,41 @@ _TOOLCHAIN_PINNED = (
)
+def _as_the_compiler_sees_it(path):
+ """The path as cpp_builder puts it on the command line.
+
+ normalize_path_separator rewrites os.sep to "/" on Windows before the command is built, so a
+ Windows tmp_path measured raw would report every character as mangled: the separators alone
+ are enough for shlex to eat. Measuring the raw path made the whole sweep fail on
+ windows-latest while passing on Linux, which is the harness being wrong, not the guard.
+ """
+ return str(path).replace(os.sep, "/") if os.name == "nt" else str(path)
+
+
+def _assert_no_unparseable_pin(sr, refused_root):
+ """Whatever the resolver published for the toolchain keys, a compiler must be able to read
+ it, and it must not sit inside the root we just refused.
+
+ The rule is not "unset". torch's own fallback is <gettempdir>/torchinductor_<login> and it
+ sanitises only [\\/:*?"<>|], so an o'brien or First Last login lands back on the character
+ that caused the refusal. Unset is only acceptable when the temporary directory is no better.
+ """
+ for key in _TOOLCHAIN_PINNED:
+ value = os.environ.get(key)
+ if value is None:
+ continue
+ assert value.strip(), f"{key} was left blank, which Inductor reads as a relative path"
+ assert not sr.toolchain_path_unparseable(value), f"{key} was pinned to {value!r}"
+ assert not value.startswith(str(refused_root)), f"{key} stayed inside {refused_root}"
+
+
def test_a_spaced_root_leaves_the_compiler_caches_to_their_own_defaults(monkeypatch, tmp_path):
""" "C:\\Users\\First Last" is an ordinary Windows account name, so the DEFAULT Studio root
contains a space for a large share of installs. Before this file pinned these, Inductor used
- its own whitespace-free temporary directory and the build worked; pinning it into a spaced
- root broke torch.compile outright. Unset is the behaviour that shipped, so that is the
- fallback: the rest of the caches, which nobody pastes into a command line, still move."""
+ its own temporary directory and the build worked; pinning it into a spaced root broke
+ torch.compile outright. The refusal now publishes a parseable directory rather than hoping
+ torch's own default is one, since for a First Last login it is not. The rest of the caches,
+ which nobody pastes into a command line, still move."""
spaced = tmp_path / "my home" / "studio"
spaced.mkdir(parents = True)
monkeypatch.setenv("UNSLOTH_STUDIO_HOME", str(spaced))
@@ -131,15 +164,15 @@ def test_a_spaced_root_leaves_the_compiler_caches_to_their_own_defaults(monkeypa
sr._setup_cache_env()
- for key in _TOOLCHAIN_PINNED:
- assert key not in os.environ, f"{key} was pinned to a path with a space"
+ _assert_no_unparseable_pin(sr, spaced)
# Non-vacuity, and the point of the guard being narrow: everything else is still contained.
for key in ("UV_CACHE_DIR", "NUMBA_CACHE_DIR", "MPLCONFIGDIR", "UNSLOTH_COMPILE_LOCATION"):
assert os.environ[key].startswith(str(spaced.parent)), key
def test_a_root_without_spaces_still_pins_the_compiler_caches(monkeypatch, tmp_path):
- """The other half of the guard: it must fire on whitespace and nothing else."""
+ """The other half of the guard: it must fire on a path a compiler cannot take, not on
+ every path."""
plain = tmp_path / "plain_home" / "studio"
plain.mkdir(parents = True)
monkeypatch.setenv("UNSLOTH_STUDIO_HOME", str(plain))
@@ -151,10 +184,530 @@ def test_a_root_without_spaces_still_pins_the_compiler_caches(monkeypatch, tmp_p
assert os.environ.get(key, "").startswith(str(plain)), key
-def test_an_explicit_spaced_compiler_cache_is_left_alone(monkeypatch, tmp_path):
- """Only a default we invented is ours to withhold. A caller who set the variable chose it,
- and silently dropping it would send their cache somewhere they did not ask for."""
- chosen = tmp_path / "their choice"
+ "name",
+ [
+ pytest.param("o'brien", id = "an apostrophe, which shlex reads as an opening quote"),
+ pytest.param(
+ 'say"hi',
+ id = "a double quote",
+ marks = pytest.mark.skipif(
+ os.name == "nt",
+ reason = "illegal in an NTFS name, so the fixture cannot be created",
+ ),
+ ),
+ ],
+)
+def test_a_quoted_root_leaves_the_compiler_caches_to_their_own_defaults(
+ name, monkeypatch, tmp_path
+):
+ """Whitespace was the only character the guard knew, and a quote is worse than a space: in
+ POSIX mode shlex swallows the rest of the command into one argument and deletes the quote,
+ so the build fails somewhere less obvious than a split path. "/home/o'brien" is an ordinary
+ account name."""
+ quoted = tmp_path / name / "studio"
+ quoted.mkdir(parents = True)
+ monkeypatch.setenv("UNSLOTH_STUDIO_HOME", str(quoted))
+ sr = _load_storage_roots()
+
+ sr._setup_cache_env()
+
+ _assert_no_unparseable_pin(sr, quoted)
+ # Non-vacuity: the caches nobody pastes into a command line still move.
+ for key in ("UV_CACHE_DIR", "NUMBA_CACHE_DIR", "UNSLOTH_COMPILE_LOCATION"):
+ assert os.environ[key].startswith(str(quoted.parent)), key
+
+
[email protected](os.name == "nt", reason = "on Windows the separator is not an escape")
+def test_a_backslash_in_a_posix_root_leaves_the_compiler_caches_alone(monkeypatch, tmp_path):
+ """Legal in a POSIX filename and an escape to shlex, so the character is eaten and the
+ compiler is handed a path that does not exist. Windows is exempt because cpp_builder
+ rewrites the separator to "/" before it builds the command."""
+ odd = tmp_path / "a\\b" / "studio"
+ odd.mkdir(parents = True)
+ monkeypatch.setenv("UNSLOTH_STUDIO_HOME", str(odd))
+ sr = _load_storage_roots()
+
+ sr._setup_cache_env()
+
+ _assert_no_unparseable_pin(sr, odd)
+
+
+ "name",
+ [
+ "plain",
+ "my dir",
+ "o'brien",
+ 'say"hi',
+ "a\tb",
+ pytest.param(
+ "a\\b",
+ marks = pytest.mark.skipif(
+ os.name == "nt",
+ reason = "cpp_builder rewrites the separator before building the command",
+ ),
+ ),
+ ],
+)
+def test_the_guard_agrees_with_what_shlex_actually_does_to_the_command(name, tmp_path):
+ """The test that keeps the character list honest. Rather than restating the predicate, this
+ builds the command shape cpp_builder builds and asks shlex.split whether the path comes back
+ whole, then requires the predicate to have said so. A character added to one and not the
+ other fails here."""
+ path = _as_the_compiler_sees_it(tmp_path / name / "cache" / "torchinductor")
+ command = f"g++ {path}/main.cpp -o {path}/main.so"
+ sr = _load_storage_roots()
+
+ try:
+ survives = shlex.split(command) == ["g++", f"{path}/main.cpp", "-o", f"{path}/main.so"]
+ except ValueError:
+ survives = False # an odd number of quotes raises rather than mangling
+
+ assert sr.toolchain_path_unparseable(path) is not survives, (
+ f"{name!r}: predicate says {sr.toolchain_path_unparseable(path)}, "
+ f"shlex.split round trip says {survives}"
+ )
+
+
+def test_no_character_at_all_lets_a_mangled_path_through(tmp_path):
+ """The invariant that actually protects a user, swept over every printable ASCII character
+ plus the whitespace Python recognises and shlex does not.
+
+ The test above pins six characters both ways. This one allows the predicate to be too
+ careful and forbids it being not careful enough, which is the only direction that breaks a
+ build. It is deliberately one-sided: str.isspace() is true for a no-break space and the
+ other Unicode spaces, while shlex splits on ASCII whitespace only, so the predicate refuses
+ seven paths a compiler would in fact have accepted. That costs containment for those roots
+ and nothing else, it predates this file, and closing it would mean re-deriving shlex's own
+ whitespace set here."""
+ sr = _load_storage_roots()
+ specials = list(string.printable) + [" ", " ", " ", " ", " ", "é"]
+ if os.name == "nt":
+ specials.remove("\\") # the separator, rewritten to "/" before the command is built
+
+ leaked = []
+ for char in specials:
+ path = f"{_as_the_compiler_sees_it(tmp_path)}/unsloth{char}root/cache/torchinductor"
+ command = f"g++ {path}/main.cpp -o {path}/main.so"
+ try:
+ survives = shlex.split(command) == ["g++", f"{path}/main.cpp", "-o", f"{path}/main.so"]
+ except ValueError:
+ survives = False
+ if not survives and not sr.toolchain_path_unparseable(path):
+ leaked.append(char)
+
+ assert leaked == [], (
+ "these characters would be pinned into a compiler command line that mangles them: "
+ + ", ".join(repr(c) for c in leaked)
+ )
+
+
+def test_a_refused_root_gets_a_parseable_cache_rather_than_torchs_own(monkeypatch, tmp_path):
+ """Leaving the variable unset is not automatically safe, which is the whole reason this
+ branch publishes something.
+
+ torch's default is <gettempdir>/torchinductor_<login>, sanitised against [\\\\/:*?"<>|] only,
+ so an o'brien or a First Last login is handed back the character that caused the refusal
+ (torch/_inductor/runtime/cache_dir_utils.py::default_cache_dir). The replacement is named
+ from a hex digest, so it cannot carry one itself, and it is keyed on the path we wanted, so
+ the same install returns to the same cache every launch."""
+ refused = tmp_path / "o'brien" / "studio"
+ refused.mkdir(parents = True)
+ temp_root = tmp_path / "tmp"
+ temp_root.mkdir()
+ monkeypatch.setenv("UNSLOTH_STUDIO_HOME", str(refused))
+ monkeypatch.setattr(tempfile, "gettempdir", lambda: str(temp_root))
+ sr = _load_storage_roots()
+ monkeypatch.setattr(sr.tempfile, "gettempdir", lambda: str(temp_root))
+
+ sr._setup_cache_env()
+ first = os.environ["TORCHINDUCTOR_CACHE_DIR"]
+
+ assert not sr.toolchain_path_unparseable(first)
+ assert first.startswith(str(temp_root))
+ assert Path(first).is_dir()
+
+ # Stable: a cache that moved every launch would be a cold compile every launch.
+ for key in _TOOLCHAIN_PINNED:
+ monkeypatch.delenv(key, raising = False)
+ sr._setup_cache_env()
+ assert os.environ["TORCHINDUCTOR_CACHE_DIR"] == first
+
+
+def _fallback_path(sr, key, intended):
+ """The exact path the resolver will choose, asked OF the resolver.
+
+ Recomputing the digest here duplicated the naming rule, and the moment the account was
+ folded into it these fixtures silently stopped colliding with the real name: the planted
+ directory sat unused and the tests passed while proving nothing."""
+ return Path(sr._parseable_toolchain_fallback(key, intended))
+
+
+ "occupy",
+ [
+ pytest.param("file", id = "the name is already a regular file"),
+ pytest.param(
+ "readonly",
+ id = "the directory exists but cannot be written",
+ marks = pytest.mark.skipif(
+ os.name == "nt",
+ reason = "a read-only bit does not stop a write on Windows",
+ ),
+ ),
+ ],
+)
+def test_an_unusable_fallback_is_not_published_either(occupy, monkeypatch, tmp_path):
+ """The fallback gets the same probe as any other toolchain path.
+
+ torch treats the value as authoritative and never reconsiders, so publishing a name that is
+ a regular file, or a directory it cannot write into, fails every build rather than the one
+ case this branch exists to prevent. mkdir(exist_ok = True) cannot answer it: it raises
+ FileExistsError for the file and says nothing at all about writability."""
+ refused = tmp_path / "o'brien" / "studio"
+ refused.mkdir(parents = True)
+ temp_root = tmp_path / "tmp"
+ temp_root.mkdir()
+ monkeypatch.setenv("UNSLOTH_STUDIO_HOME", str(refused))
+ sr = _load_storage_roots()
+ monkeypatch.setattr(sr.tempfile, "gettempdir", lambda: str(temp_root))
+
+ # Occupy the exact name the fallback will choose, derived the way the resolver derives it.
+ intended = str(sr.cache_root() / "torchinductor")
+ squatter = _fallback_path(sr, "TORCHINDUCTOR_CACHE_DIR", intended)
+ if occupy == "file":
+ squatter.write_text("not a directory", encoding = "utf-8")
+ else:
+ squatter.mkdir()
+ squatter.chmod(0o500)
+
+ try:
+ sr._setup_cache_env()
+ finally:
+ if occupy == "readonly":
+ squatter.chmod(0o700)
+
+ assert "TORCHINDUCTOR_CACHE_DIR" not in os.environ
+
+
[email protected](os.name == "nt", reason = "the temporary root is per-account on Windows")
+ "plant",
+ [
+ pytest.param("world-writable", id = "a directory another user left writable"),
+ pytest.param("symlink", id = "a symlink planted at the name"),
+ ],
+)
+def test_a_fallback_another_user_could_have_planted_is_refused(plant, monkeypatch, tmp_path):
+ """The fallback sits in the SHARED temporary root under a name derived from the install, so
+ it is predictable to anyone on the host. A write probe alone accepted a directory another
+ local user had pre-created world-writable, and TORCH_EXTENSIONS_DIR is where torch loads
+ compiled .so files from, so that is code execution in the Studio process rather than an
+ untidy cache. Ownership cannot be staged here without a second uid, so these two cover the
+ permission bits and the symlink; the uid check is exercised by inspection only."""
+ refused = tmp_path / "o'brien" / "studio"
+ refused.mkdir(parents = True)
+ shared = tmp_path / "shared-tmp"
+ shared.mkdir()
+ monkeypatch.setenv("UNSLOTH_STUDIO_HOME", str(refused))
+ sr = _load_storage_roots()
+ monkeypatch.setattr(sr.tempfile, "gettempdir", lambda: str(shared))
+
+ intended = str(sr.cache_root() / "torch-extensions" / sr._torch_runtime_tag())
+ planted = _fallback_path(sr, "TORCH_EXTENSIONS_DIR", intended)
+ if plant == "world-writable":
+ planted.mkdir(mode = 0o777)
+ planted.chmod(0o777)
+ else:
+ (shared / "elsewhere").mkdir()
+ planted.symlink_to(shared / "elsewhere")
+
+ sr._setup_cache_env()
+
+ assert "TORCH_EXTENSIONS_DIR" not in os.environ
+
+
[email protected](os.name == "nt", reason = "POSIX permission bits")
+def test_the_fallback_it_creates_is_closed_to_everyone_else(monkeypatch, tmp_path):
+ """The other half: when nothing is in the way it must not create a cache the rest of the
+ host can write into either."""
+ refused = tmp_path / "o'brien" / "studio"
+ refused.mkdir(parents = True)
+ shared = tmp_path / "shared-tmp"
+ shared.mkdir()
+ monkeypatch.setenv("UNSLOTH_STUDIO_HOME", str(refused))
+ sr = _load_storage_roots()
+ monkeypatch.setattr(sr.tempfile, "gettempdir", lambda: str(shared))
+
+ sr._setup_cache_env()
+
+ published = Path(os.environ["TORCH_EXTENSIONS_DIR"])
+ mode = published.stat().st_mode
+ assert not mode & (stat.S_IWGRP | stat.S_IWOTH), oct(mode & 0o777)
+ assert published.stat().st_uid == os.geteuid()
+
+
[email protected](os.name == "nt", reason = "POSIX rename permissions")
+ "mode, published",
+ [
+ pytest.param(0o755, True, id = "a private holding directory"),
+ pytest.param(0o1777, True, id = "world-writable WITH the sticky bit, which is /tmp"),
+ pytest.param(0o1775, True, id = "group-writable WITH the sticky bit, owned by us"),
+ pytest.param(0o777, False, id = "world-writable and NOT sticky"),
+ pytest.param(0o775, False, id = "group-writable and NOT sticky"),
+ ],
+)
+def test_the_holding_directory_decides_whether_the_fallback_can_be_swapped(
+ mode, published, monkeypatch, tmp_path
+):
+ """Validating the cache directory settles who may write inside it and nothing else.
+
+ Renaming an entry is authorised by the write bit on its PARENT, and the sticky bit is what
+ narrows that to the entry's owner. So a shared temporary root that is writable and not
+ sticky lets another account rename ours aside and leave theirs at the same name between the
+ check and the compiler reading the variable. /tmp is 1777 and fine; a TMPDIR pointed at an
+ ordinary shared directory is not."""
+ refused = tmp_path / "o'brien" / "studio"
+ refused.mkdir(parents = True)
+ shared = tmp_path / "shared"
+ shared.mkdir()
+ shared.chmod(mode)
+ monkeypatch.setenv("UNSLOTH_STUDIO_HOME", str(refused))
+ sr = _load_storage_roots()
+ monkeypatch.setattr(sr.tempfile, "gettempdir", lambda: str(shared))
+
+ try:
+ sr._setup_cache_env()
+ finally:
+ shared.chmod(0o755)
+
+ assert ("TORCH_EXTENSIONS_DIR" in os.environ) is published
+
+
[email protected](os.name == "nt", reason = "POSIX ownership semantics")
+def test_an_ancestor_owned_by_another_account_is_refused(tmp_path):
+ """A mode is not a promise, because changing it belongs to the owner.
+
+ A 0755 holding directory owned by another ordinary account, with our 0700 temporary root
+ inside it, used to pass on today's write bits alone. Its owner can open it whenever it
+ likes, rename the root away and leave a tree holding the predictable cache name. So an
+ ancestor has to be held by this account or by root as well as be closed to others. The
+ foreign owner is applied through os.stat, since changing it for real needs privileges this
+ test does not have; everything else about the tree is real.
+ """
+ from utils.paths import storage_roots
+
+ outer = tmp_path / "outer"
+ outer.mkdir()
+ os.chmod(outer, 0o755)
+ root = outer / "victim-tmp"
+ root.mkdir()
+ os.chmod(root, 0o700)
+
+ assert (
+ storage_roots._holding_dir_is_safe(root) is True
+ ), "the same tree, ours throughout, is what the refusal below has to be measured against"
+
+ real_stat = os.stat
+
+ def foreign_owner(path, *args, **kwargs):
+ info = real_stat(path, *args, **kwargs)
+ if str(path) != str(outer):
+ return info
+ fields = list(info)[:10]
+ fields[4] = os.geteuid() + 1
+ return os.stat_result(tuple(fields))
+
+ os.stat = foreign_owner
+ try:
+ assert storage_roots._holding_dir_is_safe(root) is False
+ finally:
+ os.stat = real_stat
+
+
[email protected](os.name == "nt", reason = "POSIX symlink and mode semantics")
+def test_a_temporary_root_reached_through_a_swappable_link_is_refused(tmp_path, monkeypatch):
+ """A symlink is a name, and a name in a directory another account can write is not ours.
+
+ Resolving the holding directory first discarded the lexical path, so a link in a 0777
+ directory was judged on the private directory it pointed at while staying replaceable.
+ Both chains are walked now, which also catches the reverse, a link in a safe directory
+ aimed into a shared tree. Symlinked roots are not refused outright: /tmp and /var are
+ symlinks on macOS, so the third case below has to keep passing.
+ """
+ from utils.paths.storage_roots import _holding_dir_is_safe
+
+ shared = tmp_path / "shared"
+ shared.mkdir()
+ os.chmod(shared, 0o777)
+ private = tmp_path / "private"
+ private.mkdir()
+ os.chmod(private, 0o700)
+ safe = tmp_path / "safe"
+ safe.mkdir()
+ os.chmod(safe, 0o755)
+
+ swappable = shared / "tmp-link"
+ swappable.symlink_to(private)
+ assert _holding_dir_is_safe(swappable) is False
+
+ aimed_at_shared = safe / "bad-link"
+ target = shared / "target"
+ target.mkdir()
+ os.chmod(target, 0o700)
+ aimed_at_shared.symlink_to(target)
+ assert _holding_dir_is_safe(aimed_at_shared) is False
+
+ held_safely = safe / "ok-link"
+ held_safely.symlink_to(private)
+ assert (
+ _holding_dir_is_safe(held_safely) is True
+ ), "a symlinked temporary root in a directory nobody else can write is still usable"
+
+
[email protected](os.name == "nt", reason = "POSIX rename permissions")
+def test_a_private_temp_root_inside_a_shared_parent_is_refused(monkeypatch, tmp_path):
+ """Checking one level is not enough.
+
+ TMPDIR=/shared/victim-tmp at 0700 looks private, but if /shared is 0777 another account can
+ rename the whole root away and leave a tree carrying the predictable cache name in its
+ place, which substitutes the artifacts just as effectively as swapping the leaf. Every
+ ancestor is asked, bounded by the path's own depth."""
+ shared = tmp_path / "shared"
+ shared.mkdir()
+ private_root = shared / "victim-tmp"
+ private_root.mkdir(mode = 0o700)
+ sr = _load_storage_roots()
+
+ shared.chmod(0o777)
+ try:
+ assert sr._holding_dir_is_safe(private_root) is False
+ # The leaf alone still looks fine, which is exactly why the walk is needed.
+ assert sr._dir_is_not_swappable(private_root) is True
+ shared.chmod(0o755)
+ assert sr._holding_dir_is_safe(private_root) is True
+ finally:
+ shared.chmod(0o755)
+
+
[email protected](os.name == "nt", reason = "POSIX sticky semantics")
+def test_a_sticky_parent_owned_by_somebody_else_is_still_refused(monkeypatch, tmp_path):
+ """Sticky is not a blanket safe answer.
+
+ It narrows removal and rename of a child to the child's owner, the DIRECTORY's owner, and a
+ privileged process, so a sticky shared directory belonging to another ordinary account still
+ lets that account swap our cache after it was validated. /tmp passes because root owns it,
+ and that is the case this branch is actually for. Staged by reporting a foreign uid for the
+ parent, since a directory owned by another account cannot be created here."""
+ shared = tmp_path / "shared"
+ shared.mkdir()
+ shared.chmod(0o1777)
+ sr = _load_storage_roots()
+ real_stat = sr.os.stat
+ foreign = os.geteuid() + 4242
+
+ def lying_stat(path, *args, **kwargs):
+ info = real_stat(path, *args, **kwargs)
+ if str(path) == str(shared):
+ return os.stat_result(
+ (
+ info.st_mode,
+ info.st_ino,
+ info.st_dev,
+ info.st_nlink,
+ foreign,
+ info.st_gid,
+ info.st_size,
+ int(info.st_atime),
+ int(info.st_mtime),
+ int(info.st_ctime),
+ )
+ )
+ return info
+
+ monkeypatch.setattr(sr.os, "stat", lying_stat)
+ assert sr._holding_dir_is_safe(shared) is False
+
+ monkeypatch.setattr(sr.os, "stat", real_stat)
+ assert sr._holding_dir_is_safe(shared) is True
+
+
+def test_the_windows_holding_rule_accepts_only_the_per_account_temp_root(monkeypatch, tmp_path):
+ """os.stat reports no ownership on Windows and the 0o700 handed to os.mkdir buys nothing
+ there, so a redirected %TEMP% on a shared directory cannot be told from a private one
+ without reading ACLs. The default root under %LOCALAPPDATA%\\Temp already is per-account,
+ so that is the one accepted and a redirected root gets no fallback rather than an unverified
+ one. The predicate itself is platform independent, so it is exercised here rather than only
+ on a Windows runner."""
+ local = tmp_path / "AppData" / "Local"
+ default_temp = local / "Temp"
+ default_temp.mkdir(parents = True)
+ shared = tmp_path / "shared-temp"
+ shared.mkdir()
+ monkeypatch.setenv("LOCALAPPDATA", str(local))
+ sr = _load_storage_roots()
+
+ assert sr._windows_temp_root_is_private(default_temp) is True
+ assert sr._windows_temp_root_is_private(shared) is False
+
+ monkeypatch.delenv("LOCALAPPDATA", raising = False)
+ assert sr._windows_temp_root_is_private(default_temp) is False
+
+ # And it is actually WIRED IN. Exercising the predicate alone left the call site untested:
+ # reverting the Windows branch to an unconditional True kept every assertion above green.
+ # Asserted on the source rather than by behaviour, because forcing os.name to "nt" makes
+ # pathlib instantiate a WindowsPath and raise before the branch is ever reached. A source
+ # check is weaker than a run, and it does fail when the call is removed, which is the one
+ # thing this is guarding.
+ import inspect
+
+ windows_branch = inspect.getsource(sr._holding_dir_is_safe).split('if os.name == "nt":')[1]
+ assert "_windows_temp_root_is_private(parent)" in windows_branch.strip().splitlines()[0]
+
+
[email protected](os.name == "nt", reason = "the identity is the euid on POSIX")
+def test_two_accounts_under_one_temp_root_do_not_collide(monkeypatch, tmp_path):
+ """Keyed on the intended path alone, two OS accounts sharing one install derived the same
+ name. The first created it 0700 and every later account then failed the ownership check and
+ got nothing pinned, which for a login that is itself unparseable means the build fails after
+ all. The account goes into the digest, not into the visible name: a login is exactly the
+ kind of string that started this."""
+ sr = _load_storage_roots()
+ intended = "/srv/o'brien/studio/cache/torch-extensions/tag"
+
+ monkeypatch.setattr(sr.os, "geteuid", lambda: 1000)
+ mine = sr._parseable_toolchain_fallback("TORCH_EXTENSIONS_DIR", intended)
+ monkeypatch.setattr(sr.os, "geteuid", lambda: 1001)
+ theirs = sr._parseable_toolchain_fallback("TORCH_EXTENSIONS_DIR", intended)
+
+ assert mine and theirs and mine != theirs
+ # Still parseable, and still stable for one account across calls.
+ assert not sr.toolchain_path_unparseable(mine)
+ assert sr._parseable_toolchain_fallback("TORCH_EXTENSIONS_DIR", intended) == theirs
+
+
+def test_a_refused_root_with_no_usable_temp_root_still_publishes_nothing(monkeypatch, tmp_path):
+ """The other end of the same branch. When the temporary directory carries the character too
+ there is nowhere left to point, and an unset variable is better than a broken one."""
+ refused = tmp_path / "o'brien" / "studio"
+ refused.mkdir(parents = True)
+ monkeypatch.setenv("UNSLOTH_STUDIO_HOME", str(refused))
+ sr = _load_storage_roots()
+ monkeypatch.setattr(sr.tempfile, "gettempdir", lambda: str(tmp_path / "also'bad"))
+
+ sr._setup_cache_env()
+
+ for key in _TOOLCHAIN_PINNED:
+ assert key not in os.environ, key
+
+
+def test_an_explicit_compiler_cache_is_left_alone_when_the_builders_can_read_it(
+ monkeypatch, tmp_path
+):
+ """A caller who set the variable chose it, and a path the builders can read is carried out."""
+ chosen = tmp_path / "their-choice"
monkeypatch.setenv("TORCHINDUCTOR_CACHE_DIR", str(chosen))
sr = _load_storage_roots()
@@ -163,6 +716,40 @@ def test_an_explicit_spaced_compiler_cache_is_left_alone(monkeypatch, tmp_path):
assert os.environ["TORCHINDUCTOR_CACHE_DIR"] == str(chosen)
+def test_an_inherited_unreadable_compiler_cache_is_refused(monkeypatch, tmp_path):
+ """The one thing an explicit value cannot buy: a path the builders cannot paste in.
+
+ Honouring it guarantees a failed compile rather than carrying out a preference, and it
+ arrives by routes nobody chose. Windows persists the variable to the account, so an upgrade
+ inherits what the OLD setup wrote, through any shell already open and through the desktop
+ relaunch; clearing a stored copy cannot reach a process that already read it. Refusing
+ destroys nothing, the pin being process-local and the cache regenerable, which is why
+ setup.ps1 still establishes provenance before it deletes a stored value.
+ """
+ monkeypatch.setenv("TORCHINDUCTOR_CACHE_DIR", str(tmp_path / "their choice"))
+ sr = _load_storage_roots()
+ (tmp_path / "tmp").mkdir()
+ monkeypatch.setattr(sr.tempfile, "gettempdir", lambda: str(tmp_path / "tmp"))
+
+ sr._setup_cache_env()
+
+ landed = os.environ.get("TORCHINDUCTOR_CACHE_DIR")
+ assert landed != str(tmp_path / "their choice")
+ assert landed is not None, "a usable temporary root was available, so it should be used"
+ assert not sr.toolchain_path_unparseable(landed)
+
+
+def test_the_refusal_only_applies_to_the_toolchain_keys(monkeypatch, tmp_path):
+ """A spaced HuggingFace cache is nobody's compiler argument and stays exactly as given."""
+ chosen = tmp_path / "their choice"
+ monkeypatch.setenv("HF_HUB_CACHE", str(chosen))
+ sr = _load_storage_roots()
+
+ sr._setup_cache_env()
+
+ assert os.environ["HF_HUB_CACHE"] == str(chosen)
+
+
def test_default_install_leaves_the_shared_hf_cache_alone(monkeypatch, tmp_path):
home = tmp_path / "home"
monkeypatch.setenv("HOME", str(home))
@@ -1130,7 +1717,10 @@ def test_a_blank_toolchain_override_is_dropped_on_a_spaced_root(monkeypatch, tmp
sr._setup_cache_env()
- assert "TORCHINDUCTOR_CACHE_DIR" not in os.environ
+ assert (
+ os.environ.get("TORCHINDUCTOR_CACHE_DIR") or ""
+ ).strip() != "" or "TORCHINDUCTOR_CACHE_DIR" not in os.environ
+ _assert_no_unparseable_pin(sr, spaced)
def test_an_unusable_managed_inductor_path_is_not_published(monkeypatch, tmp_path):
diff --git a/studio/backend/utils/paths/storage_roots.py b/studio/backend/utils/paths/storage_roots.py
index 05ab40da79a..9c16255990d 100644
--- a/studio/backend/utils/paths/storage_roots.py
+++ b/studio/backend/utils/paths/storage_roots.py
@@ -3,6 +3,8 @@
from __future__ import annotations
+import contextlib
+import hashlib
import importlib.util
import json
import ntpath
@@ -999,8 +1001,9 @@ def _torch_runtime_tag() -> str:
# torch/_inductor/cpp_builder.py joins the g++ invocation with spaces and reparses it with
# shlex.split, so a root containing a space splits in two and the build fails outright
# ("g++: fatal error: input file .../my is the same as output file"). The quoting is PyTorch's
-# to fix; skipping the pin just returns to the temporary directory Inductor used before. Not a
-# rare shape: "C:\Users\First Last" is an ordinary Windows account name.
+# to fix; skipping the pin just returns to the temporary directory Inductor used before. Neither
+# shape is rare: "C:\Users\First Last" is an ordinary Windows account name and "/home/o'brien"
+# an ordinary POSIX one. toolchain_path_unparseable has the full character list.
_TOOLCHAIN_PATH_KEYS = frozenset(
{
"TORCHINDUCTOR_CACHE_DIR",
@@ -1044,9 +1047,181 @@ def _usable_dir(value: str) -> bool:
return True
+def toolchain_path_unparseable(value: str) -> bool:
+ """Whether a compiler command line holding *value* survives shlex.split intact.
+
+ cpp_builder pastes the path in unquoted and reparses in POSIX mode. Measured on the real
+ command shape with "/data/O'Brien/cache": plain 4 args intact, space 6 args, apostrophe
+ 2 args with the quotes gone (an odd number raises ValueError instead).
+
+ A backslash is POSIX-only. On Windows it is the separator, and
+ cpp_builder.normalize_path_separator rewrites it to "/" first, so rejecting it there would
+ reject every Windows path.
+ """
+ if any(ch.isspace() for ch in value):
+ return True
+ if "'" in value or '"' in value:
+ return True
+ return os.name != "nt" and "\\" in value
+
+
def _toolchain_unsafe(key: str, value: str) -> bool:
"""Whether pinning *key* to *value* would hand a compiler a path it cannot parse."""
- return key in _TOOLCHAIN_PATH_KEYS and any(ch.isspace() for ch in value)
+ return key in _TOOLCHAIN_PATH_KEYS and toolchain_path_unparseable(value)
+
+
+def _private_dir(path: str) -> bool:
+ """Whether *path* is a directory only this account can write, creating it if it is absent.
+
+ The fallback lives in the SHARED temporary root under a name derived from the install, so it
+ is predictable to anyone on the host. _usable_dir alone accepted a world-writable directory
+ another local user had pre-created and published it as TORCH_EXTENSIONS_DIR, which torch
+ loads compiled .so files from: local code execution in the Studio process.
+
+ The parent is asked FIRST and for both branches, since creating the directory settles who
+ owns it and nothing about who can rename it away afterwards. lstat, not stat, or a symlink
+ planted at the name is judged by its target. Ownership is POSIX-only: on Windows the
+ temporary root is already per-account under %LOCALAPPDATA% and st_uid means nothing.
+ """
+ parent = Path(path).parent
+ try:
+ parent.mkdir(parents = True, exist_ok = True)
+ except (OSError, ValueError):
+ return False
+ if not _holding_dir_is_safe(parent):
+ return False
+ try:
+ os.mkdir(path, 0o700)
+ return True
+ except FileExistsError:
+ pass
+ except (OSError, ValueError):
+ return False
+ try:
+ info = os.lstat(path)
+ except (OSError, ValueError):
+ return False
+ if not stat_module.S_ISDIR(info.st_mode) or stat_module.S_ISLNK(info.st_mode):
+ return False
+ if os.name == "nt":
+ return True
+ if info.st_uid != os.geteuid():
+ return False
+ return not info.st_mode & (stat_module.S_IWGRP | stat_module.S_IWOTH)
+
+
+def _windows_temp_root_is_private(parent: Path) -> bool:
+ """Whether *parent* is the per-account temporary root Windows gives by default.
+
+ os.stat reports no ownership on Windows and the 0o700 given to os.mkdir buys nothing there,
+ so a redirected %TEMP% cannot be told from a private one without reading ACLs, a dependency
+ this backend does not carry. %LOCALAPPDATA%\\Temp is already per-account and ACL'd by
+ Windows, so it is the one case accepted: a redirected root gets no fallback rather than an
+ unverified one, which costs that install containment and nothing else.
+ """
+ local = os.environ.get("LOCALAPPDATA")
+ if not local:
+ return False
+ try:
+ return os.path.normcase(str(parent.resolve())) == os.path.normcase(
+ str((Path(local) / "Temp").resolve())
+ )
+ except (OSError, ValueError):
+ return False
+
+
+def _dir_is_not_swappable(directory: Path) -> bool:
+ """Whether an entry inside *directory* can be renamed away by another account."""
+ try:
+ info = os.stat(directory)
+ except (OSError, ValueError):
+ return False
+ # Both halves, and neither substitutes for the other. Ownership, because a mode is not a
+ # promise: changing it belongs to the owner, so a 0755 directory held by another ordinary
+ # account is one call away from being writable by it. Root is trusted because it can rewrite
+ # anything anyway.
+ if info.st_uid not in (0, os.geteuid()):
+ return False
+ # And the write bits, because owning a directory does not stop anyone else writing in it.
+ if not info.st_mode & (stat_module.S_IWGRP | stat_module.S_IWOTH):
+ return True
+ # Sticky narrows rename of a child to the child's owner, the DIRECTORY's owner and root, so
+ # a shared directory is usable when we or root hold it. That is why /tmp passes.
+ return bool(info.st_mode & stat_module.S_ISVTX)
+
+
+def _holding_dir_is_safe(parent: Path) -> bool:
+ """Whether another account could swap the directory we are about to trust.
+
+ Every ANCESTOR is asked, not just the immediate holder: TMPDIR=/shared/victim-tmp at 0700
+ inside a 0777 /shared looked safe, and renaming the root through /shared substitutes a tree
+ holding the predictable cache name just as well as swapping the leaf.
+
+ BOTH chains are walked, the names given and the names resolved. Resolving first discarded
+ the lexical path, so /shared/tmp-link pointing at a private directory was judged on the
+ target alone while the link itself stayed replaceable; the resolved chain still catches the
+ reverse, a link in a safe directory aimed into a shared one. Symlinked roots are not refused
+ outright, since /tmp and /var are symlinks on macOS.
+
+ On Windows there are no POSIX bits to read, so the per-account default root answers instead.
+ """
+ if os.name == "nt":
+ return _windows_temp_root_is_private(parent)
+ try:
+ chains = (Path(os.path.abspath(parent)), parent.resolve())
+ except (OSError, ValueError):
+ return False
+ for start in chains:
+ current = start
+ while True:
+ if not _dir_is_not_swappable(current):
+ return False
+ if current.parent == current:
+ break
+ current = current.parent
+ return True
+
+
+def _parseable_toolchain_fallback(key: str, intended: str) -> str | None:
+ """A cache directory the C++ builders can read, or None when even the temp root is unusable.
+
+ Keyed on the path we WANTED, so one install returns to one directory and two installs under
+ one temp root do not share a cache. The digest is hex, so the name cannot itself carry an
+ unparseable character.
+
+ The ACCOUNT is in the digest too, or two OS accounts sharing an install would derive the
+ same name, the first would create it 0700 and the rest would fail the ownership check and
+ get nothing. Hashed rather than spelled out, because a login is exactly the kind of string
+ that started this.
+ """
+ try:
+ base = tempfile.gettempdir()
+ except (OSError, ValueError):
+ return None
+ if not base or toolchain_path_unparseable(base):
+ return None
+ account = str(os.geteuid()) if hasattr(os, "geteuid") else (os.environ.get("USERNAME") or "")
+ digest = hashlib.sha256(f"{account}\0{intended}".encode("utf-8", "replace")).hexdigest()[:12]
+ candidate = str(Path(base) / f"unsloth-{key.lower().replace('_', '-')}-{digest}")
+ # The join can still reintroduce one: gettempdir() is parseable but Path may normalise.
+ return None if toolchain_path_unparseable(candidate) else candidate
+
+
+def parseable_cache_fallback(key: str, intended: str) -> str | None:
+ """A directory ready to publish for *key*, or None when no safe one can be had.
+
+ Name, holding directory, ownership and a real write probe, in that order. One entry point
+ because the diffusion cache needs exactly the same answer: it used to leave the process-wide
+ pin in place when its own per-key path was unparseable, and once startup began publishing a
+ single shared fallback that meant save_cache_artifacts serialised one shared cache into
+ every fingerprinted bundle.
+ """
+ candidate = _parseable_toolchain_fallback(key, intended)
+ if candidate is None:
+ return None
+ if not _private_dir(candidate) or not _usable_dir(candidate):
+ return None
+ return candidate
def _setup_cache_env() -> None:
@@ -1085,13 +1260,54 @@ def _setup_cache_env() -> None:
for key, value in defaults.items():
# Blank counts as unset: an inherited KEY= would otherwise pin the cache to "", which puts an empty entry on
# sys.path and sends the compiler to the system temp directory instead.
- if not (os.environ.get(key) or "").strip():
- # An explicit value is still honoured above: the caller chose it, and only a default
- # we invented is ours to withhold.
+ inherited = (os.environ.get(key) or "").strip()
+ # An explicit value is the caller's, with one exception: a toolchain path the builders
+ # cannot read is not a preference that can be carried out, since the compile fails
+ # whatever was intended by it. It also arrives by routes nobody chose, because Windows
+ # persists the value to the account, so an upgrade inherits what the OLD setup wrote
+ # through any shell already open and through the desktop relaunch, which spawns the
+ # replacement from the running process. Clearing a stored copy cannot reach a process
+ # that already read it, so the refusal belongs where the path is about to be used.
+ # Process-local and destroying nothing, which is why setup.ps1 still establishes
+ # provenance before it deletes a stored value.
+ if inherited and _toolchain_unsafe(key, inherited):
+ # Seeded from the default, not the stale value, so a healed process and a clean
+ # install share one directory instead of one per stale path.
+ fallback = parseable_cache_fallback(key, value)
+ logger.debug(
+ "refusing inherited %s=%s: the C++ builders cannot paste it into a command line "
+ "unquoted; using %s",
+ key,
+ inherited,
+ fallback if fallback is not None else "no pin at all",
+ )
+ if fallback is not None:
+ os.environ[key] = fallback
+ else:
+ os.environ.pop(key, None)
+ continue
+ if not inherited:
if _toolchain_unsafe(key, value):
+ # Unset is not automatically safe: torch's own default is
+ # <gettempdir>/torchinductor_<user>, sanitised only against [\\/:*?"<>|], so a
+ # login lands back on the character that started this. Measured against
+ # cache_dir_utils.default_cache_dir, o'brien and First Last are both refused
+ # while say"hi is sanitised into a parseable name. So publish a path the
+ # builders can read rather than hope for one.
+ fallback = parseable_cache_fallback(key, value)
+ if fallback is not None:
+ logger.debug(
+ "%s holds a character the C++ builders cannot paste into a command "
+ "line unquoted; pinning %s to %s instead",
+ value,
+ key,
+ fallback,
+ )
+ os.environ[key] = fallback
+ continue
logger.debug(
- "leaving %s unset: %s contains whitespace, which the C++ builders "
- "paste into a command line unquoted",
+ "leaving %s unset: %s holds a character the C++ builders cannot paste "
+ "into a command line unquoted, and the temporary directory is no better",
key,
value,
)
diff --git a/studio/setup.ps1 b/studio/setup.ps1
index 4c61203d4e8..fd5f1ec764d 100644
--- a/studio/setup.ps1
+++ b/studio/setup.ps1
@@ -7380,6 +7380,43 @@ if ($script:PinChangedForceReinstall -or $script:TorchImportDefinitivelyFailed)
$SkipPythonDeps = $false
}
+# An upgrade has to take the old value away, not merely stop writing a new one: every setup
+# before the refusal existed wrote an apostrophe-named account's contained path to the USER
+# environment, so it is already there for exactly the account the refusal exists for. The backend
+# refuses such a value too, but only for its own process; this is what stops it being handed to
+# everything else on the account.
+#
+# Outside the dependency block on purpose, since a current core package and a verified UV_OFFLINE
+# tree both skip that block, and clearing needs none of its work. Only a value the builders
+# cannot read AND that this installer wrote is cleared.
+function Test-UnparseableManagedTorchCache {
+ param([string]$Value, [string]$Managed)
+ if (-not $Value) { return $false }
+ $trimmed = $Value.Trim().Replace('/', '\').TrimEnd('\')
+ # Shape first: a path the builders CAN read is working for somebody and is never touched.
+ if ($trimmed -notmatch '[\s'']') { return $false }
+ # Then provenance. The only unparseable value any setup has ever persisted is the contained
+ # path this same run computes, so anything else was configured by something we did not
+ # install and is not ours to delete: an older Torch quoted its compiler arguments, and such a
+ # value can still be working there.
+ return $trimmed -ieq $Managed.Trim().Replace('/', '\').TrimEnd('\')
+}
+
+function Clear-UnparseableTorchCacheEnv {
+ $managedTorchCache = Join-Path (Join-Path $StudioHome "cache") "torchinductor"
+ if (-not $StageRoot) {
+ $persisted = [Environment]::GetEnvironmentVariable('TORCHINDUCTOR_CACHE_DIR', 'User')
+ if (Test-UnparseableManagedTorchCache $persisted $managedTorchCache) {
+ [Environment]::SetEnvironmentVariable('TORCHINDUCTOR_CACHE_DIR', [NullString]::Value, 'User')
+ substep "cleared the persisted TORCHINDUCTOR_CACHE_DIR ($persisted): an earlier setup wrote it before the character was refused"
+ }
+ }
+ if (Test-UnparseableManagedTorchCache $env:TORCHINDUCTOR_CACHE_DIR $managedTorchCache) {
+ Remove-Item -LiteralPath Env:TORCHINDUCTOR_CACHE_DIR -ErrorAction SilentlyContinue
+ }
+}
+Clear-UnparseableTorchCacheEnv
+
if (-not $SkipPythonDeps) {
# Recover what a fresh shell lost, BEFORE the manifest is dropped below: recovery reads that file.
@@ -7455,25 +7492,45 @@ if ($script:UnslothVerbose) {
#
# Two things outrank that. Long paths off keeps the short drive-root directory, since Inductor's
# filenames hit MAX_PATH and a contained cache that cannot be written is worse than an
-# uncontained one. And a path containing a space is refused as
-# storage_roots._TOOLCHAIN_PATH_KEYS refuses one: cpp_builder.py pastes it into a compiler
-# command line unquoted, and "C:\Users\First Last" is an ordinary account name.
+# uncontained one. And whitespace or an apostrophe is refused exactly as
+# storage_roots.toolchain_path_unparseable refuses it, "C:\Users\First Last" and
+# "C:\Users\O'Brien" both being ordinary account names. The other two characters that predicate
+# rejects cannot arise here: a double quote is illegal in an NTFS name, and a backslash is the
+# separator, which cpp_builder rewrites to "/" before it builds the command.
$TorchCacheDir = $null
+$TorchCacheUnparseable = $false
if ($StageRoot) {
$TorchCacheDir = Join-Path $RuntimeRoot "TORCHINDUCTOR_CACHE_DIR"
} elseif ($LongPathsEnabled) {
$candidate = Join-Path (Join-Path $StudioHome "cache") "torchinductor"
- if ($candidate -notmatch '\s') { $TorchCacheDir = $candidate }
+ if ($candidate -notmatch '[\s'']') {
+ $TorchCacheDir = $candidate
+ } elseif ($candidate -notmatch '\s') {
+ # Apostrophe only, which is the population this change added. A spaced path keeps
+ # falling through to the drive-root directory it has always used.
+ $TorchCacheUnparseable = $true
+ }
+}
+# C:\tc is shared and predictable at a drive root, where the default ACL lets any account
+# create, so persisting it for a NEW population would hand an O'Brien account an Inductor cache
+# another local user could have made first. The widened refusal publishes nothing and leaves the
+# choice to storage_roots, which puts it under the per-account %LOCALAPPDATA%\Temp or declines.
+# The pre-existing triggers, long paths off and a spaced path, keep the drive-root directory
+# they have always used: that is not this change's to move.
+if (-not $TorchCacheDir -and -not $TorchCacheUnparseable) {
+ $TorchCacheDir = "C:\tc"
}
if (-not $TorchCacheDir) {
- $TorchCacheDir = "C:\tc"
+ substep "TORCHINDUCTOR_CACHE_DIR left unset: $candidate holds a character the C++ builders cannot paste into a command line, and the shared fallback is not account-private"
}
-if (-not (Test-Path -LiteralPath $TorchCacheDir)) { [System.IO.Directory]::CreateDirectory($TorchCacheDir) | Out-Null }
-$env:TORCHINDUCTOR_CACHE_DIR = $TorchCacheDir
-if (-not $StageRoot) {
- [Environment]::SetEnvironmentVariable('TORCHINDUCTOR_CACHE_DIR', $TorchCacheDir, 'User')
+if ($TorchCacheDir) {
+ if (-not (Test-Path -LiteralPath $TorchCacheDir)) { [System.IO.Directory]::CreateDirectory($TorchCacheDir) | Out-Null }
+ $env:TORCHINDUCTOR_CACHE_DIR = $TorchCacheDir
+ if (-not $StageRoot) {
+ [Environment]::SetEnvironmentVariable('TORCHINDUCTOR_CACHE_DIR', $TorchCacheDir, 'User')
+ }
+ substep "TORCHINDUCTOR_CACHE_DIR set to $TorchCacheDir (avoids MAX_PATH issues)"
}
-substep "TORCHINDUCTOR_CACHE_DIR set to $TorchCacheDir (avoids MAX_PATH issues)"
$PinnedTorchIndexUrl = Get-PinnedTorchIndexUrl
$TorchIndexPinned = [bool]$PinnedTorchIndexUrl
diff --git a/tests/test_setup_master_root.py b/tests/test_setup_master_root.py
index c090f219cbd..45408132d71 100644
--- a/tests/test_setup_master_root.py
+++ b/tests/test_setup_master_root.py
@@ -15,6 +15,7 @@ from __future__ import annotations
import os
import pathlib
+import re
import shutil
import subprocess
from pathlib import Path
@@ -94,7 +95,6 @@ def _env(home: Path, **overrides: str) -> dict[str, str]:
return env
-@NEEDS_POSIX_BASH
def _master_root_answer(tmp_path: Path, environment: str) -> str:
"""What uninstall.ps1's own _MasterRoot answers, with *environment* run before it.
@@ -123,6 +123,7 @@ foreach ($n in @("_ExpandTilde", "_MasterRoot")) {{
return out
+@NEEDS_POSIX_BASH
def test_a_master_root_puts_the_runtimes_beside_studio(tmp_path):
root = tmp_path / "portable"
(root / "studio").mkdir(parents = True)
@@ -749,14 +750,24 @@ def test_the_windows_inductor_cache_agrees_with_the_resolver():
applies on Windows and the containment this branch is for does not happen there. It has to
name the directory the resolver would have chosen. Two things still outrank that, and both
are recorded here so a later edit cannot quietly drop them: long paths off keeps the short
- drive-root directory for MAX_PATH headroom, and a path containing a space is refused for the
- same reason storage_roots does, since the C++ builders paste it in unquoted.
+ drive-root directory for MAX_PATH headroom, and a path holding whitespace or an apostrophe is
+ refused for the same reason storage_roots.toolchain_path_unparseable refuses one, since the
+ C++ builders paste it in unquoted and reparse it with shlex in POSIX mode.
"""
ps = SETUP_PS1.read_text(encoding = "utf-8")
block = _slice(ps, "$TorchCacheDir = $null", "$env:TORCHINDUCTOR_CACHE_DIR = $TorchCacheDir")
assert 'Join-Path (Join-Path $StudioHome "cache") "torchinductor"' in block
assert "$LongPathsEnabled" in block
- assert "'\\s'" in block or '"\\s"' in block, "the whitespace refusal is gone"
+ refusal = re.search(r"-notmatch\s+'(\[[^']*(?:''[^']*)*\])'", block)
+ assert refusal, "the unparseable-path refusal is gone"
+ assert "\\s" in refusal.group(1), "the whitespace refusal is gone"
+ assert "''" in refusal.group(1), "the apostrophe refusal is gone"
+ # The two refusals do NOT share a destination. A spaced path keeps the drive-root directory
+ # it has always used; an apostrophe-only path publishes nothing, because C:\tc is shared and
+ # predictable and _setup_cache_env honours an inherited value without applying its own
+ # per-account rule to it.
+ assert "$TorchCacheUnparseable" in block, "the apostrophe case lost its separate route"
+ assert "-not $TorchCacheUnparseable" in block
assert '"C:\\tc"' in block
roots = (REPO_ROOT / "studio" / "backend" / "utils" / "paths" / "storage_roots.py").read_text(
@@ -1055,6 +1066,59 @@ def test_the_windows_setup_records_the_master_root_for_the_uninstaller():
assert "[System.IO.File]::Move(" not in block
+def test_the_refused_windows_cache_path_is_taken_away_on_upgrade():
+ """Declining to write a value is not enough: the old one is already persisted.
+
+ Every setup before the refusal existed wrote an apostrophe-named account's contained path to
+ the USER environment, so on an upgrade it is already there for exactly the account the
+ refusal exists for, and every other process on that account still inherits it. The backend
+ refuses such a value for its own process; clearing it here is what stops it reaching the
+ rest. Only a value the builders cannot read is cleared, and only one this installer wrote.
+
+ And it has to clear on EVERY launch. The refusal itself lives inside
+ `if (-not $SkipPythonDeps)`, which a current core package and a verified UV_OFFLINE tree
+ both skip, so a cleanup nested in there would never reach that account. The clear needs
+ nothing that block computes, so it is hoisted out of it.
+ """
+ ps = SETUP_PS1.read_text(encoding = "utf-8")
+ body = _slice(
+ ps, "function Clear-UnparseableTorchCacheEnv {", "\nClear-UnparseableTorchCacheEnv"
+ )
+ code = "\n".join(line for line in body.splitlines() if not line.lstrip().startswith("#"))
+ assert "GetEnvironmentVariable('TORCHINDUCTOR_CACHE_DIR', 'User')" in code, code
+ assert "[NullString]::Value, 'User'" in code, code
+ assert "Remove-Item -LiteralPath Env:TORCHINDUCTOR_CACHE_DIR" in code, code
+ # Both clears go through the one predicate, so shape and provenance cannot drift apart.
+ assert code.count("Test-UnparseableManagedTorchCache") == 2, code
+ predicate = _slice(
+ ps, "function Test-UnparseableManagedTorchCache", "\nfunction Clear-Unparseable"
+ )
+ rule = "\n".join(l for l in predicate.splitlines() if not l.lstrip().startswith("#"))
+ # Shape: a path the builders can read belongs to whoever set it.
+ assert "-notmatch '[\\s'']'" in rule, rule
+ # Provenance: and so does an unparseable path this installer never wrote. Comparing against
+ # the contained path this run computes is the only evidence available, since nothing records
+ # who set the variable.
+ assert "$Managed" in rule and "-ieq" in rule, rule
+ assert '$managedTorchCache = Join-Path (Join-Path $StudioHome "cache") "torchinductor"' in code
+ # A staged run never writes the real account's environment, as the persist below does not.
+ assert "-not $StageRoot" in code, code
+
+ # The call is outside the dependency block, measured by brace depth rather than by reading.
+ lines = ps.splitlines()
+ gate = lines.index("if (-not $SkipPythonDeps) {")
+ depth = 0
+ for close, line in enumerate(lines[gate:], start = gate):
+ depth += line.count("{") - line.count("}")
+ if depth == 0:
+ break
+ calls = [i for i, line in enumerate(lines) if line.strip() == "Clear-UnparseableTorchCacheEnv"]
+ assert calls, "nothing calls the cleanup"
+ assert any(
+ i < gate for i in calls
+ ), "the cleanup only runs on a dependency pass, which the fast paths skip"
+
+
def test_the_windows_uninstaller_clears_the_inductor_path_it_persisted():
"""setup.ps1 writes TORCHINDUCTOR_CACHE_DIR to the USER environment, so it outlives the
install. Every later PyTorch process on the account inherits it, including ones unrelated to