Skip to content

Windows sandbox blocks pytest-xdist temp dirs created with Python 3.14/pytest 0o700 permissions #19791

Open

Description

@shauneccles

What version of Codex CLI is running?

codex-cli 0.125.0

Platform

Native Windows, PowerShell shell tool, workspace-write sandbox. Workspace is on D:\Data\backlog-docgen.

Python/test stack:

  • Python 3.14.3 (MSC v.1944 64 bit (AMD64))
  • pytest 9.0.2
  • pytest-xdist 3.8.0
  • pytest configured with -n auto --dist loadfile

Issue

Inside the Codex Windows sandbox, pytest-xdist fails when pytest creates temporary directories using its default private directory mode (0o700). The failures are Windows PermissionError: [WinError 5] Access is denied when pytest/xdist tries to reopen or scan temp directories that the same test run just created.

This looks like an interaction between:

  1. Python/pytest creating temp directories with restrictive permissions (mode=0o700), including tmp_path, tmp_path_factory, and xdist worker basetemps.
  2. Codex's Windows sandbox/restricted token/ACL model not being able to access those owner-only directories after creation.
  3. xdist multiplying the problem because controller and workers exchange temp roots such as pytest-of-shaun\pytest-0\popen-gw*.

Minimal reproduction shape

In a Python project with pytest-xdist installed and default xdist config:

[tool.pytest.ini_options]
addopts = """
    -ra
    -q
    -n auto
    --dist loadfile
    --maxfail=3
    --tb=short
    --no-header
"""

Run inside the Codex Windows sandbox:

uv run pytest tests/test_active_backlogs_current_schema.py -q

Representative failures seen while investigating:

INTERNALERROR> PermissionError: [WinError 5] Access is denied: 'C:\Users\shaun\AppData\Local\Temp\pytest-of-shaun'

After forcing temp roots into the workspace with PYTEST_DEBUG_TEMPROOT, failures moved to workspace-local pytest dirs:

INTERNALERROR> PermissionError: [WinError 5] Access is denied: 'D:\Data\backlog-docgen\.pytest-tmp\run-...\pytest-of-shaun'

With xdist worker basetemps:

PermissionError: [WinError 5] Access is denied: 'D:\Data\backlog-docgen\.pytest-tmp\run-...\pytest-of-shaun\pytest-0\popen-gw1'

With tests that use tmp_path/tmp_path_factory:

PermissionError: [WinError 5] Access is denied: 'D:\Data\backlog-docgen\.pytest-tmp\run-...\pytest-of-shaun\pytest-0\test_import_no_changes0\test-backlog'

With app code using tempfile.TemporaryDirectory(), the underlying PermissionError was converted by the FastAPI app into 403 responses.

Workarounds tested

These did not solve the underlying issue:

  • serial pytest only (-n 0) avoids xdist but is not practical for larger suites
  • fixed --basetemp=pytest-temp / --basetemp=tmp_pytest eventually becomes inaccessible after aborted or repeated runs
  • PYTEST_DEBUG_TEMPROOT alone keeps dirs in the workspace but still creates 0o700 dirs that the sandbox cannot reopen
  • unique per-run temp roots avoid name reuse but still fail once pytest/xdist creates nested private dirs

The only successful in-repo workaround so far is very invasive: monkeypatching temp directory creation during tests so directories under the pytest temp root use sandbox-readable permissions, and patching xdist so workers do not receive controller-created fixed popen-gw* basetemps. That required touching Path.mkdir, os.mkdir, TMP/TEMP/TMPDIR, PYTEST_DEBUG_TEMPROOT, and xdist.workermanage.WorkerController.setup from tests/conftest.py.

That workaround is a strong smell; normal Python projects should not need to patch os.mkdir to run pytest in Codex.

Related issues found

