SQLITE_BUSY: database is locked: embedded replica file locked
A local embedded replica file could not take or release its lock, usually because another process has the same replica file open for writing.
- Error
SQLITE_BUSY: database is locked- Name
5- Category
- Replicas and consistency
What it means
An embedded replica is a local SQLite file that syncs from the remote database. Local reads are plain SQLite, and the sync loop writes the replicated frames into the same file, so local locking rules apply. The file allows one writer at a time. Two processes opening the same replica file, or a long local write while the replication loop applies frames, collide on the file lock and SQLite returns SQLITE_BUSY with the text database is locked. Turso's docs describe the layout: reads run locally from the file, and writes are sent to the cloud primary and then reflected back to the replica, so the local file is being written both by your process and by the replication loop.
Common causes
- Two processes or app instances opened the same replica file, and both tried to write.
- A local write held the file lock while
sync()tried to apply replicated frames. - A crashed process left the file's lock bookkeeping in a state a new opener cannot take.
- A raw
BEGIN IMMEDIATEin the replication-enabled libSQL build can return busy immediately instead of waiting on busy_timeout.
Find the cause
List every process that opens the replica file: `lsof replica.db` shows holders. More than one writer is the cause. If only one process holds it, check whether the error arrives during `sync()` after a long local write, which points at lock contention between your transaction and the replication loop. After a crash, open the file read-only (`sqlite3 'file:replica.db?mode=ro' 'PRAGMA integrity_check;'`) to test whether the file itself is readable.
How to fix it
-
Give each process its own replica file, or route writes through one process:
// process A const dbA = createClient({ url: 'file:replica-a.db', authToken: token }); // process B const dbB = createClient({ url: 'file:replica-b.db', authToken: token }); -
Set a busy timeout for the local file so SQLite waits instead of failing fast. In @libsql/client the
timeoutoption coversfile:URLs:const db = createClient({ url: 'file:replica.db', syncUrl, authToken, timeout: 5000 }); -
Keep local write transactions short and sync outside them:
await db.sync(); // outside any open transaction
-
If a crashed process left the file wedged, check for stale locks by opening it read-only:
sqlite3 replica.db 'PRAGMA integrity_check;'
Other ways to handle it
Use libsql:// directly for all writes and keep the replica read-only. That removes the local write lock entirely, at the cost of network round trips on writes.
Reproduce it safely
Local and honest: create two clients on the same file URL and make both write in a loop; the second writer hits `LibsqlError: SQLITE_BUSY: database is locked`. Or with plain sqlite3: hold a transaction open in one shell and run `BEGIN IMMEDIATE` in another on the same file. No remote account needed; the cloud primary never sees the contention.
Questions
Do embedded replicas support writes?
Yes. Per Turso's docs, writes on an embedded replica are sent to the cloud primary and then reflected back to the local file; reads always run locally. That round trip still touches the local file, so local file locks matter.
Can two processes share one embedded replica file?
Reads can coexist, but two writers on the same local file collide with SQLITE_BUSY. Give each process its own replica file, or route writes through the remote libsql:// URL and keep the replicas read-only.
How do I set a busy timeout for a file: database in @libsql/client?
Pass timeout in milliseconds to createClient({ url: 'file:...', timeout: 5000 }). The client applies it to every connection it opens, and it only takes effect for local SQLite databases; remote clients ignore it, and Turso Cloud does not support PRAGMA busy_timeout at all.
Related errors
SQLITE_BUSYSQLITE_BUSY: the database is busytoo many connectionsToo many connections to one databaseconnection reset / interruptedConnection reset or dropped
More replicas and consistency errors
The code and message text are from the libSQL / Turso documentation. Browse the full libSQL error list or search every database error.
Work on libSQL in SQLly
A native SQL client for macOS, Windows and Linux with schema-aware IntelliSense, guardrails for risky statements and clear error messages, for libSQL and the other databases you use.
Text and data on this page: CC BY 4.0, credit SQLly (sqlly.app). Code and SQL snippets: MIT. License details Quoted error message text belongs to its database vendor. View as Markdown