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- Catégorie
- IO and permission errors
Ce que cela signifie
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.
Causes courantes
- 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
Comment le corriger
-
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.
Reproduisez-le en toute sécurité
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
Questions
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.
Erreurs liées
IO ErrorA file could not be openedSQLITE_BUSYSQLite : la base de données est verrouillée55P03PostgreSQL : verrou non disponible
Le code et le texte du message sont issus de DuckDB documentation. Parcourir la liste complète DuckDB error list ou Rechercher toutes les erreurs de base de données.
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.
Texte et données de cette page : CC BY 4.0, en citant SQLly (sqlly.app). Code et extraits SQL : MIT. Détails de la licence Le texte des messages d’erreur cité appartient à l’éditeur de la base de données.