Skip to content

sandbox: seccomp sendto deny silently breaks Python asyncio cross-thread primitives #24933

Open

Description

@rjhuijsman

What version of Codex CLI is running?

codex-cli 0.134.0

What subscription do you have?

Plus

Which model were you using?

gpt-5.5

What platform is your computer?

Linux 6.8.0-1052-azure x86_64 x86_64

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

VSCode, via ssh

Codex doctor report

{
  "schemaVersion": 1,
  "generatedAt": "1779981391s since unix epoch",
  "overallStatus": "ok",
  "codexVersion": "0.134.0",
  "checks": {
    "app_server.status": {
      "id": "app_server.status",
      "category": "app-server",
      "status": "ok",
      "summary": "background server is not running",
      "details": {
        "control socket": "/home/vscode/.codex/app-server-control/app-server-control.sock",
        "daemon state dir": "/home/vscode/.codex/app-server-daemon",
        "mode": "ephemeral",
        "pid file": "/home/vscode/.codex/app-server-daemon/app-server.pid (missing)",
        "settings": "/home/vscode/.codex/app-server-daemon/settings.json (missing)",
        "status": "not running",
        "update-loop pid file": "/home/vscode/.codex/app-server-daemon/app-server-updater.pid (missing)"
      },
      "remediation": null,
      "durationMs": 86
    },
    "auth.credentials": {
      "id": "auth.credentials",
      "category": "auth",
      "status": "ok",
      "summary": "auth is configured",
      "details": {
        "auth env vars present": "OPENAI_API_KEY",
        "auth file": "/home/vscode/.codex/auth.json",
        "auth storage mode": "File",
        "stored API key": "false",
        "stored ChatGPT tokens": "true",
        "stored agent identity": "false",
        "stored auth mode": "chatgpt"
      },
      "remediation": null,
      "durationMs": 0
    },
    "config.load": {
      "id": "config.load",
      "category": "config",
      "status": "ok",
      "summary": "config loaded",
      "details": {
        "CODEX_HOME": "/home/vscode/.codex",
        "config.toml": "/home/vscode/.codex/config.toml",
        "config.toml parse": "ok",
        "cwd": "/workspaces/mono",
        "enabled feature flags": "shell_tool, unified_exec, shell_snapshot, terminal_resize_reflow, sqlite, hooks, enable_request_compression, multi_agent, apps, tool_suggest, plugins, in_app_browser, browser_use, browser_use_external, computer_use, plugin_sharing, image_generation, skill_mcp_dependency_install, steer, guardian_approval, goals, collaboration_modes, tool_call_mcp_elicitation, personality, fast_mode, tui_app_server, workspace_dependencies",
        "feature flag overrides": "none",
        "feature flags enabled": "27",
        "log dir": "/home/vscode/.codex/log",
        "mcp servers": "0",
        "model": "gpt-5.5",
        "model provider": "openai",
        "sqlite home": "/home/vscode/.codex"
      },
      "remediation": null,
      "durationMs": 0
    },
    "installation": {
      "id": "installation",
      "category": "install",
      "status": "ok",
      "summary": "installation looks consistent",
      "details": {
        "PATH codex #1": "/usr/bin/codex",
        "PATH codex #2": "/bin/codex",
        "PATH codex entries": "2",
        "current executable": "/usr/lib/node_modules/@openai/codex/node_modules/@openai/codex-linux-x64/vendor/x86_64-unknown-linux-musl/bin/codex",
        "install context": "npm (package /usr/lib/node_modules/@openai/codex/node_modules/@openai/codex-linux-x64/vendor/x86_64-unknown-linux-musl, bin /usr/lib/node_modules/@openai/codex/node_modules/@openai/codex-linux-x64/vendor/x86_64-unknown-linux-musl/bin, resources /usr/lib/node_modules/@openai/codex/node_modules/@openai/codex-linux-x64/vendor/x86_64-unknown-linux-musl/codex-resources, path /usr/lib/node_modules/@openai/codex/node_modules/@openai/codex-linux-x64/vendor/x86_64-unknown-linux-musl/codex-path)",
        "managed by bun": "false",
        "managed by npm": "true",
        "managed package root": "/usr/lib/node_modules/@openai/codex",
        "npm update target": "/usr/lib/node_modules/@openai/codex"
      },
      "remediation": null,
      "durationMs": 175
    },
    "mcp.config": {
      "id": "mcp.config",
      "category": "mcp",
      "status": "ok",
      "summary": "no MCP servers configured",
      "details": {},
      "remediation": null,
      "durationMs": 0
    },
    "network.env": {
      "id": "network.env",
      "category": "network",
      "status": "ok",
      "summary": "network-related environment looks readable",
      "details": {
        "proxy env vars": "none"
      },
      "remediation": null,
      "durationMs": 0
    },
    "network.provider_reachability": {
      "id": "network.provider_reachability",
      "category": "reachability",
      "status": "ok",
      "summary": "active provider endpoints are reachable over HTTP",
      "details": {
        "openai API base URL": "https://api.openai.com/v1 reachable (HTTP 404)",
        "openai API route probe": "https://api.openai.com/v1/<redacted> route exists (HTTP 401)",
        "reachability mode": "API key auth"
      },
      "remediation": null,
      "durationMs": 596
    },
    "network.websocket_reachability": {
      "id": "network.websocket_reachability",
      "category": "websocket",
      "status": "ok",
      "summary": "Responses WebSocket handshake succeeded",
      "details": {
        "DNS": "2 IPv4, 2 IPv6, first IPv4",
        "auth mode": "chatgpt",
        "connect timeout": "15000 ms",
        "endpoint": "wss://chatgpt.com/backend-api/<redacted>",
        "handshake result": "HTTP 101 Switching Protocols",
        "model provider": "openai",
        "models etag present": "true",
        "provider name": "OpenAI",
        "proxy env vars": "none",
        "reasoning header": "false",
        "server model present": "false",
        "supports websockets": "true",
        "wire API": "responses"
      },
      "remediation": null,
      "durationMs": 1929
    },
    "runtime.provenance": {
      "id": "runtime.provenance",
      "category": "runtime",
      "status": "ok",
      "summary": "running npm on linux-x86_64",
      "details": {
        "commit": "unknown",
        "current executable": "/usr/lib/node_modules/@openai/codex/node_modules/@openai/codex-linux-x64/vendor/x86_64-unknown-linux-musl/bin/codex",
        "install method": "npm (package /usr/lib/node_modules/@openai/codex/node_modules/@openai/codex-linux-x64/vendor/x86_64-unknown-linux-musl, bin /usr/lib/node_modules/@openai/codex/node_modules/@openai/codex-linux-x64/vendor/x86_64-unknown-linux-musl/bin, resources /usr/lib/node_modules/@openai/codex/node_modules/@openai/codex-linux-x64/vendor/x86_64-unknown-linux-musl/codex-resources, path /usr/lib/node_modules/@openai/codex/node_modules/@openai/codex-linux-x64/vendor/x86_64-unknown-linux-musl/codex-path)",
        "platform": "linux-x86_64",
        "version": "0.134.0"
      },
      "remediation": null,
      "durationMs": 0
    },
    "runtime.search": {
      "id": "runtime.search",
      "category": "search",
      "status": "ok",
      "summary": "search is OK (bundled)",
      "details": {
        "search command": "/usr/lib/node_modules/@openai/codex/node_modules/@openai/codex-linux-x64/vendor/x86_64-unknown-linux-musl/codex-path/rg",
        "search command readiness": "file exists",
        "search provider": "bundled"
      },
      "remediation": null,
      "durationMs": 0
    },
    "sandbox.helpers": {
      "id": "sandbox.helpers",
      "category": "sandbox",
      "status": "ok",
      "summary": "sandbox configuration is readable",
      "details": {
        "approval policy": "OnRequest",
        "codex-linux-sandbox helper": "/home/vscode/.codex/tmp/arg0/codex-arg0icbf9u/codex-linux-sandbox",
        "execve wrapper helper": "/home/vscode/.codex/tmp/arg0/codex-arg0icbf9u/codex-execve-wrapper",
        "filesystem sandbox": "restricted",
        "network sandbox": "restricted"
      },
      "remediation": null,
      "durationMs": 0
    },
    "state.paths": {
      "id": "state.paths",
      "category": "state",
      "status": "ok",
      "summary": "state paths and databases are inspectable",
      "details": {
        "CODEX_HOME": "/home/vscode/.codex (dir)",
        "active rollout files": "5 files, 34188495 total bytes, 6837699 average bytes",
        "archived rollout files": "0 files, 0 total bytes, 0 average bytes",
        "goals DB": "/home/vscode/.codex/goals_1.sqlite (file)",
        "goals DB integrity": "ok",
        "log DB": "/home/vscode/.codex/logs_2.sqlite (file)",
        "log DB integrity": "ok",
        "log dir": "/home/vscode/.codex/log (missing)",
        "sqlite home": "/home/vscode/.codex (dir)",
        "state DB": "/home/vscode/.codex/state_5.sqlite (file)",
        "state DB integrity": "ok"
      },
      "remediation": null,
      "durationMs": 2785
    },
    "terminal.env": {
      "id": "terminal.env",
      "category": "terminal",
      "status": "ok",
      "summary": "terminal metadata was detected",
      "details": {
        "COLORTERM": "truecolor",
        "TERM_PROGRAM": "vscode",
        "VSCODE_IPC_HOOK_CLI": "present",
        "color output": "enabled",
        "effective locale": "en_US.UTF-8",
        "stderr is terminal": "true",
        "stdin is terminal": "true",
        "stdout is terminal": "true",
        "terminal": "VS Code",
        "terminal size": "254x58",
        "terminal version": "1.121.0"
      },
      "remediation": null,
      "durationMs": 0
    },
    "updates.status": {
      "id": "updates.status",
      "category": "updates",
      "status": "ok",
      "summary": "update configuration is locally consistent",
      "details": {
        "cached latest version": "0.134.0",
        "check for update on startup": "true",
        "last checked at": "2026-05-28T11:07:49.777753047Z",
        "latest version": "0.134.0",
        "latest version status": "current version is not older",
        "npm update target": "/usr/lib/node_modules/@openai/codex",
        "update action": "npm install -g @openai/codex",
        "version cache": "/home/vscode/.codex/version.json"
      },
      "remediation": null,
      "durationMs": 398
    }
  }
}

