Skip to content

Sandbox blocks asyncio self-pipe wakeups: call_soon_threadsafe/to_thread/anyio hang with PermissionError (EPERM) #15053

Open

Description

@outlook84

What version of Codex CLI is running?

codex-cli 0.115.0

What subscription do you have?

Team

Which model were you using?

gpt-5.4-medium,gpt-5.4-xhigh

What platform is your computer?

Linux 6.6.87.2-microsoft-standard-WSL2 x86_64 unknown

What terminal emulator and version are you using (if applicable)?

Google Antigravity

What issue are you seeing?

Summary

I found what looks like a Codex sandbox issue.

Inside the Codex sandbox, Python async code that relies on cross-thread event loop wakeups
can hang or time out. This affects:

  • asyncio.call_soon_threadsafe(...)
  • asyncio.to_thread(...)
  • loop.run_in_executor(None, ...)
  • anyio.to_thread.run_sync(...)
  • anyio.Path(...).exists() and similar helpers that offload sync work to threads

Outside the sandbox, the same commands complete successfully.

Environment

  • Codex CLI sandbox
  • Reproduced on March 18, 2026
  • Project Python: .venv/bin/python3
  • Python version: 3.13
  • pytest: 9.0.2
  • pytest-asyncio: 1.3.0
  • anyio: 4.12.1

What steps can reproduce the bug?

Reproduction

I used this command in a repo only as a probe harness:

.venv/bin/python3 scripts/probe_async_executor.py --timeout 2

probe_async_executor.py

Inside the sandbox, the probe showed this pattern:

  • asyncio.to_thread(...): timeout
  • loop.run_in_executor(None, ...): timeout
  • anyio.to_thread.run_sync(...): timeout
  • anyio.Path(...).exists(): timeout
  • custom ThreadPoolExecutor(...) cases: success

Outside the sandbox, the exact same command completed successfully and all probe cases
passed.

Minimal repro

This hangs inside the sandbox:

import asyncio
import threading

def cb(loop):
    print("thread:call_soon_threadsafe", flush=True)
    loop.call_soon_threadsafe(lambda: (print("loop:callback", flush=True), loop.stop()))

loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
threading.Timer(0.5, cb, args=(loop,)).start()
loop.run_forever()
print("done", flush=True)
loop.close()

Expected output:

thread:call_soon_threadsafe
loop:callback
done

Actual behavior inside the sandbox:

  • only thread:call_soon_threadsafe prints
  • the event loop never wakes up
  • the process hangs until killed / times out

Root cause evidence

With PYTHONASYNCIODEBUG=1, asyncio logs the real failure:

DEBUG:asyncio:Fail to write a null byte into the self-pipe socket
Traceback (most recent call last):
File "/usr/lib/python3.13/asyncio/selector_events.py", line 152, in _write_to_self
csock.send(b'\0')
PermissionError: [Errno 1] Operation not permitted

This appears to break the Unix selector event loop wakeup mechanism used by
call_soon_threadsafe().

Relevant stdlib path:

  • asyncio/selector_events.py, _write_to_self() uses csock.send(b'\0')
  • asyncio/base_events.py, shutdown_default_executor() depends on call_soon_threadsafe(...)

That explains why some asyncio.to_thread(...) work appears to finish but asyncio.run(...)
still hangs during shutdown of the default executor.

Extra confirmation

I also verified that this is not specific to the repo or virtualenv startup hooks:

  • reproduces with plain python3
  • reproduces with python3 -S
  • does not require importing any project code
  • disappears when rerun outside the Codex sandbox

I also tested lower-level primitives:

  • cross-thread socket.socketpair().send(...) fails with PermissionError: [Errno 1] Operation
    not permitted
  • cross-thread os.pipe() write works

So this looks specifically related to the sandbox blocking the socketpair-based wakeup path
used by asyncio on Unix.

What is the expected behavior?

Expected behavior

Codex sandboxed Python processes should be able to use standard-library asyncio thread
wakeups correctly, or the sandbox should detect and clearly report that this runtime pattern
is unsupported.

Actual behavior

The sandbox causes false async failures that look like application bugs:

  • hangs in asyncio.to_thread(...)
  • hangs in anyio.to_thread.run_sync(...)
  • hangs during shutdown_default_executor()
  • misleading timeouts in pytest and repo diagnostics

