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: stop a Git Bash nul file from blocking every Windows MXC tool call (#13037)

Daniel Hana13d3a50d908
diff --git a/studio/backend/core/inference/os_sandbox.py b/studio/backend/core/inference/os_sandbox.py
index 49eebd05764..63fed4a0ef9 100644
--- a/studio/backend/core/inference/os_sandbox.py
+++ b/studio/backend/core/inference/os_sandbox.py
@@ -7,6 +7,7 @@ from __future__ import annotations
import errno
import functools
import hashlib
+import ntpath
import os
import platform
import re
@@ -240,10 +241,29 @@ class _ScanBudgetExceeded(Exception):
"""The walk ran out of budget: not a hazard, so it must not refuse an `auto` launch."""
+def _extended_path(path: str) -> str:
+ """The \\\\?\\ spelling of a Windows path, which reaches a file named nul, con or com1 instead of the device."""
+ if path.startswith(("\\\\?\\", "\\\\.\\")):
+ return path
+ # Callers pass an absolute workdir. normpath is string-only; abspath would hand back \\.\nul for C:\work\nul.
+ absolute = ntpath.normpath(path)
+ if absolute.startswith("\\\\"):
+ return "\\\\?\\UNC\\" + absolute[2:]
+ return "\\\\?\\" + absolute
+
+
+# The real filesystem API, not sys.platform: tests stand in a Windows platform over a POSIX tmp_path.
+_NT_PATHS = os.name == "nt"
+
+
+def _entry_path(path: str) -> str:
+ return _extended_path(path) if _NT_PATHS else path
+
+
def directory_signature(path: str) -> tuple:
"""Identity plus mtime for one directory, the unit a cached verdict is re-checked in."""
try:
- info = os.stat(path)
+ info = os.stat(_entry_path(path))
except OSError:
return (path, None)
return (path, info.st_dev, info.st_ino, info.st_mtime_ns)
@@ -261,6 +281,15 @@ def _host_channel_hazard(
witness: "list[tuple] | None" = None,
) -> str | None:
"""Return a host-access hazard under *root*, or None."""
+ # One namespace for listing and stat, so a directory named nul is walked as the one stat judged.
+ top = _entry_path(root)
+ hazard = _walk_for_host_channels(top, max_entries, seconds, witness)
+ return hazard if hazard is None or top == root else hazard.replace(top, root)
+
+
+def _walk_for_host_channels(
+ root: str, max_entries: int, seconds: float, witness: "list[tuple] | None"
+) -> str | None:
deadline = time.monotonic() + seconds
entries = 0
# Only an unaccounted hard link leads outside; cp -al, git clone --local and pip make nlink > 1.
@@ -292,8 +321,9 @@ def _host_channel_hazard(
if stat.S_ISLNK(info.st_mode):
continue
if stat.S_ISDIR(info.st_mode):
- # Misses a same-filesystem bind mount; Linux also asks the mount table.
- if os.path.ismount(path):
+ # Misses a same-filesystem bind mount; Linux also asks the mount table. A Windows mounted folder is a
+ # reparse point, refused above, and ntpath.ismount calls a directory named nul the \\.\nul device root.
+ if not _NT_PATHS and os.path.ismount(path):
return f"contains a nested host mount: {path}"
continue
if not stat.S_ISREG(info.st_mode):
diff --git a/studio/backend/tests/test_sandbox_windows_reserved_names.py b/studio/backend/tests/test_sandbox_windows_reserved_names.py
new file mode 100644
index 00000000000..2e4040d9dbc
--- /dev/null
+++ b/studio/backend/tests/test_sandbox_windows_reserved_names.py
@@ -0,0 +1,192 @@
+# SPDX-License-Identifier: AGPL-3.0-only
+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
+
+"""A file named nul, con or com1 in the session workdir is a file, not the device (#12473)."""
+
+from __future__ import annotations
+
+import os
+import re
+import shutil
+import stat
+import subprocess
+import sys
+
+import pytest
+
+from core.inference import os_sandbox
+
+_RESERVED = ("nul", "con", "aux", "prn", "com1", "lpt1")
+_EXTENDED = "\\\\?\\"
+
+
+def _simulate_windows_stat(monkeypatch):
+ """A plain Win32 stat of ...\\nul answers for the NUL device; the \\\\?\\ spelling reaches the file."""
+ real_lstat, real_stat, real_scandir = os.lstat, os.stat, os.scandir
+
+ def posix(path):
+ return path[len(_EXTENDED) :].replace("\\", "/")
+
+ def windows(real):
+ def lookup(path, *args, **kwargs):
+ path = os.fspath(path)
+ if path.startswith(_EXTENDED):
+ return real(posix(path), *args, **kwargs)
+ stem = os.path.basename(path).split(".")[0].lower()
+ if stem in _RESERVED:
+ return os.stat_result((stat.S_IFCHR | 0o666, 0, 0, 0, 0, 0, 0, 0, 0, 0))
+ return real(path, *args, **kwargs)
+
+ return lookup
+
+ monkeypatch.setattr(os_sandbox, "_NT_PATHS", True, raising = False)
+ monkeypatch.setattr(os, "lstat", windows(real_lstat))
+ monkeypatch.setattr(os, "stat", windows(real_stat))
+ monkeypatch.setattr(
+ os,
+ "scandir",
+ lambda path = ".": real_scandir(posix(path) if str(path).startswith(_EXTENDED) else path),
+ )
+
+
+ ("path", "expected"),
+ [
+ ("C:\\Users\\a\\work\\nul", "\\\\?\\C:\\Users\\a\\work\\nul"),
+ ("C:/Users/a/work/nul", "\\\\?\\C:\\Users\\a\\work\\nul"),
+ ("C:\\Users\\a\\work\\.\\sub\\..\\nul", "\\\\?\\C:\\Users\\a\\work\\nul"),
+ ("\\\\server\\share\\work\\nul", "\\\\?\\UNC\\server\\share\\work\\nul"),
+ ("\\\\?\\C:\\Users\\a\\work\\nul", "\\\\?\\C:\\Users\\a\\work\\nul"),
+ ("\\\\?\\UNC\\server\\share\\nul", "\\\\?\\UNC\\server\\share\\nul"),
+ ],
+)
+def test_the_extended_spelling_of_a_workdir_entry(path, expected):
+ assert os_sandbox._extended_path(path) == expected
+
+
[email protected](sys.platform == "win32", reason = "simulates Windows stat on a POSIX filesystem")
[email protected]("name", ["nul", "NUL", "con", "com1", "lpt1", "nul.txt"])
+def test_a_reserved_name_file_does_not_refuse_the_session(monkeypatch, tmp_path, name):
+ workdir = tmp_path / "work"
+ (workdir / "sub").mkdir(parents = True)
+ (workdir / name).write_text("hi\n")
+ (workdir / "sub" / name).write_text("hi\n")
+ _simulate_windows_stat(monkeypatch)
+
+ assert os_sandbox.scan_workdir_for_host_channels(str(workdir)) == ()
+ assert (workdir / name).read_text() == "hi\n"
+
+
[email protected](sys.platform == "win32", reason = "simulates Windows stat on a POSIX filesystem")
+def test_a_reserved_name_directory_is_walked_and_witnessed(monkeypatch, tmp_path):
+ workdir = tmp_path / "work"
+ (workdir / "nul").mkdir(parents = True)
+ (workdir / "nul" / "out.txt").write_text("hi\n")
+ _simulate_windows_stat(monkeypatch)
+
+ witness: list = []
+ assert os_sandbox.cache_share_hazard(str(workdir), witness) is None
+ assert os_sandbox.directory_witness_matches(witness)
+ (workdir / "nul" / "new.txt").write_text("hi\n")
+ assert not os_sandbox.directory_witness_matches(witness)
+
+
[email protected](sys.platform == "win32", reason = "simulates Windows stat on a POSIX filesystem")
+def test_a_real_device_node_beside_a_nul_file_still_refuses(monkeypatch, tmp_path):
+ workdir = tmp_path / "work"
+ workdir.mkdir()
+ (workdir / "nul").write_text("hi\n")
+ os.mkfifo(workdir / "planted")
+ _simulate_windows_stat(monkeypatch)
+
+ # Named as the caller spelled it, not by the \\?\ path the walk used.
+ with pytest.raises(
+ os_sandbox.WorkdirUnsafeError,
+ match = f"device or IPC node: {re.escape(str(workdir / 'planted'))}$",
+ ):
+ os_sandbox.scan_workdir_for_host_channels(str(workdir))
+
+
[email protected](sys.platform == "win32", reason = "simulates Windows stat on a POSIX filesystem")
+def test_an_outside_hard_link_named_nul_still_refuses(monkeypatch, tmp_path):
+ outside = tmp_path / "outside.txt"
+ outside.write_text("secret\n")
+ workdir = tmp_path / "work"
+ workdir.mkdir()
+ os.link(outside, workdir / "nul")
+ _simulate_windows_stat(monkeypatch)
+
+ with pytest.raises(os_sandbox.WorkdirUnsafeError, match = "hard-linked from outside"):
+ os_sandbox.scan_workdir_for_host_channels(str(workdir))
+
+
+def _on_disk(path):
+ # Not the helper under test, so main runs the same fixture.
+ return _EXTENDED + path
+
+
+def _remove_extended(path):
+ # pytest's own cleanup uses plain paths and cannot remove it.
+ extended = _on_disk(path)
+ if os.path.isdir(extended):
+ shutil.rmtree(extended)
+ elif os.path.lexists(extended):
+ os.remove(extended)
+
+
[email protected](sys.platform != "win32", reason = "needs the Win32 reserved-name mapping")
+def test_a_nul_file_does_not_refuse_the_session_on_windows(tmp_path):
+ workdir = tmp_path / "work"
+ workdir.mkdir()
+ planted = str(workdir / "nul")
+ with open(_on_disk(planted), "w") as handle:
+ handle.write("hi\n")
+ try:
+ # The defect this guards: a plain stat of the file answers for the NUL device.
+ assert not stat.S_ISREG(os.lstat(planted).st_mode)
+ assert stat.S_ISREG(os.lstat(_on_disk(planted)).st_mode)
+ assert os_sandbox.scan_workdir_for_host_channels(str(workdir)) == ()
+ finally:
+ _remove_extended(planted)
+
+
[email protected](sys.platform != "win32", reason = "needs the Win32 reserved-name mapping")
+def test_a_nul_directory_is_walked_on_windows(tmp_path):
+ workdir = tmp_path / "work"
+ workdir.mkdir()
+ planted = str(workdir / "nul")
+ os.mkdir(_on_disk(planted))
+ try:
+ with open(_on_disk(os.path.join(planted, "out.txt")), "w") as handle:
+ handle.write("hi\n")
+ assert os_sandbox.scan_workdir_for_host_channels(str(workdir)) == ()
+ os.link(
+ _on_disk(os.path.join(planted, "out.txt")),
+ str(tmp_path / "outside.txt"),
+ )
+ with pytest.raises(os_sandbox.WorkdirUnsafeError, match = "hard-linked from outside"):
+ os_sandbox.scan_workdir_for_host_channels(str(workdir))
+ finally:
+ _remove_extended(planted)
+
+
[email protected](sys.platform != "win32", reason = "needs Git Bash on Windows")
+def test_git_bash_redirect_to_nul_does_not_refuse_the_session(tmp_path):
+ from core.inference import tools
+
+ bash = tools._windows_bash()
+ if bash is None:
+ pytest.skip(
+ reason = "no trusted Git Bash: Studio runs the Terminal on cmd.exe, where > nul is the device"
+ )
+ workdir = tmp_path / "work"
+ workdir.mkdir()
+ planted = str(workdir / "nul")
+ try:
+ subprocess.run([bash, "-c", "echo hi > nul"], cwd = workdir, check = True, timeout = 60)
+ assert stat.S_ISREG(
+ os.lstat(_on_disk(planted)).st_mode
+ ), "Git Bash did not leave a nul file"
+ assert os_sandbox.scan_workdir_for_host_channels(str(workdir)) == ()
+ finally:
+ _remove_extended(planted)