What issue are you seeing?

Inside Codex's Linux sandbox (any sandbox_mode other than danger-full-access), Python's asyncio.AbstractEventLoop.call_soon_threadsafe(callback) from a background thread silently fails to wake the asyncio selector. The callback is queued in the loop's ready queue but epoll_wait does not return to run it. The callback only ever runs as a side effect of some other event waking the selector — most commonly, the next scheduled timer firing (any call_later, asyncio.sleep, or wait_for(timeout=…)).

The other events makes the bug non-obvious. It is not a clean "call_soon_threadsafe hangs forever"; it is "call_soon_threadsafe takes as long as the next unrelated timer in the loop". A program that has, say, a wait_for(coro, timeout=30) in flight will see every cross-thread interaction silently stretched to up to 30 s instead of completing in milliseconds — and wait_for will still report success when its inner coroutine eventually completes inside the deadline, so nothing throws and nothing logs.

Because call_soon_threadsafe is Python's only way to deliver an event to an asyncio loop from another OS thread, every asyncio primitive that crosses a thread boundary is silently broken:

  • loop.run_in_executor(executor, fn) resolves its Future via call_soon_threadsafe(future.set_result, …) from the worker thread. After the first call (which can succeed by accident — see "Why the first call works" below), every subsequent call hangs.
  • asyncio.Event.set() called from a background Python thread.
  • asyncio.Future.set_result() called from a background Python thread.
  • asyncio.to_thread(fn) (Python 3.9+, the modern wrapper around
    run_in_executor).
  • Any Python library that bridges blocking I/O to asyncio via a thread pool (httpx's sync transports under async, asyncpg's blocking fallbacks, sync database drivers wrapped by anyio, filesystem access via aiofiles, …).

