IO Error: The database file is locked by another process
Another process holds the write lock on the DuckDB file, so this process cannot open it read-write.
- Exception
IO Error- Categoría
- IO and permission errors
Qué significa
DuckDB allows many processes on one file only if all of them are read-only. The first read-write connection takes an exclusive lock for the lifetime of its process, and any second process that wants the file read-write fails with this error. Inside a single process several connections share one lock, so the error can only come from a second process.
The wording varies by version: recent builds name the holder, for example Conflicting lock is held in /usr/local/bin/duckdb (PID 4242) by user app, and older builds say Conflicting lock is held by another process. Both point at the same file and the same collision.
Causas comunes
- A second process opened the same file read-write: a CLI session, a notebook kernel, a background job or another instance of your app.
- An orchestrator retry started a new run while the previous one still holds the file.
- The file sits on a network share where file locking behaves differently, so who holds the lock is unclear.
Find the cause
-- Confirm which file this connection actually opened, and whether it is read-only: SELECT database_name, path, readonly FROM duckdb_databases(); -- Then, in your shell (not in DuckDB), list the processes holding it: -- lsof /data/db.duckdb
Cómo solucionarlo
-
Find the holder with your operating system's tools and close it, then retry. On macOS and Linux, lsof lists the process that has the file open.
-
When you only need to query, open read-only. This only works when no read-write process holds the file, so it does not coexist with a running writer:
ATTACH '/data/db.duckdb' AS wh (READ_ONLY); -- fails while a read-write process holds the file
-
Route all writes through one process. DuckDB has no cross-process table or row locks, so the choices are one writer plus readers that open only after the writer exits, or everything read-only.
SELECT database_name, path FROM duckdb_databases();
-
Serialize background jobs against each other in the orchestrator so two runs cannot target the same file at once.
Other ways to handle it
If several processes must work against the same data at once, give each writer its own DuckDB file and keep the shared data in Parquet or CSV, which every process can read concurrently. A single DuckDB file with one writer and read-only readers is the supported shape.
Reprodúcelo de forma segura
ATTACH 'lock_demo.duckdb' AS db; -- Keep this session open. In a second session run the same line: ATTACH 'lock_demo.duckdb' AS db; -- IO Error: Could not set lock on file "lock_demo.duckdb": Conflicting lock is held by another process
Preguntas
Can two processes read the same file at the same time?
Yes, as long as every one of them opens it read-only. The exclusive lock is for writing, not for reading. A read-only open fails while a read-write process holds the file.
I closed everything and it still says locked. Is the lock stale?
Operating systems release locks when a process dies, so a truly stale lock is rare. The usual leftover is a service, editor plugin or notebook kernel still running. Check with lsof before suspecting corruption.
Errores relacionados
IO ErrorA file could not be openedSQLITE_BUSYSQLite: la base de datos está bloqueada55P03PostgreSQL: bloqueo no disponible
El código y el texto del mensaje son de DuckDB documentation. Explora la lista completa de DuckDB error list o buscar cada error de base de datos.
Work on DuckDB in SQLly
A native SQL client for macOS, Windows and Linux with schema-aware IntelliSense, guardrails for risky statements and clear error messages, for DuckDB and the other databases you use.
Texto y datos de esta página: CC BY 4.0, con atribución a SQLly (sqlly.app). Código y fragmentos SQL: MIT. Detalles de la licencia El texto citado de los mensajes de error pertenece a su proveedor de base de datos.