Opening a long thread blocks the database writer for 7–10 s #21840
Version: 0.1.0, Linux server, statev2.sqlite ≈ 10 GB.
What happens: opening a thread calls http.orchestration.loadThreadSnapshotWindow → environment.orchestration.threadBoundedSnapshot. Its whole duration is one sqlite span: the WITH eligible AS NOT MATERIALIZED (...) turn-items query in readCanonicalProjection. It runs inside sql.transaction with db.connection: writer. Seven consecutive opens measured 8.5 s median, 9.6 s max. A warm repeat of the same thread took 50 ms.
Why it matters: because the read holds the writer, every write waits behind it: creating a thread, dispatching a message, event-sink writes. From the client this looks like the app freezing when you switch threads or start a new one.
Suggestion: run snapshot reads on a reader connection outside the write transaction (WAL already allows concurrent readers). Separately, consider an index-only path for the window boundary so a cold open doesn't scan every turn item of the thread.
Repro data: span tree and timings from server.trace.ndjson are available on request. They contain no thread content.
juliusmarminge added labelsaccepted
juliusmarminge added labelsbug
juliusmarminge added labelsvia-triage
#21869 would move
getThreadSnapshotWindowto the reader. Four otherProjectionStorereads still default to the writer on main at e45ddea067e8, because neither they nor their callers below opt intoreadOnly. On 0.1.1-nightly.20261019.3360 (1451e7cb2303), with a real 36.7 GBstatev2.sqlite, each one blocked the server's main event loop for the whole statement. Times are CEST, UTC+2.Read on the writer Caller Statement Stall log record getShellSnapshotThreadLaunchService branch rename, 18:26:13 11,701 ms 11,256 ms same same, 19:21:05 11,697 ms 12,514 ms same startup heartbeat, which reads the full shell to count threads, 19:27:54 8,091 ms 7,942 ms getThreadRecords, message-id queryMCP tools/call, 19:23:5012,380 ms 11,482 ms getRecoveryThreadIds, plain querystartup recovery, 19:26:08 11,448 ms none logged, across the restart getSettlementCandidates11 auto-settlement sweeps, 18:10 to 19:28 813 to 5,311 ms 2,444 to 5,170 ms for the 9 SELECTs over 2 s While each statement ran, no other server span started or ended, and every row with a stall record matched its statement to within a second. That fits the writer being a synchronous
DatabaseSyncon the main thread that gets every statement not markedreadOnly.The host was overloaded (Apple M1, 8 cores, 16 GB, load average 57.28 at 18:25:57), and the times include whatever that added. For comparison, the same shell-row query ran on the reader worker in the same minutes: all 12 runs over 5 s, taking 8.72 to 20.93 s. Six had stall records of at most 4,813 ms in their windows, and four had none. One shares a 30 s window with the heartbeat read above. The last shares a window with a 15,962 ms record I can't explain, although server spans kept starting and ending throughout it. I read this as the writer connection blocking the event loop while the reader worker lets other server work run. It doesn't separate out the host load or explain why the statements took 8 to 20 s.
If you'd rather keep this issue to the snapshot window, I can file the remaining reads separately. Span trees are available on request.
Reader runs, call sites, and method
Reader runs, each with a parent
sql.transactionondb.connection: reader:Start Caller Statement Longest gap with no server span Stall records in its windows 18:23:15 pull-request sync 17.74 s 2.14 s 15,962 ms 18:23:49 HTTP shell load 20.93 s 0.78 s 2,188 ms 18:24:21 WebSocket shell load 18.56 s 2.84 s none 18:25:22 pull-request sync 10.34 s 1.83 s none 19:20:01 pull-request sync 9.29 s 1.02 s 4,209 ms 19:21:38 pull-request sync 11.27 s 3.24 s 4,813 ms 19:24:30 pull-request sync, inferred from sibling spans 12.11 s 2.38 s 4,520 ms 19:28:02 pull-request sync 11.12 s 6.28 s, during a separate AgentAwarenessRelay writer read 7,942 ms 19:28:39 HTTP shell load 11.18 s 0.75 s none 19:28:59 WebSocket shell load 10.85 s 0.75 s none 19:29:11 HTTP shell load 9.73 s 0.97 s 2,471 ms 19:29:24 WebSocket shell load 8.72 s 1.84 s 2,471 ms Call sites on main at e45ddea067e8:
getShellSnapshot: ThreadLaunchService.ts L511-L517, serverRuntimeStartup.ts L148-L160 and L280, AgentAwarenessRelay.ts L708-L711. Pull-request sync calls it underreadOnlyat ThreadPullRequestService.ts L124-L130.getThreadRecords:loadProjectionin OrchestratorMcpService.ts L977-L992 and other MCP tools. The message-id query is inreadCanonicalProjectionand runs whenmessagesis requested and more than one message comes back.getRecoveryThreadIds("runtime"): ProviderRuntimeRecoveryService.ts L703-L704, during startup recovery.getSettlementCandidates: everyThreadSettlementServiceV2.sweepwhile auto-settlement is configured, at ThreadSettlementService.ts L316-L319. Open #21628 shortens this transaction but keeps it on the writer.
Method:
- A gap is the longest time inside a statement with no server span starting or ending. It leaves out spans the desktop renderer forwards, which carry the renderer's clock and reached the trace only after each freeze ended. Span boundaries alone don't prove that requests were answered.
- "Same query" rests on both paths calling
readShellSnapshotRowsand on matching query text, which the trace cuts after about 110 characters of SQL. Reader callers ask for active threads only and writer callers for all, so the row sets differ. - A stall record is the largest sampled event-loop delay minus the monitor's 1 s resolution, sampled about every 30 s. I matched a record at time T to the 30 s before it. The monitor logs only delays above 2 s, and only when the loop's active time is at least the delay, so "none" means no record, not proof of a delay under 2 s. The log has no record from 19:25:14 to 19:28:07, across the restart, and I didn't check why.
Traces analyzed with GPT-6.1 Sol in Codex and Claude Opus 5.5 in Claude Code, written by Claude Opus 5.5, on behalf of @ImSynem.
juliusmarminge added labelsneeds more info
juliusmarminge removed labelsneeds more info
More evidence from 0.1.2-nightly.20261021.3499 (17 GB
statev2.sqlite, macOS). Right after a restart with a cold cache,readShellSnapshotRowstakes about 34 s, and several callers run it inside a writersql.transaction:ThreadPullRequestServiceV2.synchronize(68.7 s and 34.3 s),StorageCleanup.readThreads(35.0 s) andserver.startup.heartbeat.record(33.8 s). Writer statements waited up to 68.8 s behind them, and the event loop recorded a 34 s stall in the same window. Once the cache was warm, the same read took about 4 s.Another effect of the same cold-start wait: today the desktop window failed at startup with
DesktopLocalEnvironmentAuthSessionBootstrapError: Failed to create the local desktop bearer session. The desktop's POST to/oauth/tokentimed out after 10000 ms, becauseSessionStore.issuewaited behind twoProjectionStore.readShellSnapshotRowsreads (34.5 s and 35.4 s) that ran inside writer transactions. The longest write wait in that minute was 35.3 s. A minute later the server answered normally, so the 10 s client timeout turns a slow first minute into a failed launch.Note
Claude Opus 5.5 in Claude Code, writing on behalf of @tsmith165. Measured on his Mac on 2026-10-22.
Another data point, from a larger database on the current nightly: 0.1.2-nightly.20261022.3526, macOS desktop app,
statev2.sqliteat 44.2 GiB.Database size
Table Rows orchestration_events6,618,661 orchestration_command_receipts3,479,494 orchestration_v2_projection_turn_items834,009 threads 2,630 The largest thread has about 512,000 events and 78,268 turn items (about 210 MB of
payload_json).Stalls on this build (
event-loop-stalls.ndjson)- In the 54 minutes after a restart at 06:48 local, the log has 56 records over 1 s. 23 of them are over 5 s, and the longest is 13.3 s.
- In the 20 minutes before that, the first run of this build after the nightly update, it has 21 records. The longest is 13.6 s.
What the user sees
The desktop shows "Main Workstation is reconnecting" for 15 to 45 s, and actions wait until it reconnects. Cheap calls wait behind the blocked loop: on this build
ServerSecretStore.gettook up to 9.6 s andSessionStore.verifyup to 14.1 s. On the 20261021 nightly the same calls took up to 45.7 s.Several long-running orchestrator threads on this machine read other threads through the T3 tools, so reads of large threads happen often. The four writer reads @ImSynem listed above match what we see. History and timeline pages are covered in #20427.
Same pattern here, through MCP rather than the UI.
- Setup: Windows, Nightly 0.1.2-nightly.20261021.3473,
statev2.sqlite31.1 GB. The longest thread has over 6,300 runs. OrchestratorMcpService.readThread→loadReadableThreadran a writer-connection transaction that executed 25.5 s (twice in about 95 minutes).OrchestratorMcpService.listThreadsandws.orchestrationV2.threadSnapshotThenLivealso each held the writer for 5 s or more.- Over the same window, writer-connection statements spent 97.3% of their time waiting for the lock (p99 5.7 s, max 90.8 s).
- So agents reading threads through MCP freeze writes the same way opening a thread does. +1 to running these snapshot reads on the reader connection, outside a write transaction.
- Setup: Windows, Nightly 0.1.2-nightly.20261021.3473,
The four reads are filed separately, as you offered, and a pull request is up for them: #23689.
It covers all four named here, plus
getThreadShell, which is the shell row for one thread and is read on every thread read:Read Caller Covered by #23689 getShellSnapshotthread launch, startup heartbeat, agent awareness relay yes getThreadRecordsMCP tools/call, message-id queryyes getRecoveryThreadIdsstartup recovery yes getSettlementCandidatesthe auto-settlement sweep yes getThreadShellthe shell row for one thread yes It is the same shape of change as #21869: each read is wrapped in
NodeSqliteClient.readOnly, the existing helper that routes a read to the reader connection. The queries, the transaction shape, the error mapping and the results are unchanged; only the connection moves.One extra measurement, from a host serving a 25.5 GB
statev2.sqliteunder load 68-116 on 10 cores, which is the MCP-heavy case:MCP tool calls: 1,857 in ~10 minutes (peak 506 in one minute) DB lock waits > 100 ms: 1,608 (518 seconds of lock wait in that window) correlation calls/min vs total lock wait/min: r = 0.751 sqlite ops by connection: 18,408 (writer, read) <-- reads also go through the writer 234 (writer, write) sql.transaction by connection: writer n=15,839 mean=35.9ms reader n=8 mean=497.7ms78.5% of every query in that window ran inside an MCP call, and the worst single lock wait was 6,622 ms on
SELECT payload_json FROM orchestration_v2_projection_nodes WHERE node_id = ?. Thereaderline in the transaction table is the same starvation you measured: the reader pool was used for 8 transactions in ten minutes, and those were the ones that averaged 497.7 ms.For the test, the reader-worker and the writer are put on either side of a held write lock rather than a stream of inserts, so the read cannot land after the writes are done. The same read then behaves differently on the two connections:
MEASURE read-on-reader: connection=reader lock_wait_ms=0.024 lock_held_for_ms=13 uncommitted_rows_visible=0 MEASURE read-on-writer: outcome=TimeoutError lock_held_for_ms=3002 connections_acquired=0The read on the reader connection completes in 13 ms with the lock held; the same read on the writer connection cannot acquire a connection at all. The
uncommitted_rows_visible=0is the check that the writer's row was still uncommitted when the read finished, so the write really was in flight for the whole read.I have not touched the query cost, which you also raised - the indexes are separate work against a different bottleneck, and #21869's scope note says the same.
Note
🤖 GPT-6.1 Terra [1m] responding on behalf of Theo
Closing as fixed by the merged SQLite reader change. Bounded thread and shell snapshots now use the reader connection, with regression coverage for reads while the writer is occupied. #23902 ProjectionStore.ts
t3dotgg(GitHub profile, opens in a new tab) closed this
Sign in to join this conversation.
Note
Grok responding on behalf of Julius.
Thanks for the measurements. Current main still behaves the way you describe.
The windowed thread snapshot read runs in a plain
sql.withTransaction(apps/server/src/orchestration-v2/ProjectionStore.ts:5225-5274). A write transaction there starts withBEGIN IMMEDIATEon the writer connection (packages/shared/src/nodeSqliteClient.ts:818), so it holds the write lock for as long as the read takes. The server already has a way to send a read transaction to the reader connection (nodeSqliteClient.ts:54-63), and some other reads use it, but this snapshot read doesn't.None of the open pull requests move this read off the writer. #21193 adds indexes for windowed lookups, which addresses the cold query cost, your second suggestion, but not where the read runs.
Accepting this as a bug.