The bug is silent — no log, no exception, no warning. To the Python program it looks like the kernel just isn't delivering the wakeup.

My agent claims the root cause is the blocking of the sendto syscall by the Codex sandbox:

    // https://github.com/openai/codex/blob/main/codex-rs/linux-sandbox/src/landlock.rs

    deny_syscall(&mut rules, libc::SYS_sendto);
    deny_syscall(&mut rules, libc::SYS_sendmmsg);
    // ... no AF_UNIX exception, unlike SYS_socket/SYS_socketpair below.

What steps can reproduce the bug?

Run the following Python program first outside the codex CLI, then inside it:

#!/usr/bin/env python3
"""
Minimal repro: asyncio's cross-thread wakeup (`call_soon_threadsafe`)
is silently broken inside the OpenAI Codex CLI's Linux sandbox. As a
direct consequence, every higher-level asyncio primitive that relies
on this — `run_in_executor`, threaded `Event.set`, `Future.set_result`
from a worker thread, the default child watcher's SIGCHLD path,
and `asyncio.run()`'s OWN cleanup — silently stalls until some
unrelated timer in the loop happens to fire and incidentally drain
the ready queue.

The subtle part: the wakeup is not "lost forever", it's "lost until
the next timer". So programs that already had a `wait_for` timeout
or a periodic `call_later` in flight will appear to "work" -- but
every cross-thread interaction will take as long as the next
scheduled timer, instead of completing in milliseconds. A
`wait_for(timeout=30)` masks a 30-second stall as success.

Usage:
    python3 repro.py

Outside Codex (or with `sandbox_mode = "danger-full-access"`):
    All three tests pass. Script exits 0.

Inside Codex with any sandbox mode that filters network
(`workspace-write`, `read-only`, `full-auto`):
    All three tests fail. Script exits 1.

NB: this script deliberately avoids `asyncio.run()` and uses
`os._exit()` to bypass interpreter shutdown. Inside Codex's sandbox,
`asyncio.run()`'s own cleanup hangs forever -- it calls
`loop.shutdown_default_executor()`, which calls
`call_soon_threadsafe()` to signal the executor-shutdown thread
back to the loop. That's exactly the broken primitive we are
demonstrating, with no timeout by default, so the interpreter never
exits. Worse, because `codex exec` captures stdout via a pipe,
Python block-buffers the test output -- the user sees ZERO output
because the buffer is never flushed. We work around this by setting
`flush=True` on every print and skipping the asyncio cleanup path.

The three tests target the same underlying bug at three layers:

  1. Syscall layer: `socket.send` on an AF_UNIX `socketpair` returns
     EPERM, because Codex's seccomp filter denies `SYS_sendto`
     unconditionally. (Yes — even on a local UNIX socket whose only
     correspondent is the same process. The filter does not condition
     on socket family the way it does for `SYS_socket` /
     `SYS_socketpair`.)

  2. asyncio primitive layer: `loop.call_soon_threadsafe(callback)`
     from a background thread fails to wake the selector. asyncio's
     `_write_to_self` calls `self._csock.send(b'\\0')` (which is
     literally a `sendto(2)` syscall on its internal AF_UNIX
     socketpair). seccomp returns EPERM. asyncio silently swallows
     the OSError. The callback sits in the ready queue but
     `epoll_wait` never returns to run it.

  3. User-visible layer: `loop.run_in_executor(None, fn)` queues `fn`
     in the default `ThreadPoolExecutor`, the worker runs it in
     microseconds, then signals completion via
     `call_soon_threadsafe(future.set_result, ...)` — exactly the
     silently-broken path. The future stays pending until some
     unrelated timer event wakes the selector.

Root cause, verified against the actual filter source:
    https://github.com/openai/codex/blob/main/codex-rs/linux-sandbox/src/landlock.rs

    deny_syscall(&mut rules, libc::SYS_sendto);
    deny_syscall(&mut rules, libc::SYS_sendmmsg);
    // ... no AF_UNIX exception, unlike SYS_socket/SYS_socketpair below.

Related upstream issue (different symptom, same class of bug):
    https://github.com/openai/codex/issues/19927  (asyncio.subprocess
    hang inside the same sandbox; their workaround
    `asyncio.SafeChildWatcher()` is subprocess-specific and does
    nothing for the bug demonstrated here.)

This script has no dependencies beyond the Python standard library.
"""

