diff --git a/crates/pdfs-core/src/db/mod.rs b/crates/pdfs-core/src/db/mod.rs
index c397024..8d0da7f 100644
--- a/crates/pdfs-core/src/db/mod.rs
+++ b/crates/pdfs-core/src/db/mod.rs
@@ -445,6 +445,13 @@ fn is_in_memory(path: &Path) -> bool {
/// SQLite uses POSIX record locks, so the two never interact, and a lock file
/// left behind carries no state — the lock lives in the kernel and dies with
/// the process, however it exits.
+///
+/// A refusal is retried for [`LOCK_RETRY`] before it counts as another
+/// daemon. A child this process spawns holds a copy of every descriptor from
+/// its fork until its exec closes them, and `flock` belongs to the open file,
+/// not the descriptor. So a lock the process has just let go of can stay held
+/// for that moment, and a reopen right after a drop is refused
+/// (`docs/BUGS.md` B290).
fn acquire_single_writer_lock(path: &Path) -> Result> {
if is_in_memory(path) {
return Ok(None);
@@ -456,22 +463,32 @@ fn acquire_single_writer_lock(path: &Path) -> Result > {
.truncate(false)
.write(true)
.open(&lock_path)?;
- // SAFETY: `file` owns the descriptor and outlives the call.
- let rc = unsafe { libc::flock(file.as_raw_fd(), libc::LOCK_EX | libc::LOCK_NB) };
- if rc != 0 {
+ let deadline = std::time::Instant::now() + LOCK_RETRY;
+ loop {
+ // SAFETY: `file` owns the descriptor and outlives the call.
+ let rc = unsafe { libc::flock(file.as_raw_fd(), libc::LOCK_EX | libc::LOCK_NB) };
+ if rc == 0 {
+ return Ok(Some(file));
+ }
let e = std::io::Error::last_os_error();
- if e.raw_os_error() == Some(libc::EWOULDBLOCK) {
+ if e.raw_os_error() != Some(libc::EWOULDBLOCK) {
+ return Err(Error::Io(e));
+ }
+ if std::time::Instant::now() >= deadline {
return Err(Error::Other(format!(
"another Proton Drive daemon is already using {} — \
stop it first (`systemctl --user stop proton-drive.service`)",
path.display()
)));
}
- return Err(Error::Io(e));
+ std::thread::sleep(std::time::Duration::from_millis(20));
}
- Ok(Some(file))
}
+/// How long a refused single-writer lock is retried; far longer than a fork
+/// takes to reach its exec, far shorter than anyone notices.
+const LOCK_RETRY: std::time::Duration = std::time::Duration::from_millis(500);
+
fn lock_path_for(path: &Path) -> PathBuf {
let mut name = path.as_os_str().to_owned();
name.push(".lock");
diff --git a/docs/BUGS.md b/docs/BUGS.md
index d24fb3b..50e13bd 100644
--- a/docs/BUGS.md
+++ b/docs/BUGS.md
@@ -305,6 +305,7 @@ a live account since.
| Entry | Title |
|---|---|
+| [B290](#b290--reopening-the-database-right-after-closing-it-can-be-refused-as-another-daemon) | Reopening the database right after closing it can be refused as another daemon |
| [B276](#b276--a-stop-can-end-with-a-panic-in-a-thread-the-stop-never-waited-for) | A stop can end with a panic in a thread the stop never waited for |
| [B258](#b258--journalctl--p-warning-shows-nothing-whatever-the-daemon-logged) | `journalctl -p warning` shows nothing, whatever the daemon logged |
| [B256](#b256--pdfs-copy-right-after-a-save-copies-the-old-version) | `pdfs copy` right after a save copies the old version |
@@ -392,6 +393,30 @@ a live account since.
Newest first.
+### B290 — Reopening the database right after closing it can be refused as another daemon
+
+**Status:** Fixed, 2026-11-15
+**Found:** 2026-11-15, CI on `chore/release-3.8.0` (fd6b2d3):
+`db::tests::what_a_create_sent_outlives_a_newer_blob_its_trash_and_a_restart` failed once with
+"another Proton Drive daemon is already using /tmp/pdfs-create-sent-….db". It passes on its own.
+**Where:** `crates/pdfs-core/src/db/mod.rs`, `acquire_single_writer_lock`
+
+**Repro:** In one process, drop a `Db` and open the same file again while another thread spawns a
+child process. Rare; seen once in about 500 tests that run in parallel, several of which spawn
+processes.
+
+**Cause:** The single-writer lock is a `flock` on `.lock`, and a `flock` belongs to the open
+file, not to one descriptor. A child holds a copy of every descriptor from its fork until its exec
+closes the `O_CLOEXEC` ones. A lock dropped by the parent in that moment stays held by the child,
+and an immediate reopen gets `EWOULDBLOCK`, which reads as a second daemon.
+
+**Fix:** A refused lock is retried every 20 ms for up to 500 ms before it counts as another
+daemon. That covers any fork-to-exec window. A real second daemon is still refused, half a
+second later.
+
+**Verified:** `cargo test -p pdfs-core` on 2026-11-15, including
+`a_second_writer_is_refused_while_the_first_holds_the_database`.
+
### B289 — The Takeout import mixes in other services and drops location and live photos
**Status:** Fixed, not verified live