diff --git a/src/db.rs b/src/db.rs index 24395fcac0..0a2b6d9bb8 100644 --- a/src/db.rs +++ b/src/db.rs @@ -2477,8 +2477,8 @@ mod tests { assert!(OpenOptions::default().allow_unlocked(true).allow_unlocked); } - /// `allow_unlocked` must not open a door past a live lock. It applies only - /// where the filesystem cannot lock at all. + /// `allow_unlocked` does not override a held lock. It applies only when + /// the filesystem does not support locking. #[test] fn test_allow_unlocked_does_not_bypass_a_live_lock() { let dir = tempfile::tempdir().unwrap(); diff --git a/src/storage/backend/file.rs b/src/storage/backend/file.rs index 8d772f1023..ab4cfc668a 100644 --- a/src/storage/backend/file.rs +++ b/src/storage/backend/file.rs @@ -91,19 +91,16 @@ fn already_open_here(path: &Path) -> bool { .unwrap_or(false) } -/// Atomically test-and-set `path` into `OPEN_PATHS`, admitting only the -/// first caller. +/// Check and record `path` under the same mutex so only the first caller +/// can claim it. /// -/// Used by the `ProceedUnlocked` arm, where there is no kernel lock and this -/// registry is the only thing preventing two handles on one unlockable path -/// (#304). Must be a single critical section: checking with -/// `already_open_here` and inserting afterwards would let two threads both -/// observe "absent" and both proceed. A poisoned mutex yields `false`, so -/// this fails CLOSED rather than admitting a second handle it cannot -/// account for. +/// `ProceedUnlocked` uses this process-local registry when the filesystem +/// cannot provide a lock (#304). Checking and inserting separately would +/// let two threads both claim the path. If the mutex is poisoned, refuse +/// the claim. /// -/// Split out from `open_with` so the race can be exercised directly (#330) -/// instead of needing a genuinely lock-incapable filesystem. +/// This helper lets tests check the race without a filesystem that lacks +/// locks (#330). fn claim_unlocked_path(path: &Path) -> bool { OPEN_PATHS .lock() @@ -122,9 +119,8 @@ fn claim_unlocked_path(path: &Path) -> bool { /// commonly do) -- can observe `WouldBlock` during that few-microsecond /// window even though no other process is really contending for the file. /// SQLite has the same class of problem and solves it with `busy_timeout`; -/// this is our version, bounded so a database that is genuinely locked by -/// another process still fails -- after paying the full retry budget below, -/// not instantly -- rather than `open` hanging indefinitely. +/// we also limit how long we retry. If another process keeps its lock, +/// `open` returns an error after the retries instead of waiting indefinitely. const LOCK_RETRY_ATTEMPTS: u32 = 10; /// Initial backoff between retries, doubled each attempt up to @@ -208,14 +204,8 @@ impl FileBackend { let mut lock_result = file.try_lock(); - // Retry a `WouldBlock` with backoff, but ONLY when the conflict might - // be with another process. If this process already has the path - // open, waiting is pointless: we are the only thing that could ever - // release that lock, and we are standing right here not releasing - // it, so every retry would just burn the budget on a certain - // failure. Skipping this check keeps `test_second_open_in_same_process_is_refused` - // fast instead of paying the full retry budget on every same-process - // rejection. + // Retry `WouldBlock` with backoff only if this process does not + // already have the path open. Skip retries for a known local conflict. if matches!(lock_result, Err(std::fs::TryLockError::WouldBlock)) && !already_open_here(&canonical) {