import asyncio
import os
import socket
import sys
import threading
import time

# `TIMEOUT_S` is the outer asyncio.wait_for timeout. `HEALTHY_S` is the
# threshold above which we declare a wakeup pathologically slow.
#
# Calibration: outside Codex, a cross-thread wakeup resolves in ~50 ms
# (mostly the deliberate `time.sleep(0.05)` in the test). Inside
# Codex's sandbox, `call_soon_threadsafe` does NOT wake the selector
# at all -- but `asyncio.wait_for` schedules a `call_later` timer for
# `TIMEOUT_S`, and when THAT timer fires the selector wakes and
# drains the ready queue, which includes the long-since-queued
# `set_result` callback. The future resolves and `wait_for` returns
# normally, having waited ~`TIMEOUT_S`. So under the broken
# environment the elapsed time is essentially exactly `TIMEOUT_S`,
# not a `wait_for` timeout. The test must therefore distinguish
# "wakeup arrived in a sane time" from "wakeup arrived only because
# an unrelated timer happened to fire". `HEALTHY_S` = 0.5s cleanly
# separates the two regimes.
TIMEOUT_S = 2.0
HEALTHY_S = 0.5


def print_flush(*args, **kwargs) -> None:
    """`print` that always flushes. Required because `codex exec`
    captures stdout via a pipe, so Python's default block-buffering
    means a user under Codex sees ZERO output until the buffer is
    flushed -- which, inside Codex's sandbox, may be never (see
    the cleanup-hang explanation in the module docstring)."""
    kwargs.setdefault("flush", True)
    print(*args, **kwargs)


# ---------------------------------------------------------------------------
# Test 1: syscall layer.
# ---------------------------------------------------------------------------

def test_socket_send() -> tuple[bool, str]:
    """Try to do, synchronously, the exact operation asyncio's self-
    pipe wakeup depends on: send one byte through one half of an
    AF_UNIX socketpair. Returns (passed, message)."""
    try:
        a, b = socket.socketpair(socket.AF_UNIX, socket.SOCK_STREAM, 0)
    except OSError as e:
        return (
            False,
            f"socket.socketpair(AF_UNIX, SOCK_STREAM) raised {e!r} "
            f"-- Codex's seccomp filter denies SYS_socketpair in "
            f"proxy-routed mode.",
        )
    try:
        try:
            a.send(b'\0')
        except OSError as e:
            return (
                False,
                f"socket.send on an AF_UNIX socketpair raised {e!r} "
                f"-- Codex's seccomp filter denies SYS_sendto. This "
                f"is the same syscall asyncio uses internally for "
                f"cross-thread loop wakeups.",
            )
        return (True, "socket.send on an AF_UNIX socketpair succeeded.")
    finally:
        a.close()
        b.close()


# ---------------------------------------------------------------------------
# Test 2: asyncio primitive layer.
# ---------------------------------------------------------------------------

