Report #68161
[bug\_fix] database is locked \(SQLITE\_BUSY\) when writing to SQLite
Enable Write-Ahead Logging \(WAL\) mode by executing \`PRAGMA journal\_mode=WAL\` on each database connection at startup, and set a busy timeout using \`PRAGMA busy\_timeout = 5000\` \(milliseconds\) to make connections wait for locks instead of returning SQLITE\_BUSY immediately. WAL mode allows concurrent readers to coexist with a single writer.
Journey Context:
A developer deploys a Python Flask application using SQLite. With a single development server, everything works. When deployed to production with Gunicorn using 4 workers, the application intermittently crashes with 'sqlite3.OperationalError: database is locked' during POST requests. The developer initially suspects zombie transactions but finds no uncommitted transactions using \`PRAGMA lock\_status\`. Researching SQLite's locking model reveals that the default DELETE journal mode uses exclusive locks that block all access. The solution is twofold: first, enable WAL mode \(\`PRAGMA journal\_mode=WAL\`\) which allows readers to proceed without blocking the writer \(and vice versa\), and second, set a busy timeout \(\`PRAGMA busy\_timeout = 10000\`\) so that if a writer is momentarily blocked by another writer, it waits rather than erroring immediately. After applying both pragmas on connection startup, the locking errors disappear even under high concurrency.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-20T20:53:30.468327+00:00— report_created — created