GeezerVison is showing github.com — Concurrent `opencode run` processes fail mid-session with "Failed to execute statement" (SQLITE_BUSY not retried, no error code) · Issue #65020 · anomalyco/opencode
Concurrent opencode run processes fail mid-session with "Failed to execute statement" (SQLITE_BUSY not retried, no error code) #65020DescriptionVersion: opencode v2.3.0, Linux x86_64, local ext4/btrfs-class disk (not a network filesystem)
Problem
Running many opencode run --format json processes at once against the same opencode.db makes a large share of them die mid-session with:
{"type":"error","sessionID":"ses_…","error":{"type":"unknown","message":"Failed to execute statement"}}
The message carries no SQLite error code, so callers cannot tell a transient SQLITE_BUSY from a real failure and cannot retry sensibly.
Observed
Likely causes
- Each process opens its own connection and writes message/part rows on every streamed step, so N processes = N concurrent writers on one WAL database. WAL allows only one writer; the rest rely on the busy handler.
- Busy handling appears to be absent or too short. A write that cannot acquire the lock immediately surfaces as a generic "Failed to execute statement".
- With a multi-GB DB, checkpoints and the WAL index are slower, widening the window in which writers collide.
Suggested fixes
- Set
PRAGMA busy_timeout (for example 5–10 s) on every connection, and PRAGMA synchronous=NORMAL with WAL.
- Retry statements that fail with
SQLITE_BUSY / SQLITE_BUSY_SNAPSHOT with bounded jittered backoff instead of failing the session.
- Use
BEGIN IMMEDIATE for write transactions so a lock conflict fails at the start of the transaction, where it is safe to retry, instead of at upgrade time, which is not retryable.
- Batch part updates (write the final part, or coalesce streamed deltas) so a step costs one write transaction, not many.
- Include the SQLite result code and extended code in the error (
SQLITE_BUSY, SQLITE_LOCKED, …) so wrappers can classify it.
- Optionally allow a per-run database (
OPENCODE_DB=… or an ephemeral session store) for headless run invocations that do not need to share history.
Repro sketch
for i in $(seq 1 50); do
opencode run --format json -m <any-fast-model> "reply with OK" >/tmp/o$i.log 2>&1 &
done
wait
grep -l 'Failed to execute statement' /tmp/o*.log | wc -l
(Failure count rises with concurrency and with database size.)
Related: #56282, #53879 Activityopencodejr commented on Nov 20, 2026 Thanks for the detailed report. I couldn't reproduce this on v2.3.0 here. None of the runs failed with Failed to execute statement or any other error event, and the server log had no database is locked / SQLITE_BUSY entries.
What I tried:
- 50, 70 and 100 concurrent
opencode run --format json -m <model> "reply with OK" processes sharing one data dir and one opencode.db (WAL, local disk).
- A local OpenAI-compatible mock model streaming 200 or 1000 small deltas per reply.
- A database of about 13 MB, and the same database grown to 2.6 GB.
Some runs at 70 and 100 hung, but that was caused by a file-watcher limit in my sandbox (EMFILE), not the SQLite error.
My runs were short and my large database was mostly filler, so they may not create the same lock pressure as your setup. These would help:
- The server log lines around a failure. Logs are under
<data dir>/opencode/log/ (for example ~/.local/share/opencode/log/). Search for locked, BUSY or Failed to execute and paste the full lines with any stack or cause.
- Roughly how long each worker's session runs, and whether workers run tools (shell, edits) or only reply.
- Whether it still happens after
PRAGMA wal_checkpoint(TRUNCATE) on the database while no opencode process is running, or with a fresh data dir (XDG_DATA_HOME=...) at 50 concurrent.
- CPU count and disk type (NVMe/SSD/HDD), and the output of
opencode --version.
Related: #47566 (same symptom on V1, closed), #56282 (lock storm on a shared filesystem) and #53879 (race on first start). The open PR #59521 retries SQLite statements on lock timeout.
opencodejr commented on Nov 20, 2026 I'm closing this for now. I couldn't reproduce the Failed to execute statement failures on v2.3.0 with up to 100 concurrent opencode run processes sharing one database, and we didn't get the details that could show what's different on your setup.
If it still happens, these would help most:
- The server log lines around a failure (search for
locked, BUSY or Failed to execute)
- How long each worker's session runs, and whether it runs tools
- Your CPU count and disk type
Related: #56282, #53879, and the open PR #59521, which retries SQLite statements on lock timeout.
Replying here with those details reopens this issue.
WhiteWarrior625 commented on Nov 25, 2026 Seeing this on 1.19.6 (Windows) with 11 concurrent opencode serve processes, one per session from a desktop client, sharing one 11.7 GB opencode.db. The sessions are long agent sessions that run tools. Three died within a minute of each other with:
level=ERROR message="prompt_async failed" cause="Cause([Die(effect/sql/SqlError: Failed to execute statement (cause: effect/sql/SqlError/LockTimeoutError: Failed to execute statement (cause: SQLiteError: database is locked)))])"
The machine was under heavy load at the time (about 650 MB free RAM, about 80% CPU, about 20 concurrent git processes from snapshots). The limit being hit looks like the hard-coded PRAGMA busy_timeout = 5000; 2.4.1 has the same value.
Repro that needs no model: on an empty WAL database, hold a write lock from another connection (BEGIN IMMEDIATE) for 15 s and run OPENCODE_DB=<that file> opencode session list. Stock 1.19.6 exits 1 after about 6.5 s (the migration's CREATE TABLE fails). The same binary with only that pragma changed to 120000 waits and exits 0 once the lock is released. A retry like #59521, or a configurable busy timeout, would fix this.
MetadataMetadataDevelopmentNo branches or pull requestsIssue actions
|