async def test_call_soon_threadsafe() -> tuple[bool, str]:
    """Schedule a callback from a background OS thread via
    `loop.call_soon_threadsafe` and measure how long it takes for the
    selector to wake up and run it."""
    loop = asyncio.get_running_loop()
    event = asyncio.Event()

    def from_thread() -> None:
        # Tiny stall so the main task is definitely parked on
        # `event.wait()` before we fire the wakeup. This delay is
        # OUTSIDE the timing window.
        time.sleep(0.05)
        loop.call_soon_threadsafe(event.set)

    threading.Thread(target=from_thread, daemon=True).start()
    start = time.monotonic()
    try:
        await asyncio.wait_for(event.wait(), timeout=TIMEOUT_S)
    except asyncio.TimeoutError:
        return (
            False,
            f"loop.call_soon_threadsafe failed to wake the selector "
            f"within {TIMEOUT_S}s, even with the `wait_for` deadline "
            f"timer firing. The callback is queued but `epoll_wait` is "
            f"not returning at all. asyncio's `_write_to_self` is "
            f"silently swallowing an OSError from "
            f"`self._csock.send(b'\\0')`.",
        )
    elapsed_ms = (time.monotonic() - start) * 1000
    if elapsed_ms > HEALTHY_S * 1000:
        # The wakeup eventually "arrived" -- but only because the
        # `wait_for` deadline timer fired and drained the ready queue
        # as a side effect. The real `call_soon_threadsafe` self-pipe
        # wakeup is still broken. Healthy systems resolve this in
        # tens of milliseconds; ~`TIMEOUT_S` means the bug is
        # present.
        return (
            False,
            f"loop.call_soon_threadsafe wakeup latency was "
            f"{elapsed_ms:.0f}ms (healthy <{HEALTHY_S * 1000:.0f}ms). "
            f"The callback was queued by the worker thread within "
            f"~50ms but the selector did not run it until the "
            f"`wait_for` deadline timer fired at {TIMEOUT_S}s -- "
            f"i.e. cross-thread wakeups are only succeeding by "
            f"accident, via unrelated timer events. asyncio's "
            f"`_write_to_self` is silently swallowing an OSError from "
            f"`self._csock.send(b'\\0')`.",
        )
    return (
        True,
        f"loop.call_soon_threadsafe woke the selector in "
        f"{elapsed_ms:.2f}ms.",
    )


# ---------------------------------------------------------------------------
# Test 3: user-visible layer.
# ---------------------------------------------------------------------------

async def test_run_in_executor() -> tuple[bool, str]:
    """Issue several `loop.run_in_executor` calls back-to-back. The
    very first call can succeed even on broken systems because lazy
    `ThreadPoolExecutor` creation synchronously yields the loop and
    incidentally drains the ready queue once. Subsequent calls have
    to use the broken self-pipe wakeup path, so they hang."""
    loop = asyncio.get_running_loop()
    timings_ms: list[float] = []
    for i in range(3):
        start = time.monotonic()
        try:
            await asyncio.wait_for(
                loop.run_in_executor(None, lambda: None),
                timeout=TIMEOUT_S,
            )
        except asyncio.TimeoutError:
            return (
                False,
                f"loop.run_in_executor call #{i + 1} did not return "
                f"within {TIMEOUT_S}s, even with the `wait_for` "
                f"deadline timer firing. Prior call timings (ms): "
                f"{[round(t, 2) for t in timings_ms]}.",
            )
        elapsed_ms = (time.monotonic() - start) * 1000
        timings_ms.append(elapsed_ms)
        if elapsed_ms > HEALTHY_S * 1000:
            # Same "passed only because a timer fired" pattern as
            # test 2. Note the first call typically slips under the
            # threshold even on broken systems, because lazy
            # ThreadPoolExecutor creation synchronously yields the
            # loop and incidentally drains the ready queue once.
            return (
                False,
                f"loop.run_in_executor call #{i + 1} took "
                f"{elapsed_ms:.0f}ms (healthy <{HEALTHY_S * 1000:.0f}ms). "
                f"The worker thread completed in microseconds and "
                f"queued the future's `set_result` callback via "
                f"`call_soon_threadsafe`, but the loop did not run it "
                f"until the next unrelated timer event. Timings of "
                f"all calls so far (ms): "
                f"{[round(t, 2) for t in timings_ms]}.",
            )
    return (
        True,
        f"loop.run_in_executor x3 timings (ms): "
        f"{[round(t, 2) for t in timings_ms]}.",
    )


# ---------------------------------------------------------------------------

async def amain() -> int:
    print_flush(
        f"python={sys.version.split()[0]}  pid={os.getpid()}  "
        f"ppid={os.getppid()}  loop={type(asyncio.get_running_loop()).__name__}"
    )
    # /proc/self/status:Seccomp tells us if a BPF filter is active.
    # 0=off, 1=strict, 2=filter. Under Codex's sandbox it is 2.
    try:
        with open('/proc/self/status', 'r') as f:
            for line in f:
                if line.startswith(('Seccomp:', 'NoNewPrivs:')):
                    print_flush('  ' + line.rstrip())
    except OSError:
        pass

    print_flush()
    print_flush("Test 1 (syscall): send 1 byte through an AF_UNIX socketpair")
    t1_passed, t1_msg = test_socket_send()
    print_flush(f"  {'PASS' if t1_passed else 'FAIL'}: {t1_msg}")

    print_flush()
    print_flush("Test 2 (asyncio primitive): loop.call_soon_threadsafe from a thread")
    t2_passed, t2_msg = await test_call_soon_threadsafe()
    print_flush(f"  {'PASS' if t2_passed else 'FAIL'}: {t2_msg}")

    print_flush()
    print_flush("Test 3 (user-visible): loop.run_in_executor x3")
    t3_passed, t3_msg = await test_run_in_executor()
    print_flush(f"  {'PASS' if t3_passed else 'FAIL'}: {t3_msg}")

    print_flush()
    if t1_passed and t2_passed and t3_passed:
        print_flush("ALL OK: asyncio cross-thread wakeup works here.")
        return 0
    print_flush(
        "REPRO REPRODUCED: asyncio cross-thread wakeup is broken here. "
        "See module docstring for root cause."
    )
    return 1


