Skip to content

WSL Codex sandbox hangs FastAPI/Starlette TestClient and blocks Vite localhost bind #28517

Open

Description

@anipatke

Environment:

  • Codex CLI: 0.140.0
  • OS: Ubuntu 24.04 under WSL
  • Terminal: Warp
  • Repo path: /home/user/code/Kids-Tutor
  • codex doctor sandbox: restricted filesystem + restricted network, approval
    OnRequest
  • Project trust_level: trusted in ~/.codex/config.toml

Problem:
Inside Codex sandbox on WSL, FastAPI/Starlette TestClient requests hang
indefinitely. The same tests pass outside the sandbox.

A second related symptom: Playwright/Vite cannot bind localhost in the sandbox
with:
Error: listen EPERM: operation not permitted 127.0.0.1:5173

Minimal repro:

from fastapi import FastAPI
from fastapi.testclient import TestClient

app = FastAPI()

@app.get("/health")
def health():
    return {"ok": True}

print("before")
print(TestClient(app).get("/health").status_code)
print("after")

Expected:
Prints 200 and exits.

Actual in Codex sandbox:
Prints "before", then hangs in Starlette/AnyIO TestClient.

Observed stack:

  • starlette.testclient.py handle_request / enter
  • anyio.from_thread portal wait
  • asyncio selector loop waiting

Control:
Running the same repo gate outside Codex sandbox passed:

  • make test -> backend 1441 passed / 216 skipped; frontend vitest 305 passed
  • npx playwright test --project=chromium admin-weekly-report-review -> 6
    passed when allowed to bind localhost

Impact:
Codex can produce false audit/test failures in WSL because normal local test
gates hang only inside the sandbox. This has happened more than once.

Activity

  1. added
    bug
    CLI
    windows-os
    sandbox
    on Jun 16, 2026
  2. MakenRosa commented on Jun 17, 2026

    @MakenRosa

    I reduced this to a plain socket repro in the Linux sandbox. The failure is not FastAPI/Vite-specific; restricted network currently blocks creating an IPv4 socket at all, so frameworks that start an in-process loopback listener fail before they can even bind().

    Repro in WSL after building codex-linux-sandbox:

    cargo build -p codex-linux-sandbox --bin codex-linux-sandbox
    

    Then compare outside sandbox, restricted network, and enabled network:

    outside: ('127.0.0.1', 45563)
    sandbox_restricted: PermissionError: [Errno 1] Operation not permitted
    restricted_exit:1
    sandbox_enabled: ('127.0.0.1', 42321)
    enabled_exit:0
    

    The restricted case fails at Python's socket.socket() constructor, not at bind():

    _socket.socket.__init__(self, family, type, proto, fileno)
    PermissionError: [Errno 1] Operation not permitted
    

    Relevant code path:

    • codex-rs/linux-sandbox/src/linux_run_main.rs maps NetworkSandboxPolicy::Restricted to BwrapNetworkMode::Isolated unless managed proxy mode is enabled.
    • BwrapNetworkMode::Isolated adds --unshare-net in codex-rs/linux-sandbox/src/bwrap.rs.
    • The inner stage applies the permission profile to the already-bwrapped process, and the network seccomp restrictions deny socket creation.
    • Existing sandbox tests cover blocked outbound attempts like curl, wget, ping, nc 127.0.0.1:80, and /dev/tcp, but I do not see coverage for the allowed-local-listener case (socket() -> bind(127.0.0.1, 0) -> listen()).

    That matches both symptoms in the issue:

    • Starlette/FastAPI TestClient can hang or fail because the test harness may create sockets internally even though it is not trying to reach the public network.
    • Vite/Playwright cannot start a localhost server because the sandbox rejects socket creation under restricted network.

    A good fix direction would be to distinguish outbound network access from local loopback listeners. At minimum, add a regression test for creating/listening on 127.0.0.1:0 under restricted network, then decide whether the seccomp policy should allow AF_INET/AF_INET6 socket creation with later constraints, or whether a dedicated "loopback allowed, external denied" network mode is needed. The current policy is stricter than "network disabled" in the way developer/test tooling usually expects, because it also denies local-only server setup.

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

    CLIbugsandboxwindows-os

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions