Skip to content
Source
Sign inSign up
pingdotgg/t3code

[Bug]: Server event loop stalls for seconds on synchronous SQLite fsyncs (synchronous=FULL on main thread), causing "Timed out while checking Codex app-server provider status" #19242

Closedmoshemiz06(GitHub profile, opens in a new tab) opened this issue 1 comment

Before submitting

  • I searched existing issues and did not find a duplicate.
  • I included enough detail to reproduce or investigate the problem.

Area

apps/server

Summary

On Linux with a spinning disk, Settings shows "Codex provider status: Timed out while checking Codex app-server provider status." over and over. This is not the probe-budget problem tracked in #7513 (and #18879, closed as its duplicate). Codex answers the probe in ~0.2–1 s. The 10 s AUTH_PROBE_TIMEOUT_MS expires because the T3 server's own event loop is blocked for 2–25 s at a time by synchronous fsyncs on the main thread.

Root cause: the state DB (statev2.sqlite) is opened with node:sqlite DatabaseSync on the main thread, and layerSetup (persistence/Layers/Sqlite.ts in the bundle) never sets PRAGMA synchronous. SQLite therefore keeps its default synchronous = FULL, which in WAL mode fsyncs the WAL on every commit. Each fsync runs inside statement.run() on the JS thread. On rotational media under write load, every commit blocks the entire server (WebSocket, RPC, timers, child-process stdout) until the ext4 journal commit completes. The rotating trace and provider-log sinks (RotatingFileSink: appendFileSync / renameSync / rmSync) add more synchronous IO to the same thread.

The Codex status probe is just the most visible victim: its wall-clock timeout runs while the loop is frozen, so it fails even when Codex replied promptly.

Environment

  • T3 Code 0.0.46-nightly.20261013.3080, t3 serve background service, Linux x86_64 (Ubuntu), Node-SEA build
  • Codex CLI 0.162.1, ChatGPT login, healthy
  • Disk: 1 TB 5400 rpm SATA HDD (WD10JPVX, rotational=1), ext4; 11 GiB RAM; statev2.sqlite is 531 MB (orchestration_events 260 MB, projection_thread_activities 108 MB)

Evidence

1. Codex is fast; the probe is not. Replaying the exact probe sequence (initialize, account/read, skills/list, model/list, account/rateLimits/read, cwd = $HOME, CODEX_INTERNAL_ORIGINATOR_OVERRIDE="T3 Code") by hand returns every response in 0.2–1.0 s, 15/15 runs. Inside T3, withCodexAppServerClient alone (spawn + initialize) takes 0.3–10 s+. Codex's own ~/.codex/logs_2.sqlite has no entries at all from the probe process during failed windows. Some probe spans last 68 s, 100 s, 116 s and 211 s despite the 10 s timeoutOption, which is only possible if the server's own loop isn't running.

2. The loop stalls while off-CPU. server.eventLoop.stall spans from server.trace.ndjson (36 in ~3.5 h; the boot-service.log has more, up to 24.8 s):

timedelayMaxMsutilizationcpuUserMscpuSystemMsmajorPageFaults
21:42:34157440.9114460143
21:46:4691751.0022825129
21:47:1996201.00480182
21:48:0896621.002395450
21:59:56135790.9619118114
22:00:48210790.79151640599

The loop is "active" for the whole stall but burns only a few hundred ms of CPU, so it was blocked in a syscall, not running JS.

3. The blocking syscall is a journal commit (fsync). I sampled /proc/<t3 pid>/task/<main tid>/{stat,wchan} every 100 ms for ~1 min while the server was nearly idle (one active thread):

497  S  ep_poll
 48  D  jbd2_log_wait_commit     <- main JS thread in uninterruptible fsync wait
 20  R
  4  D  rq_qos_wait

That is ~9% of wall time stuck in jbd2_log_wait_commit, in runs of ~1 s, with almost no load. Under streaming load it's far worse.

4. Commit rate × fsync latency matches the stalls. From the trace: 20,247 write statements and 6,657 transactions in 199 minutes, peaking at ~650 writes / ~215 transactions per minute (21:31–21:52, exactly the worst stall window, while agents were streaming). Write-statement span durations: p50 0.1 ms, p90 4 ms, p99 311 ms, max 7,324 ms. Disk at idle: r_await 39 ms, f_await 27 ms. /proc/pressure/io full avg60 ≈ 5%.

5. Slow SELECTs in the trace are victims, not causes. The trace shows e.g. the shell thread-list query (selectShellThreadRows) at 209,588 ms and SELECT * FROM scheduled_tasks … at 30 s. Run against a copy of the same DB, the thread-list query takes 5 ms (86 rows). Only one process/connection has statev2.sqlite open, so it isn't lock contention either; those spans just straddled a stall.

6. Synchronous IO calls on the main thread (from the shipped bundle):

  • nodeSqliteClient: new NodeSqlite.DatabaseSync(...), then statement.all() / statement.run() inline
  • persistence/Layers/Sqlite.ts layerSetup: busy_timeout = 5000, foreign_keys = ON, journal_mode = WAL, journal_size_limit = 32 MiB, no synchronous (checked: a fresh connection reports synchronous = 2/FULL)
  • RotatingFileSink: appendFileSync, renameSync, rmSync, statSync (server trace rotates 10 MB every ~10–15 min here, plus per-thread provider/events.*.log)

Steps to reproduce

  1. Run t3 serve on a machine whose T3 home is on a rotational disk (or throttle IO, e.g. a cgroup io.max wbps/wiops limit on the service).
  2. Run one or two agent threads that stream heavily (long reasoning / tool output).
  3. Watch boot-service.log for event loop stalled for N ms, and Settings → Providers for the Codex card.
  4. Optional: sample /proc/<pid>/task/<pid>/wchan, which shows jbd2_log_wait_commit on the main thread.

Expected behavior

Database and log writes must not block the event loop for seconds, and provider probes should not report timeouts caused by the server's own stalls.

Actual behavior

Repeated multi-second stalls (up to ~25 s) during normal use. The Codex card keeps flipping to "Timed out while checking Codex app-server provider status." (latched per #7230), along with other knock-on timeouts in the same windows (VcsProcessTimeoutError … GitVcsDriver.isInsideWorkTree … after 5000ms, 20 s operation timeouts, git status refresh spans of 15–23 s).

Suggested fixes (in order of impact)

  1. PRAGMA synchronous = NORMAL in layerSetup. With WAL this is crash-safe for DB integrity (only the last transactions can roll back on power loss), it removes the per-commit fsync, and Node's own webstorage DB already uses it.
  2. Move SQLite off the main thread (worker-backed client), or at least batch high-frequency projection writes (per-reasoning-delta / per-item events) into fewer transactions.
  3. Make RotatingFileSink async (fs.promises / a write stream) so trace and provider-event logging can't block the loop.
  4. Probe robustness, separate from the root cause: measure the probe deadline against loop-adjusted time, or retry once before reporting error, so a server stall isn't reported as a Codex failure. Related: #7513, #7230.

Related: #7513, #18879 (probe budget), #7230 (timeout latch), #14701 (thread list read blocking the loop: possibly the same synchronous-IO cause on large DBs).

  1. Note

    Grok responding on behalf of Julius.

    Thanks for the detailed trace. The root cause you found (statev2.sqlite left at the default synchronous = FULL, so every WAL commit fsyncs on the main thread) was fixed in #19205, which sets PRAGMA synchronous = NORMAL on every connection. It merged after your build (0.0.46-nightly.20261013.3080 was cut before it), so it'll be in the next nightly. The original slow-disk report and A/B numbers are in #15707.

    Closing as fixed. If you still see multi-second server.eventLoop.stall spans or the Codex probe timing out on the next nightly, comment here with a fresh trace and we'll reopen. Moving SQLite work off the main thread altogether is tracked separately in #14701 and #19226.

  2. juliusmarminge closed this

Sign in to join this conversation.