if __name__ == "__main__":
    # We deliberately do NOT use `asyncio.run(amain())`. asyncio.run's
    # teardown calls `loop.shutdown_default_executor()`, whose
    # implementation spawns a thread that runs
    # `executor.shutdown(wait=True)` and then signals back to the loop
    # via `call_soon_threadsafe(future.set_result, None)`. That is
    # exactly the broken primitive this script demonstrates, with no
    # timeout by default -- so under Codex's sandbox `asyncio.run`
    # hangs forever during cleanup, AFTER all our tests have already
    # completed. The user would see no output (block-buffered stdout)
    # and assume the script itself is stuck.
    #
    # Instead we drive the loop manually, then `os._exit()` to skip
    # interpreter shutdown entirely. `loop.close()` does call
    # `executor.shutdown(wait=False)`, which doesn't hang.
    _loop = asyncio.new_event_loop()
    try:
        _code = _loop.run_until_complete(amain())
    finally:
        _loop.close()
    sys.stdout.flush()
    sys.stderr.flush()
    os._exit(_code)

The output I observe is:

$ codex exec "python3 ./repro.py"
OpenAI Codex v0.134.0
--------
workdir: /workspaces/mono/repro
model: gpt-5.5
provider: openai
approval: never
sandbox: read-only
reasoning effort: medium
reasoning summaries: none
session id: 019e6f3d-feae-7f22-b062-aac01872b64e
--------
user
python3 ./repro.py
warning: Codex could not find bubblewrap on PATH. Install bubblewrap with your OS package manager. See the sandbox prerequisites: https://developers.openai.com/codex/concepts/sandboxing#prerequisites. Codex will use the bundled bubblewrap in the meantime.
hook: SessionStart
hook: SessionStart Completed
codex
I’ll run the script and report the output.
exec
/bin/bash -lc 'python3 ./repro.py' in /workspaces/mono/repro
codex
The script is still running after printing the first failure and starting the asyncio-threadsafe test. I’m going to wait briefly so I can capture the rest rather than cutting off a hanging repro too early.
 exited 1 in 4032ms:
  FAIL: loop.call_soon_threadsafe wakeup latency was 2002ms (healthy <500ms). The callback was queued by the worker thread within ~50ms but the selector did not run it until the `wait_for` deadline timer fired at 2.0s -- i.e. cross-thread wakeups are only succeeding by accident, via unrelated timer events. asyncio's `_write_to_self` is silently swallowing an OSError from `self._csock.send(b'\0')`.

Test 3 (user-visible): loop.run_in_executor x3
  FAIL: loop.run_in_executor call #2 took 2002ms (healthy <500ms). The worker thread completed in microseconds and queued the future's `set_result` callback via `call_soon_threadsafe`, but the loop did not run it until the next unrelated timer event. Timings of all calls so far (ms): [2.09, 2002.26].

REPRO REPRODUCED: asyncio cross-thread wakeup is broken here. See module docstring for root cause.

codex
`python3 ./repro.py` exited with code `1`.

Key output:

"""text
Test 1: FAIL
socket.send on an AF_UNIX socketpair raised PermissionError(1, 'Operation not permitted')
Codex's seccomp filter denies SYS_sendto.

Test 2: FAIL
loop.call_soon_threadsafe wakeup latency was 2002ms

Test 3: FAIL
loop.run_in_executor call #2 took 2002ms

REPRO REPRODUCED: asyncio cross-thread wakeup is broken here.
"""

The repro confirms the environment’s seccomp filter is blocking the syscall asyncio relies on for cross-thread event loop wakeups.

What is the expected behavior?

As run outside a Codex sandbox:

$ python3 ./repro.py 
python=3.10.12  pid=15949  ppid=2079  loop=_UnixSelectorEventLoop
  NoNewPrivs:   0
  Seccomp:      0

Test 1 (syscall): send 1 byte through an AF_UNIX socketpair
  PASS: socket.send on an AF_UNIX socketpair succeeded.

Test 2 (asyncio primitive): loop.call_soon_threadsafe from a thread
  PASS: loop.call_soon_threadsafe woke the selector in 50.25ms.

Test 3 (user-visible): loop.run_in_executor x3
  PASS: loop.run_in_executor x3 timings (ms): [1.8, 0.14, 0.12].

ALL OK: asyncio cross-thread wakeup works here.

Additional information

No response