I searched openai/codex issues for:

  • pytest xdist PermissionError 0o700 Windows sandbox
  • pytest-of-shaun popen-gw PermissionError Windows sandbox
  • PYTEST_DEBUG_TEMPROOT pytest-of-shaun Windows sandbox
  • 0o700 Windows sandbox ACL Python tempfile

I did not find this exact issue.

Closest related Codex issues I found:

Related pytest issue:

Question

Is this expected behavior for the Windows sandbox? If yes, is there an intended way for common Python test tooling that creates 0o700 temp dirs (pytest/tmp_path/tmp_path_factory/tempfile + pytest-xdist workers) to run inside the sandbox without disabling xdist or broadening execution outside the sandbox?

From the user side, this looks like a very common combination: native Windows + Codex CLI sandbox + Python 3.14 + pytest + pytest-xdist. The expected behavior is that temp directories created by a sandboxed process remain accessible to that process and its sandboxed child worker processes for the duration of the test run.

Activity

  1. added
    bug
    CLI
    windows-os
    sandbox
    on Apr 27, 2026
  2. removed
    windows-os
    CLI
    on Apr 27, 2026
  3. JeppeKlitgaard commented on Jul 16, 2026

    @JeppeKlitgaard

    This is an really frustrating issue to run into. I believe this is also related: #9757

  4. grsrzxgvmpg commented on Sep 26, 2026

    @grsrzxgvmpg

    Additional data point with the underlying mechanism, plus a narrower workaround than patching os.mkdir from conftest.py.

    Environment: native Windows 11, codex-cli 0.153.4, [windows] sandbox = "unelevated", workspace-write, Python 3.13.7 (so not only 3.14), pytest 9.1.0.

    Mechanism. On Windows, Python ≥ 3.13 implements os.mkdir(path, 0o700) (also tempfile.mkdtemp/TemporaryDirectory) by writing a protected DACL containing only OWNER RIGHTS, SYSTEM and Administrators, instead of inheriting the parent's ACL. The parent ACL is what grants CodexSandboxUsers and the per-sandbox SIDs. The unelevated sandbox's restricted token needs those restricting SIDs to pass the access check, so the new directory is unusable even by the sandboxed process that just created it. Python 3.11 still inherits the parent ACL and is unaffected. A directory created with 0o700 outside the sandbox, e.g. %TEMP%\pytest-of-<user> from a normal pytest run, poisons later sandboxed runs the same way.

    Minimal repro without pytest (run from any workspace dir):

    codex sandbox -c 'sandbox_mode="workspace-write"' -- python -c "import os; os.mkdir('d', 0o700); open('d/f.txt','w').write('x')"
    PermissionError: [Errno 13] Permission denied: 'd/f.txt'
    # same command with os.mkdir('d') (default mode) succeeds; Get-Acl shows AreAccessRulesProtected=True for the 0o700 dir
    

    Confirming the earlier report: --basetemp=<workspace dir> and PYTEST_DEBUG_TEMPROOT do not help. pytest still creates the basetemp and every numbered dir with mode=0o700.

    Narrow workaround that works here: a small pytest plugin, installed with a pytest11 entry point, that downgrades 0o700 → default mode only while pytest itself creates its dirs, by wrapping TempPathFactory.getbasetemp, TempPathFactory.mktemp and cacheprovider._make_cachedir. The patch must be re-entrant, because mktemp calls getbasetemp. Code under test is untouched, and its own 0o700 dirs stay protected.

    import contextlib, functools, os, sys
    _REAL_MKDIR = os.mkdir
    
    @contextlib.contextmanager
    def _inherit_acl_mkdir():
        def mkdir(path, mode=0o777, *, dir_fd=None):
            return _REAL_MKDIR(path, 0o777 if mode == 0o700 else mode, dir_fd=dir_fd)
        previous, os.mkdir = os.mkdir, mkdir
        try:
            yield
        finally:
            os.mkdir = previous
    
    def _wrap(fn):
        @functools.wraps(fn)
        def wrapper(*a, **k):
            with _inherit_acl_mkdir():
                return fn(*a, **k)
        wrapper._win_sandbox_tmp = True
        return wrapper
    
    if sys.platform == "win32":
        from _pytest import cacheprovider, tmpdir
        for name in ("getbasetemp", "mktemp"):
            f = getattr(tmpdir.TempPathFactory, name)
            if not getattr(f, "_win_sandbox_tmp", False):
                setattr(tmpdir.TempPathFactory, name, _wrap(f))
        if not getattr(cacheprovider._make_cachedir, "_win_sandbox_tmp", False):
            cacheprovider._make_cachedir = _wrap(cacheprovider._make_cachedir)

    Results inside codex sandbox (workspace-write) after one-time removal of the pre-poisoned pytest-of-<user>:

    Run Without plugin With plugin
    tmp_path + tmp_path_factory test, default temp root (run twice) 1 failed, 1 error 2 passed / 2 passed
    a real ~290-test suite that uses tmp_path heavily 59 passed, 9 failed, 220 errors 278 passed, 10 failed

    The remaining 10 failures are app code calling tempfile.mkdtemp(), which is the same mechanism outside pytest's control. I haven't tested xdist worker basetemps (popen-gw*); they come from getbasetemp on the controller, so I'd expect them to be covered, but that is unverified.

    A durable fix probably belongs on the sandbox side. The goal is for directories a sandboxed process creates with an explicit owner-only DACL to remain usable by that same restricted token, since 0o700 is now common in Python tooling. pytest-dev/pytest#10679 (hardcoded 0o700) is the pytest-side counterpart.

  5. ljx719476856li commented on Oct 8, 2026

    @ljx719476856li

    Related reproduction with desktop package 26.1002.7124.0, bundled codex-cli 0.162.0-alpha.2, Windows build 10.0.26200.9457 and Python 3.12.10. The current desktop shell uses a sandbox-specific temporary directory shaped like %LOCALAPPDATA%\Packages\sandbox.{GUID}\AC\Temp. We have not established that its implementation is identical to the original report's restricted-token backend.

    Minimal Python reproduction inside the managed desktop shell:

    import tempfile
    from pathlib import Path
    with tempfile.TemporaryDirectory() as d:
        p = Path(d) / "probe.txt"
        p.write_text("ok")
        assert p.read_text() == "ok"

    Directory creation succeeds, but writing probe.txt fails with PermissionError / WinError 5. Cleanup also fails with access denied.

    Controls on this installation:

    • Creating, reading and deleting a file directly in TEMP succeeds.
    • A new child directory created with os.mkdir(path, 0o777) supports file write and cleanup.
    • A new child directory created with os.mkdir(path, 0o700) is created but rejects subsequent file write and cleanup.
    • The ordinary TemporaryDirectory lifecycle succeeds when explicitly placed in an allowed workspace directory on this installation. This is a local mitigation, not a universal fix.
    • Restarting the desktop app did not resolve it. A Windows reboot was not tested.

    No pytest or xdist dependency is needed for this reproduction. The independent runtime-sharing-violation report is #51932; we have not demonstrated a causal connection between the two errors.

  6. jm2 commented on Oct 26, 2026

    @jm2

    Confirming this is still present on codex-cli 0.163.0 (unelevated sandbox, workspace-write, windows.sandbox="unelevated"), Windows 11 ARM64 build 26200, Python 3.14.7 arm64. It's not limited to x64 or older builds.

    Minimal repro without pytest, run with codex sandbox -c windows.sandbox="unelevated" -c sandbox_mode="workspace-write" -- python repro.py from a workspace dir:

    outside sandbox inside sandbox
    os.mkdir(p), then write a file in it OK OK
    os.mkdir(p, 0o700), then write a file in it OK PermissionError: [Errno 13]
    tempfile.mkdtemp(), then write a file in it OK PermissionError: [Errno 13]

    In our case it blocks every test that uses tempfile.mkdtemp() at fixture setup, before any assertion runs.

    This isn't specific to 0o700 or to Python. Any kernel object the sandboxed process creates with a DACL that doesn't name one of the token's restricting SIDs is unusable by that same process. I tested named pipes with explicit security descriptors. The sandboxed process creates the pipe, then opens its client end with CreateFileW(GENERIC_WRITE):

    pipe DACL (SDDL) client open inside sandbox
    default (NULL security attributes) ERROR_ACCESS_DENIED (5)
    D:(A;;GA;;;OW) (owner rights only) ERROR_ACCESS_DENIED (5)
    D:(A;;GA;;;OW)(A;;GA;;;RC) (+ RESTRICTED) ERROR_ACCESS_DENIED (5)
    D:(A;;GA;;;WD) (Everyone) OK

    So a protected owner-only ACL (Python ≥ 3.13 mkdir(…, 0o700)) and the token's own default DACL both fail the restricted-SID pass of the access check. The silent ninja build hang is a separate symptom of this, filed as #57122.

    Suggested fix: make objects created by a sandboxed process accessible to its restricting SIDs. For example, set the restricted token's TokenDefaultDacl to include an ACE for the sandbox's capability SID. Ideally, also make the restricting-SID set satisfiable by OWNER RIGHTS / creator-owner ACEs, so owner-only DACLs written by the sandboxed process itself still work. As the earlier comments show, per-tool workarounds (pytest plugins, patching os.mkdir) don't scale.

  7. pskraemer11 commented on Nov 21, 2026

    @pskraemer11

    Additional confirmation (single-process pytest, no xdist required)

    Summary

    This issue also reproduces without pytest-xdist, for plain single-process pytest: the Codex Windows sandbox cannot access Pytest's tmp_path directories (created with mode=0o700), failing with PermissionError: [WinError 5] on the first access, while the same test run succeeds outside the sandbox.

    Environment

    • Windows 11 (Home)
    • Codex Desktop (sandbox mode, workspace-write)
    • Python 3.12, Pytest 9.2.0
    • No xdist involved

    Reproduction

    A minimal suite with a single test that uses tmp_path (e.g. writes a file) fails inside the sandbox with PermissionError: [WinError 5] when the test first touches the freshly created temp directory; the identical run in a regular PowerShell is green.

    Isolated cause

    Reproduced with plain Python, without Pytest:

    • Path.mkdir() without a mode → list/write/delete works in the sandbox.
    • Path.mkdir(mode=0o700) → PermissionError [WinError 5] afterwards on listing, writing, and deleting.

    The ACL of such a directory shows a protected DACL with OWNER RIGHTS, SYSTEM, and Administrators (FullControl). The sandbox token carries Administrators as deny-only in its token information and has no allow ACE that applies after the DACL is replaced. Parent directories, by contrast, keep inherited ACLs with full user control.

    Responsibilities

    Pytest side (not a bug): Pytest creates tmp_path and basetemp directories with mode=0o700 as a secure default. That is intentional behavior, not a Pytest defect. Whether that restriction is even required on Windows is disputable at most.

    Codex side (fix needed here): A process that is allowed to create a folder with restrictive permissions can afterwards no longer access it itself, because the ACL leaves no effective allow ACE for the sandbox token after the DACL is replaced. Standard Pytest suites are therefore unusable inside the sandbox even though they work system-wide, and users cannot configure this per repository. The fix belongs on the sandbox side (token/ACL handling, or broadening the sandbox write scope).

    Workaround (not a solution)

    Per repository, a tests/conftest.py that overrides tmp_path: root under tests/artifacts/ (git-ignored), one folder per test created with .mkdir() without a mode, cleanup with shutil.rmtree. This has to be maintained per repository and does not address the sandbox interaction itself.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugsandbox

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions