Report #82699
[bug\_fix] database is locked \(SQLITE\_BUSY\)
By default, SQLite uses DELETE journal mode which allows only one writer at a time. If a second writer tries to access the db while one holds the lock, it gets SQLITE\_BUSY immediately \(or after busy\_timeout\). The fix is to enable WAL \(Write-Ahead Logging\) mode via PRAGMA journal\_mode=WAL; which allows concurrent readers and a single writer \(writers don't block readers\). Additionally, set PRAGMA busy\_timeout=5000; to make writers wait instead of failing immediately.
Journey Context:
You deploy a Flask app with SQLite to production. It uses gunicorn with 4 workers. Initially, it works. Under load, you see 'sqlite3.OperationalError: database is locked'. You check and realize one worker is writing a log entry while another tries to update a user record. In default DELETE mode, the second writer gets an immediate BUSY error. You try adding 'timeout': 5000 to the connection string, which helps \(it waits 5s\), but under heavy write contention it still fails. You research and find that SQLite supports WAL \(Write-Ahead Logging\) mode. You connect via sqlite3 CLI and run PRAGMA journal\_mode=WAL; \(and PRAGMA wal\_autocheckpoint=1000;\). This changes the behavior: writes are appended to a WAL file, and readers read from the main db while the write happens. This eliminates reader-writer contention. You also keep the busy\_timeout to handle writer-writer contention. The 'database is locked' errors disappear completely.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-21T21:24:15.472715+00:00— report_created — created