Activity

  1. added
    bug
    on May 28, 2026
  2. added
    tool-calls
    on May 28, 2026
  3. JunJieLiu51520 commented on May 29, 2026

    @JunJieLiu51520

    I'd like to work on this. The root cause is clear from the repro: deny_syscall(&mut rules, libc::SYS_sendto) in the seccomp filter applies unconditionally — including to AF_UNIX socketpairs that asyncio uses internally for cross-thread wakeups — while SYS_socket and SYS_socketpair already have an AF_UNIX exception.

    I'll trace the exact filter code and propose a targeted fix. Happy to submit a PR if the team would like me to implement this.

  4. JunJieLiu51520 commented on May 29, 2026

    @JunJieLiu51520

    Root cause & fix

    Root cause

    In codex-rs/linux-sandbox/src/landlock.rs, NetworkSeccompMode::Restricted unconditionally denies SYS_sendto and SYS_sendmmsg:

    deny_syscall(&mut rules, libc::SYS_sendto);
    deny_syscall(&mut rules, libc::SYS_sendmmsg);

    Python's asyncio uses sendto() on connected AF_UNIX socketpairs internally (_write_to_self self-pipe mechanism) for cross-thread wakeups. These calls have dest_addr=NULL, addrlen=0. The seccomp filter blocks them — asyncio silently swallows the EPERM, leaving the event loop selector stuck until an unrelated timer fires.

    By contrast, SYS_socket and SYS_socketpair already have AF_UNIX exceptions in the filter, so creating AF_UNIX sockets works fine — only sending on them is broken.

    Fix

    Replace the unconditional SYS_sendto deny with a conditional rule that only fires when addrlen (arg5) ≠ 0:

    // Allow sendto on connected sockets (addrlen==0, e.g. AF_UNIX socketpair wakeups).
    // Deny sendto with an explicit destination address (addrlen!=0, i.e. network sends).
    // BPF cannot dereference the sockaddr pointer to inspect sa_family, so addrlen==0
    // is the correct proxy: POSIX mandates addrlen==0 for connected-socket sends.
    let sendto_network_rule = SeccompRule::new(vec![SeccompCondition::new(
        5, // arg5: addrlen
        SeccompCmpArgLen::Dword,
        SeccompCmpOp::Ne,
        0u64,
    )?])?;
    rules.insert(libc::SYS_sendto, vec![sendto_network_rule]);

    For SYS_sendmmsg, the destination address is embedded inside the mmsghdr struct (a pointer BPF cannot dereference). Following the same rationale as the existing SYS_recvfrom exemption, sendmmsg is allowed unconditionally — network isolation is still enforced by the existing SYS_socket/SYS_connect denials which prevent creation of or connection to network sockets.

    My fork branch with the full implementation: fix/24933-seccomp-sendto-af-unix

    Happy to submit a PR if the team would like me to implement this.

  5. added a commit that references this issue on May 29, 2026
    9ccf86d
  6. pzfreo commented on Aug 18, 2026

    @pzfreo

    Still reproduces on codex 0.147.0 (Debian, Python 3.14, Fly.io microVM), and the analysis above matches the measurement exactly.

    Minimal probe, run inside codex exec -s workspace-write:

    1. socketpair() created OK
    2. sock.send() BLOCKED: PermissionError [Errno 1] Operation not permitted
    3. os.write(fd) OK
    

    Worth noting for anyone tracking this: #20427 was closed by #26625 ("Allow socketpair in proxy-routed Linux sandbox", merged 2026-06-05). The probe above is direct evidence that fix is present — socketpair() now succeeds — and that the SYS_sendto / SYS_sendmmsg denial this issue describes is the remaining half. The AF_UNIX exception exists for socket creation but not for sending on the resulting fd.

    The reason this is easy to misdiagnose deserves a mention. Whether it hangs is a race:

    loop running
    fast to_thread -> 42 in 0.00s      # completes before the loop parks; no wakeup needed
    Timeout (0:00:45)!                 # slow to_thread never returns
    

    Outside the sandbox that slow call returns in 3.00s. A fast asyncio.to_thread finishes before the loop reaches select(), so it needs no wakeup at all and looks perfectly healthy. Only work slow enough for the loop to park hangs — and then it hangs forever, because _write_to_self swallows the OSError and call_soon_threadsafe has already queued the callback. Small smoke tests pass; real workloads wedge.

    In our case it took a while to find because every plausible suspect cleared: /dev/shm, multiprocessing, spawn with RLIMIT_DATA, and the whole CAD library behaved identically inside and outside the sandbox. The agent simply reported its render command as "still running; no output" indefinitely, while the identical command under the same confinement finished in four seconds.

    Workaround for anyone blocked, until the seccomp rule gains an AF_UNIX exception for sendto: keep one timer permanently pending on the loop, which caps how long it can park so the already-queued callback is picked up on the next pass.

    def keep_loop_responsive(loop, interval=0.05):
        def beat():
            loop.call_later(interval, beat)
        beat()

    Public asyncio API, and inert once this is fixed upstream. Note it needs to cover loop shutdown too — asyncio.run calls shutdown_default_executor, which awaits a future resolved from a thread and hangs the same way, so a plain heartbeat task that gets cancelled with the main coroutine is not sufficient.

    Related: #15053 (oldest report, 2026-03-18), #33793 (has a tested patch whose author was blocked from opening a PR).

  7. ned2 commented on Aug 30, 2026

    @ned2

    Consolidating the related reports here because this appears to be one seccomp defect with much broader impact than the minimal Python reproducer suggests.

    The restricted, network-disabled Linux profile denies sendto() on an already-connected socket. In Python, that breaks the AF_UNIX socketpair wakeup used by call_soon_threadsafe(). I found at least eight open issue threads which are likely caused by this one underlying problem (plus an older closed report) spanning Codex 0.55 through 0.151:

    • Core asyncio/AnyIO hangs: call_soon_threadsafe(), asyncio.to_thread(), executors, and AnyIO thread helpers in #15053, #24933, #33793, and the later reproductions in #7852.
    • Database and application-store hangs: SQLAlchemy/aiosqlite, SQLite worker handoffs, migrations, and related tests in #9906 and #30352.
    • Web-test hangs: AnyIO filesystem offloading, Starlette/FastAPI FileResponse, and route tests in #29147.
    • Native compute/render stalls: Daft collect() in #33793 and a CAD render reported in #24933.
    • Probable Bun/TypeScript impact: TypeScript 7's async updateSnapshot() hangs under Bun but works under Node in #40473; the same report confirms the connected-socket send() denial directly.
    • Historical exact report: #6352 demonstrated the failed socketpair write and resulting asyncio hang on Codex 0.55.

    This is easy to misdiagnose because the worker often completes successfully, Python suppresses the wakeup OSError, and the process simply appears to remain running. Fast work can win the race and pass, while realistic work hangs after the event loop parks. The same missed wakeup also affects asyncio.run() shutdown, so a test can print PASSED and then stall for minutes.

    #20427 and #26625 allowed AF_UNIX socketpair() creation, but current probes show that sending on the resulting pair still receives EPERM.

    #33793 already describes a narrow fix, available as commit 582a47d: allow send() on an already-connected socket while continuing to deny sendto() with an explicit destination. The reporter states that all 120 sandbox tests pass and that both the asyncio and Daft reproductions succeed with the patched binary.

    Given the repeated reports and the tested narrow patch, this feels like something worth prioritising. @etraut-openai would it be possible to get some relevant eyeballs on this one?

  8. mwatola-glitch commented on Sep 23, 2026

    @mwatola-glitch

    Still reproduces on codex-cli 0.155.1 (Linux, Python 3.14.7, pytest-asyncio 1.4.0); landlock.rs:211 is unchanged on main.

    One addition to the timer workaround above: sendmsg is not on the deny list. In the same sandbox sock.sendmsg([b"\0"]) on the loop's self-pipe socketpair succeeds where sock.send() gets EPERM, so swapping asyncio's wakeup write removes the hang without polling:

    # sitecustomize.py, put on PYTHONPATH for sandboxed runs
    import os
    if os.environ.get("CODEX_SANDBOX_NETWORK_DISABLED") == "1":
        import asyncio.selector_events as se
    
        def _write_to_self(self):
            csock = self._csock
            if csock is None:
                return
            try:
                csock.sendmsg([b"\0"])
            except OSError:
                pass
    
        se.BaseSelectorEventLoop._write_to_self = _write_to_self

    With this in place a pytest suite that used to hang at ~23% inside the sandbox finishes in 2.8 s.

  9. dariuszkowalski-com commented on Oct 18, 2026

    @dariuszkowalski-com

    Reproduced on Codex CLI 0.162.1 (app-server 0.163.0), Linux aarch64, kernel 6.17, glibc 2.39.

    With network_access = false (profile managed, network restricted) the seccomp filter rejects
    sendto on AF_UNIX sockets as well:

    $ codex sandbox -c sandbox_mode='"workspace-write"' \
          -c sandbox_workspace_write.network_access=false -- \
          python3 -c 'import socket; a, b = socket.socketpair(); a.send(b"x")'
    PermissionError: [Errno 1] Operation not permitted
    

    sendmsg(2) and write(2) on the same socket are permitted, and so is the pidfd_open syscall
    (a raw syscall(434, getpid(), 0) through ctypes returns a descriptor).

    CPython's event loop wakes itself with socket.send() on its self-pipe socketpair — one sendto
    syscall — and BaseSelectorEventLoop._write_to_self swallows the resulting OSError. Every
    loop.call_soon_threadsafe() is therefore lost and the loop waits in epoll_wait for ever. An
    interpreter without os.pidfd_open (the python-build-standalone CPython 3.12.13 does not have it;
    the Debian 3.12.3 build does) reaps children with ThreadedChildWatcher, whose thread uses exactly
    that wakeup, so even a plain subprocess wait never returns after the child has exited:

    $ cat probe.py
    import asyncio
    async def main():
        proc = await asyncio.create_subprocess_exec("/bin/true")
        await proc.wait()
    asyncio.run(main())
    
    network_access=true   -> exits in 0.00 s
    network_access=false  -> never exits (an interpreter with os.pidfd_open: 0.00 s either way)
    

    Any task runner that drives subprocesses through asyncio hangs the same way: the command does its
    work, prints its output and then never exits. Together with #51615 (the inner sandbox tree outliving
    the Codex process) one workstation accumulated about 100 orphaned trees over three weeks — 312
    processes, ~0.5 GiB PSS and ~2.4 GiB of swap.

    Either of two changes would fix it: permit sendto on AF_UNIX sockets in the restricted profile
    (it grants no network reachability), or permit that path explicitly for socketpairs. Our local
    workaround is a site shim that replaces the self-pipe wakeup with sendmsg, active only when
    CODEX_SANDBOX_NETWORK_DISABLED is set.

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

    bugtool-calls

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions