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.
Python agentaichatgptdeepseekfine-tuninggemmaimage-generationllamallmllmsopenaipythonqwenreinforcement-learningself-hostedstable-diffusiontext-to-speechttsuiunsloth
Studio: launch sandboxed tool processes without a Python pre-exec on Linux (#15625)
Daniel Han3533d1a57468
diff --git a/studio/backend/core/inference/tools.py b/studio/backend/core/inference/tools.pyindex c1f549c11e0..8790a5ff783 100644--- a/studio/backend/core/inference/tools.py+++ b/studio/backend/core/inference/tools.py@@ -8747,6 +8747,230 @@ def _sandbox_preexec():pass+# _sandbox_preexec without a Python pre-exec (Linux): Popen does setsid/umask in C, setpriv and prlimit exec in+# place (same PID), so the server is vforked instead of forked through interpreter code.+_SANDBOX_SHIM_DIRS = ("/usr/bin", "/bin", "/usr/sbin", "/sbin")+_sandbox_shims: "tuple[str, str] | None | bool" = False # False = not probed yet+_sandbox_shims_lock = threading.Lock()+++def _trusted_system_binary(name: str) -> "str | None":+ """Root-owned, not group/world writable, no set-id bit or file capability: either would clear the death signal.+ World-readable too: an unreadable binary runs non-dumpable, which hides /proc/<pid>/exe from the readiness wait."""+ for directory in _SANDBOX_SHIM_DIRS:+ path = os.path.join(directory, name)+ try:+ st = os.stat(path)+ except OSError:+ continue+ if (+ not S_ISREG(st.st_mode)+ or st.st_uid != 0+ or st.st_mode & 0o022+ or st.st_mode & (stat.S_ISUID | stat.S_ISGID)+ or not st.st_mode & 0o111+ or not st.st_mode & 0o004+ ):+ continue+ try:+ os.getxattr(path, "security.capability")+ continue+ except OSError:+ pass+ return path+ return None+++def _probe_sandbox_shims() -> "tuple[str, str] | None":+ setpriv = _trusted_system_binary("setpriv")+ prlimit = _trusted_system_binary("prlimit")+ if setpriv is None or prlimit is None:+ return None+ try:+ # Old setpriv lacks --pdeathsig (util-linux < 2.33), and busybox-style builds take other flags.+ # The limits a launch sets, so a policy refusing one of them (SELinux checks a changed hard limit) keeps+ # the pre-exec, which skips a refused limit where prlimit would refuse the launch.+ limits = _sandbox_rlimit_args()+ if not limits:+ soft, hard = _resource.getrlimit(_resource.RLIMIT_CPU)+ limits = [+ "--cpu={}:{}".format(+ "unlimited" if soft == _resource.RLIM_INFINITY else soft,+ "unlimited" if hard == _resource.RLIM_INFINITY else hard,+ )+ ]+ result = subprocess.run(+ [+ setpriv,+ "--no-new-privs",+ "--pdeathsig",+ "KILL",+ "--",+ prlimit,+ *limits,+ "--",+ setpriv,+ "--dump",+ ],+ stdin = subprocess.DEVNULL,+ stdout = subprocess.PIPE,+ stderr = subprocess.DEVNULL,+ env = {"PATH": os.defpath, "LC_ALL": "C"},+ timeout = 5,+ start_new_session = True,+ )+ except (OSError, ValueError, subprocess.SubprocessError, AttributeError):+ return None+ # Read back after two more execs: an LSM transition on exec (AppArmor, SELinux) would clear the death signal.+ dump = result.stdout.decode("ascii", "replace").splitlines()+ if (+ result.returncode != 0+ or "no_new_privs: 1" not in dump+ or not any(+ line.startswith("Parent death signal:") and line.split(":", 1)[1].strip(" []") == "KILL"+ for line in dump+ )+ ):+ return None+ try:+ # Read by _wait_for_death_signal.+ os.readlink("/proc/self/exe")+ except OSError:+ return None+ return setpriv, prlimit+++def _sandbox_exec_shims() -> "tuple[str, str] | None":+ global _sandbox_shims+ if _sandbox_shims is False:+ with _sandbox_shims_lock:+ if _sandbox_shims is False:+ _sandbox_shims = _probe_sandbox_shims()+ return _sandbox_shims+++def _sandbox_rlimit_args() -> "list[str] | None":+ """The limits _sandbox_preexec would set, as prlimit flags; None if one could fail there (it skips a failing+ limit, prlimit refuses the launch), so the caller keeps the pre-exec for that host."""+ if _resource is None:+ return None+ wanted = []+ try:+ wanted.append(+ (+ "nproc",+ _resource.RLIMIT_NPROC,+ int(os.environ.get("UNSLOTH_STUDIO_SANDBOX_NPROC", "10000")),+ )+ )+ except ValueError:+ pass+ wanted.append(("fsize", _resource.RLIMIT_FSIZE, 100 * 1024 * 1024))+ if _sandbox_as_bytes is not None:+ wanted.append(("as", _resource.RLIMIT_AS, _sandbox_as_bytes))+ try:+ wanted.append(+ (+ "cpu",+ _resource.RLIMIT_CPU,+ int(os.environ.get("UNSLOTH_STUDIO_SANDBOX_CPU_S", "600")),+ )+ )+ except ValueError:+ pass+ try:+ nofile = int(os.environ.get("UNSLOTH_STUDIO_SANDBOX_NOFILE", "16384"))+ hard_cur = _resource.getrlimit(_resource.RLIMIT_NOFILE)[1]+ target = nofile if hard_cur == _resource.RLIM_INFINITY else min(nofile, hard_cur)+ if target > _sandbox_nr_open():+ # Above fs.nr_open setrlimit fails even under the hard limit: the pre-exec skips it.+ return None+ wanted.append(("nofile", _resource.RLIMIT_NOFILE, target))+ except (ValueError, OSError):+ pass+ args = []+ for flag, resource_id, value in wanted:+ if not isinstance(value, int) or value < 0 or value >= 2**63:+ return None+ try:+ hard_cur = _resource.getrlimit(resource_id)[1]+ except (ValueError, OSError):+ return None+ if hard_cur != _resource.RLIM_INFINITY and value > hard_cur:+ # Raising a hard limit needs CAP_SYS_RESOURCE: the pre-exec skips it without, raises it with.+ if _has_cap_sys_resource():+ return None+ continue+ args.append(f"--{flag}={value}:{value}")+ return args+++def _sandbox_nr_open() -> int:+ """fs.nr_open, or 0 when unreadable (which keeps the pre-exec)."""+ try:+ with open("/proc/sys/fs/nr_open", encoding = "ascii") as f:+ return int(f.read().strip())+ except (OSError, ValueError):+ return 0+++def _has_cap_sys_resource() -> bool:+ """Unknown counts as held, which keeps the pre-exec."""+ try:+ with open("/proc/self/status", encoding = "ascii", errors = "replace") as f:+ for line in f:+ if line.startswith("CapEff:"):+ return bool(int(line.split()[1], 16) & (1 << 24))+ except (OSError, ValueError, IndexError):+ pass+ return True+++def _sandbox_shim_argv(argv, env) -> "tuple[str, ...] | None":+ """argv behind setpriv and prlimit, or None to keep _sandbox_preexec."""+ # Python 3.9 forks (no vfork) even without a pre-exec, so the shims would only add two execs there.+ if sys.platform != "linux" or sys.version_info < (3, 10) or not argv:+ return None+ shims = _sandbox_exec_shims()+ if shims is None:+ return None+ # A missing program keeps Popen's own FileNotFoundError instead of a prlimit exit status.+ search = os.pathsep.join(os.get_exec_path(env))+ if shutil.which(argv[0], path = search) is None:+ return None+ limits = _sandbox_rlimit_args()+ if not limits:+ # Without a flag prlimit prints its limits table on a terminal before running the program.+ return None+ setpriv, prlimit = shims+ return (setpriv, "--no-new-privs", "--pdeathsig", "KILL", "--", prlimit, *limits, "--", *argv)+++def _wait_for_death_signal(pid: int, setpriv: str) -> None:+ """Popen returns before setpriv arms the death signal; the pre-exec armed it before Popen returned, so this+ waits as long as Popen would have (no deadline: returning early would hand back an unarmed child)."""+ delay = 0.0001+ while True:+ try:+ exe = os.readlink(f"/proc/{pid}/exe")+ except OSError:+ return # exited (setpriv is readable, so a live one is not hidden)+ if exe not in (setpriv, setpriv + " (deleted)"):+ return+ time.sleep(delay)+ delay = min(delay * 2, 0.005)+++def _spawn_behind_sandbox_shims(prepared, popen_kwargs):+ proc = subprocess.Popen(prepared.argv, **popen_kwargs)+ if prepared.execution_record is not None:+ prepared.execution_record = dataclasses.replace(+ prepared.execution_record, execution_status = "started"+ )+ _wait_for_death_signal(proc.pid, os.path.realpath(prepared.argv[0]))+ return proc++def _account_confinement():"""Confinement for the acting account's next child: None for owner, raises if unavailable."""return account_confinement(_SANDBOX_SITE_DIR)@@ -9100,7 +9324,17 @@ def _apply_prepared_launch(prepared, popen_kwargs: dict) -> dict:popen_kwargs.pop("preexec_fn", None)popen_kwargs["start_new_session"] = Trueelse:- popen_kwargs["preexec_fn"] = prepared.preexec_fn+ shim_argv = None+ if prepared.preexec_fn is _sandbox_preexec and prepared.spawn_callback is None:+ shim_argv = _sandbox_shim_argv(prepared.argv, prepared.env)+ if shim_argv is not None:+ popen_kwargs.pop("preexec_fn", None)+ popen_kwargs["start_new_session"] = True+ popen_kwargs["umask"] = 0o077+ prepared.argv = shim_argv+ prepared.spawn_callback = _spawn_behind_sandbox_shims+ else:+ popen_kwargs["preexec_fn"] = prepared.preexec_fnpopen_kwargs["close_fds"] = prepared.close_fdsif prepared.pass_fds:# Empty for every fallback, so Windows never sees a kwarg it rejects.diff --git a/studio/backend/tests/test_bypass_permissions.py b/studio/backend/tests/test_bypass_permissions.pyindex 7738979e7fa..4f5d886e65d 100644--- a/studio/backend/tests/test_bypass_permissions.py+++ b/studio/backend/tests/test_bypass_permissions.py@@ -134,7 +134,13 @@ def test_python_sandboxed_uses_sandbox_preexec_and_safe_env(captured_prepared_lamonkeypatch.setenv("HF_TOKEN", "secret-abc")_python_exec("print(1)", None, 5, "t", disable_sandbox = False)kwargs = captured_prepared_launch["kwargs"]- assert _carries(kwargs["preexec_fn"], tools._sandbox_preexec)+ if "preexec_fn" in kwargs:+ assert _carries(kwargs["preexec_fn"], tools._sandbox_preexec)+ else:+ # The same setup without a Python pre-exec: setsid and umask by Popen, the rest by setpriv/prlimit.+ argv = captured_prepared_launch["prepared"].argv+ assert kwargs["start_new_session"] is True and kwargs["umask"] == 0o077+ assert argv[1:5] == ("--no-new-privs", "--pdeathsig", "KILL", "--")assert "HF_TOKEN" not in kwargs["env"]diff --git a/studio/backend/tests/test_sandbox_spawn_shims.py b/studio/backend/tests/test_sandbox_spawn_shims.pynew file mode 100644index 00000000000..577dfd2ec09--- /dev/null+++ b/studio/backend/tests/test_sandbox_spawn_shims.py@@ -0,0 +1,352 @@+# SPDX-License-Identifier: AGPL-3.0-only+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0++"""Sandboxed tool launches without a Python pre-exec: the child must get exactly what _sandbox_preexec gave it."""++import json+import os+import signal+import subprocess+import sys+import textwrap+import threading+import time++import pytest++import core.inference.tools as tools+from core.inference import os_sandbox++pytestmark = pytest.mark.skipif(sys.platform != "linux", reason = "the shims are Linux only")++# The production decision, probe included: installed binaries that fail it keep the pre-exec.+_HAVE_SHIMS = (+ sys.platform == "linux"+ and sys.version_info >= (3, 10)+ and tools._sandbox_exec_shims() is not None+)+_needs_shims = pytest.mark.skipif(+ not _HAVE_SHIMS,+ reason = "util-linux setpriv/prlimit missing or refused by the probe, or Python 3.9 (keeps the pre-exec)",+)++_REPORT = textwrap.dedent(+ """+ import ctypes, json, os, resource as r+ libc = ctypes.CDLL(None, use_errno=True)+ sig = ctypes.c_int(-1)+ libc.prctl(2, ctypes.byref(sig), 0, 0, 0) # PR_GET_PDEATHSIG+ um = os.umask(0)+ nnp = [l.split()[1] for l in open('/proc/self/status') if l.startswith('NoNewPrivs')][0]+ names = ['RLIMIT_NPROC', 'RLIMIT_FSIZE', 'RLIMIT_AS', 'RLIMIT_CPU', 'RLIMIT_NOFILE']+ print(json.dumps(dict(pid=os.getpid(), session_leader=os.getsid(0) == os.getpid(),+ group_leader=os.getpgid(0) == os.getpid(), umask=um, nnp=nnp, pdeathsig=sig.value,+ limits={n: r.getrlimit(getattr(r, n)) for n in names})))+ """+)++[email protected](autouse = True)+def _fresh_probe(monkeypatch):+ monkeypatch.setattr(tools, "_sandbox_shims", False)+++def _launch(tmp_path, argv, **popen_kwargs):+ plan = os_sandbox.ToolLaunchPlan(+ argv = tuple(argv),+ workdir = str(tmp_path),+ env = tools._build_safe_env(str(tmp_path)),+ preexec_fn = tools._sandbox_preexec,+ requested_mode = "software",+ timeout_seconds = 30,+ execution_kind = "python",+ )+ tools._refresh_sandbox_memory_limit()+ prepared = tools._prepare_tool_launch(plan)+ kwargs = dict(+ stdin = subprocess.DEVNULL, stdout = subprocess.PIPE, stderr = subprocess.STDOUT, text = True+ )+ kwargs.update(popen_kwargs)+ kwargs = tools._apply_prepared_launch(prepared, kwargs)+ return os_sandbox.spawn_prepared_launch(prepared, **kwargs), prepared, kwargs+++def _report(tmp_path):+ proc, prepared, kwargs = _launch(tmp_path, [sys.executable, "-c", _REPORT])+ out, _ = proc.communicate(timeout = 30)+ seen = json.loads(out.strip().splitlines()[-1])+ assert (+ seen.pop("pid") == proc.pid+ ), "the tool must keep the Popen pid: every kill path signals it"+ return seen, prepared, kwargs+++def _alive(pid):+ try:+ with open(f"/proc/{pid}/stat") as f:+ return f.read().rsplit(")", 1)[1].split()[0] != "Z"+ except OSError:+ return False+++def _gone(pid, budget = 5.0):+ end = time.monotonic() + budget+ while time.monotonic() < end and _alive(pid):+ time.sleep(0.05)+ return not _alive(pid)+++@_needs_shims+def test_sandboxed_launch_has_no_python_preexec(tmp_path):+ _, prepared, kwargs = _report(tmp_path)+ assert "preexec_fn" not in kwargs+ assert kwargs["start_new_session"] is True+ assert kwargs["umask"] == 0o077+ assert prepared.argv[0].endswith("/setpriv")+++@_needs_shims+def test_child_sees_what_the_preexec_gave_it(tmp_path, monkeypatch):+ shimmed, _, kwargs = _report(tmp_path)+ assert "preexec_fn" not in kwargs+ monkeypatch.setattr(tools, "_sandbox_shims", None)+ reference, _, kwargs = _report(tmp_path)+ assert kwargs["preexec_fn"] is tools._sandbox_preexec+ assert shimmed == reference+ assert reference["session_leader"] and reference["group_leader"]+ assert reference["umask"] == 0o077 and reference["nnp"] == "1"+ assert reference["pdeathsig"] == signal.SIGKILL+++@_needs_shims+ "env",+ [+ {"UNSLOTH_STUDIO_SANDBOX_NPROC": "not-a-number", "UNSLOTH_STUDIO_SANDBOX_CPU_S": "30"},+ {"UNSLOTH_STUDIO_SANDBOX_NOFILE": str(2**40)},+ ],+)+def test_limit_overrides_match_the_preexec(tmp_path, monkeypatch, env):+ for name, value in env.items():+ monkeypatch.setenv(name, value)+ shimmed, _, kwargs = _report(tmp_path)+ assert "preexec_fn" not in kwargs+ monkeypatch.setattr(tools, "_sandbox_shims", None)+ reference, _, _ = _report(tmp_path)+ assert shimmed["limits"] == reference["limits"]+++def test_a_hard_limit_it_could_only_raise_with_privilege_keeps_the_preexec(tmp_path, monkeypatch):+ import resource++ monkeypatch.setattr(tools, "_has_cap_sys_resource", lambda: True)+ hard = resource.getrlimit(resource.RLIMIT_NOFILE)[1]+ monkeypatch.setenv("UNSLOTH_STUDIO_SANDBOX_NPROC", str(2**62))+ assert tools._sandbox_rlimit_args() is None+ monkeypatch.setattr(tools, "_has_cap_sys_resource", lambda: False)+ args = tools._sandbox_rlimit_args()+ if resource.getrlimit(resource.RLIMIT_NPROC)[1] != resource.RLIM_INFINITY:+ # Unprivileged, the pre-exec's setrlimit fails and is skipped, so the flag is left out.+ assert not any(arg.startswith("--nproc=") for arg in args)+ assert f"--nofile={min(16384, hard)}:{min(16384, hard)}" in args+++def test_missing_shims_keep_the_preexec(tmp_path, monkeypatch):+ monkeypatch.setattr(tools, "_sandbox_shims", None)+ _, prepared, kwargs = _report(tmp_path)+ assert kwargs["preexec_fn"] is tools._sandbox_preexec+ assert prepared.argv[0] == sys.executable+++def test_an_untrusted_shim_is_not_used(tmp_path, monkeypatch):+ fake = tmp_path / "setpriv"+ fake.write_text('#!/bin/sh\nexec "$@"\n')+ fake.chmod(0o755)+ monkeypatch.setattr(tools, "_SANDBOX_SHIM_DIRS", (str(tmp_path),))+ # Not root-owned (the test does not run as root), so it never stands in for the system binary.+ if os.geteuid() != 0:+ assert tools._trusted_system_binary("setpriv") is None+++def test_a_composed_backend_preexec_is_left_alone(tmp_path):+ def composed():+ tools._sandbox_preexec()++ prepared = os_sandbox.PreparedSandboxLaunch(+ argv = (sys.executable, "-c", "pass"),+ workdir = str(tmp_path),+ env = {},+ preexec_fn = composed,+ backend = "bubblewrap",+ )+ kwargs = tools._apply_prepared_launch(prepared, {})+ assert kwargs["preexec_fn"] is composed+ assert prepared.argv[0] == sys.executable+++@_needs_shims+def test_a_missing_program_still_raises_from_popen(tmp_path):+ with pytest.raises(FileNotFoundError):+ _launch(tmp_path, ["/nonexistent/studio-tool"])+++@_needs_shims+def test_the_death_signal_is_armed_before_the_launch_returns(tmp_path):+ """The forking thread exits right after the launch: the pre-exec armed the signal before Popen returned, so+ the child dies with that thread, and the shim launch must not leave a window where it does not."""+ holder = {}++ def launch():+ holder["proc"] = _launch(tmp_path, [sys.executable, "-c", "import time; time.sleep(60)"])[0]++ worker = threading.Thread(target = launch)+ worker.start()+ worker.join()+ proc = holder["proc"]+ try:+ assert _gone(proc.pid)+ finally:+ tools._kill_process_tree(proc)+ proc.wait()+++@_needs_shims+def test_the_tool_dies_with_a_killed_server(tmp_path):+ backend = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))+ server = textwrap.dedent(+ f"""+ import os, subprocess, sys, time+ sys.path.insert(0, {backend!r})+ from core.inference import os_sandbox, tools+ plan = os_sandbox.ToolLaunchPlan(argv=(sys.executable, '-c', 'import time; time.sleep(60)'),+ workdir={str(tmp_path)!r}, env=tools._build_safe_env({str(tmp_path)!r}),+ preexec_fn=tools._sandbox_preexec, requested_mode='software', timeout_seconds=60,+ execution_kind='python')+ prepared = tools._prepare_tool_launch(plan)+ kw = tools._apply_prepared_launch(prepared, dict(stdin=subprocess.DEVNULL, stdout=subprocess.DEVNULL))+ assert 'preexec_fn' not in kw+ print(os_sandbox.spawn_prepared_launch(prepared, **kw).pid, flush=True)+ time.sleep(60)+ """+ )+ parent = subprocess.Popen(+ [sys.executable, "-c", server],+ stdout = subprocess.PIPE,+ text = True,+ start_new_session = True,+ cwd = backend,+ )+ try:+ tool_pid = int(parent.stdout.readline())+ assert _alive(tool_pid)+ parent.kill()+ parent.wait()+ assert _gone(tool_pid), "the tool outlived the server that launched it"+ finally:+ parent.kill()+ parent.wait()+ parent.stdout.close()+++@_needs_shims+def test_kill_tree_takes_the_whole_group(tmp_path):+ proc, _, _ = _launch(tmp_path, ["bash", "-c", "sleep 100 & sleep 100 & echo $(jobs -p); wait"])+ children = [int(pid) for pid in proc.stdout.readline().split()]+ assert children and all(os.getpgid(pid) == proc.pid for pid in children)+ tools._kill_process_tree(proc)+ proc.wait()+ proc.stdout.close()+ assert all(_gone(pid) for pid in (proc.pid, *children))+++def test_the_readiness_wait_outlasts_a_slow_setpriv(monkeypatch):+ """Popen with the pre-exec returned only once the signal was armed, however long that took."""+ clock = [0.0]+ reads = []++ def readlink(path):+ reads.append(path)+ return "/usr/bin/setpriv" if len(reads) <= 5 else "/usr/bin/prlimit"++ def sleep(seconds):+ clock[0] += 1.0++ monkeypatch.setattr(tools.os, "readlink", readlink)+ monkeypatch.setattr(tools.time, "monotonic", lambda: clock[0])+ monkeypatch.setattr(tools.time, "sleep", sleep)+ tools._wait_for_death_signal(12345, "/usr/bin/setpriv")+ assert len(reads) == 6, "returned while setpriv had not armed the signal yet"+++def test_an_unreadable_shim_is_not_used(tmp_path, monkeypatch):+ """An unreadable binary runs non-dumpable, so /proc/<pid>/exe of a live setpriv would fail and pass for exit."""+ fake = tmp_path / "setpriv"+ fake.write_bytes(b"")+ real_stat = os.stat+ mode = [0o100711]++ def stat(path, *args, **kwargs):+ st = real_stat(path, *args, **kwargs)+ if str(path) != str(fake):+ return st+ fields = list(st)+ fields[0], fields[4] = mode[0], 0 # st_mode, st_uid+ return os.stat_result(fields)++ monkeypatch.setattr(tools, "_SANDBOX_SHIM_DIRS", (str(tmp_path),))+ monkeypatch.setattr(tools.os, "stat", stat)+ assert tools._trusted_system_binary("setpriv") is None+ mode[0] = 0o100755+ assert tools._trusted_system_binary("setpriv") == str(fake)+++def test_nofile_above_nr_open_keeps_the_preexec(monkeypatch):+ monkeypatch.setattr(tools, "_has_cap_sys_resource", lambda: False)+ monkeypatch.setattr(tools, "_sandbox_nr_open", lambda: 64)+ monkeypatch.setenv("UNSLOTH_STUDIO_SANDBOX_NOFILE", "16384")+ import resource++ if resource.getrlimit(resource.RLIMIT_NOFILE)[1] > 64:+ assert tools._sandbox_rlimit_args() is None+++def test_a_failed_limit_query_keeps_the_preexec(monkeypatch):+ import resource++ real = tools._resource.getrlimit++ def getrlimit(resource_id):+ if resource_id == resource.RLIMIT_FSIZE:+ raise OSError("refused")+ return real(resource_id)++ monkeypatch.setattr(tools._resource, "getrlimit", getrlimit)+ assert tools._sandbox_rlimit_args() is None++[email protected](sys.version_info < (3, 10), reason = "Python 3.9 keeps the pre-exec")+def test_no_limit_flags_keeps_the_preexec(tmp_path, monkeypatch):+ """prlimit without a flag prints its limits table on a terminal before running the program."""+ monkeypatch.setattr(tools, "_sandbox_shims", ("/usr/bin/setpriv", "/usr/bin/prlimit"))+ monkeypatch.setattr(tools, "_sandbox_rlimit_args", lambda: [])+ env = tools._build_safe_env(str(tmp_path))+ assert tools._sandbox_shim_argv([sys.executable, "-c", "pass"], env) is None+++def test_the_probe_sets_the_limits_a_launch_sets(monkeypatch):+ """An unchanged hard limit skips the LSM setrlimit check, so probing the current CPU limit proves nothing."""+ seen = []+ dump = b"no_new_privs: 1\nParent death signal: KILL\n"++ def run(argv, **kwargs):+ seen.append(list(argv))+ return subprocess.CompletedProcess(argv, 0, dump, b"")++ monkeypatch.setattr(tools, "_trusted_system_binary", lambda name: "/usr/bin/" + name)+ monkeypatch.setattr(tools.subprocess, "run", run)+ limits = tools._sandbox_rlimit_args()+ if not limits:+ pytest.skip("no limit flag applies on this host")+ assert tools._probe_sandbox_shims() == ("/usr/bin/setpriv", "/usr/bin/prlimit")+ start = seen[0].index("/usr/bin/prlimit") + 1+ assert seen[0][start : seen[0].index("--", start)] == limitsdiff --git a/studio/backend/tests/test_tool_sandbox_wiring.py b/studio/backend/tests/test_tool_sandbox_wiring.pyindex 014b320f414..e463d0f9872 100644--- a/studio/backend/tests/test_tool_sandbox_wiring.py+++ b/studio/backend/tests/test_tool_sandbox_wiring.py@@ -226,7 +226,7 @@ def test_the_process_unsloth_holds_still_lands_in_its_own_session():real = subprocess.Popendef capture(argv, **kwargs):- seen.append((argv, kwargs.get("preexec_fn")))+ seen.append((argv, kwargs.get("preexec_fn"), kwargs.get("start_new_session", False)))return real(argv, **kwargs)subprocess.Popen = capture@@ -235,11 +235,21 @@ def test_the_process_unsloth_holds_still_lands_in_its_own_session():assert "6" in tools._bash_exec("echo 6", None, 60, _SESSION)finally:subprocess.Popen = real- launches = [preexec for _, preexec in seen if preexec is not None]- bookkeeping = [argv for argv, preexec in seen if preexec is None]- assert len(launches) == 2, [argv for argv, _ in seen]+ launches = [+ (argv, preexec) for argv, preexec, new_session in seen if preexec is not None or new_session+ ]+ bookkeeping = [+ argv for argv, preexec, new_session in seen if preexec is None and not new_session+ ]+ assert len(launches) == 2, [entry[0] for entry in seen]assert all(tuple(argv)[:1] == ("ps",) for argv in bookkeeping), bookkeeping- seen = launches+ seen = []+ for argv, preexec in launches:+ if preexec is None:+ # No Python pre-exec: Popen runs setsid itself, then setpriv and prlimit exec in place.+ assert os.path.basename(argv[0]) == "setpriv", argv+ continue+ seen.append(preexec)for preexec in seen:read_fd, write_fd = os.pipe()child = os.fork()