Report #15342
[bug\_fix] sqlite3.OperationalError: database is locked \(SQLITE\_BUSY\)
Execute \`PRAGMA busy\_timeout = 5000;\` \(milliseconds\) immediately after opening the database connection. This configures SQLite to retry the operation with exponential backoff for up to 5 seconds before returning SQLITE\_BUSY. Additionally, enable WAL mode \(\`PRAGMA journal\_mode=WAL;\`\) to allow concurrent readers during writes.
Journey Context:
Python Flask app with SQLite backend deployed with gunicorn \(multi-process\). Under any concurrent load, writes fail with "database is locked". Developer discovers SQLite's default busy timeout is 0 \(immediate fail\). Initially tries wrapping every write in a \`while\` loop with \`time.sleep\`, which works but is inefficient. Eventually finds the \`busy\_timeout\` pragma, sets it to 10000 \(10s\), and the errors disappear because SQLite internally handles the contention sleeping. For higher concurrency, they later switch to WAL mode to allow readers to proceed during writes.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-16T23:49:55.591509+00:00— report_created — created