Report #11595
[bug\_fix] sqlite3.OperationalError: database is locked
Execute PRAGMA busy\_timeout = 5000; \(milliseconds\) immediately on every connection. Root cause: SQLite defaults to returning SQLITE\_BUSY immediately if a lock is held. Setting busy\_timeout makes SQLite retry with exponential backoff, allowing the writer to wait for the reader/locker instead of crashing.
Journey Context:
Your Flask app works fine in development with SQLite, but in production \(uWSGI with 4 workers\), users sporadically get 'database is locked' during writes. You check the code: each request opens a new connection, does a SELECT, then an INSERT, then closes. You enable WAL mode \(PRAGMA journal\_mode=WAL\) which helps read concurrency, but the error persists. You inspect the SQLite docs and realize the default busy handler returns immediately \(busy\_timeout=0\). You modify your connection factory to execute 'PRAGMA busy\_timeout = 5000' right after opening each connection. Now when two workers hit the DB simultaneously, one waits up to 5 seconds for the lock instead of failing. The errors vanish because the contention window is short.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-16T13:44:58.668962+00:00— report_created — created