Skip to content

The agent that grows with you

fix(test-runner): key the scratch root by user, not a shared literal

ethernet14b3232cbdcc
diff --git a/scripts/run_tests_parallel.py b/scripts/run_tests_parallel.py
index 4d2b0c86a2e..27886f4c850 100644
--- a/scripts/run_tests_parallel.py
+++ b/scripts/run_tests_parallel.py
@@ -64,11 +64,18 @@ def _runner_scratch_root() -> str:
the FHS disk-backed temp root and is used because the alternatives fail tests that assume
the root's shape: under the Hermes home conftest relocates the basetemp; under a dot-dir
(~/.cache) the hidden-dir search tests see every fixture as hidden; anything longer than
- the old /tmp root pushes AF_UNIX test sockets past sun_path."""
+ the old /tmp root pushes AF_UNIX test sockets past sun_path.
+
+ The name is per-USER because a fixed literal in a world-writable sticky dir belongs to
+ whoever created it first: a root-owned root (a container or system-service run) makes every
+ later makedirs/mkdtemp here fail with EPERM for every other user on the host, with no way
+ back that does not need root. Keying by uid means no run is blocked by another's leftovers.
+ """
+ name = "hermes-pytest" + (f"-{os.getuid()}" if hasattr(os, "getuid") else "")
if os.name == "nt" or not os.path.isdir("/var/tmp"): # no-tmp: ok — probing the disk-backed FHS root
- root = os.path.join(tempfile.gettempdir(), "hermes-pytest")
+ root = os.path.join(tempfile.gettempdir(), name)
else:
- root = "/var/tmp/hermes-pytest" # no-tmp: ok — /var/tmp is disk-backed by FHS, never tmpfs
+ root = f"/var/tmp/{name}" # no-tmp: ok — /var/tmp is disk-backed by FHS, never tmpfs
os.makedirs(root, exist_ok=True)
return root
diff --git a/tests/scripts/test_run_tests_parallel.py b/tests/scripts/test_run_tests_parallel.py
index b5438cd1c29..1465a4afad4 100644
--- a/tests/scripts/test_run_tests_parallel.py
+++ b/tests/scripts/test_run_tests_parallel.py
@@ -535,3 +535,33 @@ def test_files_from_dash_reads_the_list_from_stdin(tmp_path: Path) -> None:
assert proc.returncode == 0, proc.stdout
assert "Running 1 test files" in proc.stdout, proc.stdout
assert "✓2" in proc.stdout or "2 passed" in proc.stdout, proc.stdout
+
+
+def test_scratch_root_is_per_user(tmp_path: Path, monkeypatch) -> None:
+ """Two users on one host must never share the runner's scratch root.
+
+ A fixed literal in a world-writable sticky dir belongs to whoever created it first: a
+ root-owned root (a container or system-service run) makes every later ``mkdtemp`` there
+ fail with EPERM for every other user, with no non-root way back.
+ """
+ import importlib
+
+ scripts_dir = Path(__file__).resolve().parents[2] / "scripts"
+ monkeypatch.syspath_prepend(str(scripts_dir))
+ runner = importlib.import_module("run_tests_parallel")
+
+ # Exercise the non-/var/tmp arm so the probe never mints roots in the real shared dir.
+ # (Narrow: on 3.14 ``Path.is_dir()`` itself goes through ``os.path.isdir``.)
+ monkeypatch.setattr(runner.tempfile, "gettempdir", lambda: str(tmp_path))
+ real_isdir = os.path.isdir
+ monkeypatch.setattr(runner.os.path, "isdir",
+ lambda path: False if str(path) == "/var/tmp" else real_isdir(path))
+
+ monkeypatch.setattr(runner.os, "getuid", lambda: 1000)
+ mine = Path(runner._runner_scratch_root())
+ monkeypatch.setattr(runner.os, "getuid", lambda: 0)
+ theirs = Path(runner._runner_scratch_root())
+
+ assert mine != theirs
+ assert mine.is_dir() and theirs.is_dir()
+ assert mine.parent == tmp_path and theirs.parent == tmp_path