Additional information

No response

Activity

  1. added
    bug
    on Mar 18, 2026
  2. added
    CLI
    sandbox
    on Mar 18, 2026
  3. removed
    CLI
    on Mar 18, 2026
  4. vshikhar95 commented on Apr 4, 2026

    @vshikhar95

    Confirming this issue also explains a pytest symptom I reproduced locally.

    In my repo, Codex could run backend pytest directly in the foreground inside the sandbox, but the same sandbox still caused pytest hangs in other execution paths. Outside the sandbox, the same pytest runs completed normally.

    So this looks consistent with the underlying asyncio cross-thread wakeup bug described here, with pytest hangs as one practical manifestation.

  5. ned2 commented on Aug 30, 2026

    @ned2

    I can reproduce this on a current Codex/Python stack:

    • Codex CLI 0.151.0
    • Linux 7.0.0-30-generic x86_64
    • Python 3.14.7
    • pytest 9.1.1
    • pytest-asyncio 1.4.0
    • Textual 8.2.8
    • workspace-write sandbox

    End-to-end reproduction

    This probe uses only the Python standard library. Textual, pytest, and the application are not required.

    Create the standard-library-only probe:

    cat >/tmp/codex-asyncio-sandbox-repro.py <<'PY'
    import asyncio
    
    async def main() -> None:
        await asyncio.to_thread(print, "worker completed", flush=True)
    
    asyncio.run(main())
    print("closed", flush=True)
    PY

    First, run it directly as the host control:

    timeout 10s .venv/bin/python /tmp/codex-asyncio-sandbox-repro.py
    

    Expected output:

    worker completed
    closed
    

    Exit code: 0.

    Next, launch the identical command through a Codex workspace-write sandbox with network access disabled:

    codex exec --ephemeral \
      --sandbox workspace-write \
      -c sandbox_workspace_write.network_access=false \
      -C /home/ned/projects/dashpot \
      "Run this exact command without requesting escalation and report stdout and its exit code: timeout 10s .venv/bin/python /tmp/codex-asyncio-sandbox-repro.py"

    Codex report reports: sandbox: workspace-write [workdir, /tmp, $TMPDIR]

    The command then produces:

    worker completed
    

    Exit code: 124.

    worker completed proves that the executor task ran. The missing closed output and exit code 124 show that asyncio.run() did not complete its default-executor shutdown before the ten-second outer timeout.

    For comparison, repeating the codex exec command with:

    -c sandbox_workspace_write.network_access=true
    

    produces:

    worker completed
    closed
    

    and exits 0.

    This isolates the failure to the Codex Linux sandbox policy used when network access is disabled, rather than Python, the virtual environment, or the application under test.

    Lower-level confirmation

    Inside the affected sandbox, an AF_UNIX socketpair write fails directly:

    import socket
    
    writer, reader = socket.socketpair()
    writer.send(b"x")

    Inside the sandbox, send() raises: PermissionError: [Errno 1] Operation not permitted

    This is the mechanism asyncio uses to wake its Unix selector after call_soon_threadsafe() is invoked from another thread.

    I also tested the minimal asyncio.to_thread() probe with several installed Python versions inside the same affected sandbox:

    • Python 3.11: exits normally
    • Python 3.12: hangs
    • Python 3.13: hangs
    • Python 3.14.6: hangs
    • Python 3.14.7: hangs

    Scheduling an otherwise unnecessary event-loop timer also allows shutdown to complete. That is consistent with the completion callback already being queued but the selector not being awakened by the cross-thread socketpair write.

    Real-world impact

    I encountered this through a Textual pytest suite. Textual's Markdown renderer submits MarkdownIt.parse to asyncio's default executor.

    The affected test printed PASSED, but teardown then remained in:

    asyncio.runners.Runner.close
    loop.shutdown_default_executor(constants.THREAD_JOIN_TIMEOUT)
    

    Python waited for its 300-second executor shutdown timeout. The complete suite finished in approximately 21 seconds outside the affected sandbox but incurred repeated five-minute pauses inside it. Textual only exposed the behavior; it is not needed to reproduce it.

    This appears to be the same underlying seccomp / sendto() problem investigated in #24933 rather than an application, Textual, pytest, or CPython defect.

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

    bugperformancesandbox

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions