diff --git a/crates/ledger-store/src/backup.rs b/crates/ledger-store/src/backup.rs index f1a83e7b..281505ce 100644 --- a/crates/ledger-store/src/backup.rs +++ b/crates/ledger-store/src/backup.rs @@ -510,7 +510,7 @@ impl Git { use std::os::unix::process::CommandExt; c.process_group(0); } - let mut child = c.spawn().map_err(|e| match e.kind() { + let mut child = crate::lock::spawn(&mut c).map_err(|e| match e.kind() { std::io::ErrorKind::NotFound => BackupError::NoGit, kind => BackupError::Io(kind), })?; diff --git a/crates/ledger-store/src/lock.rs b/crates/ledger-store/src/lock.rs index 1da55e9f..69a70460 100644 --- a/crates/ledger-store/src/lock.rs +++ b/crates/ledger-store/src/lock.rs @@ -12,9 +12,80 @@ //! 2. A lock is let go when its file is closed, by this process ending //! or not (the system does it; nothing here keeps a record). //! 3. A question that cannot be answered is answered "not held". +//! 4. The writer lock of a store is let go when the store is dropped, for +//! every process that could still hold it: a child forked by another +//! thread of this process holds a copy of every open descriptor (this +//! lock's too) from its fork until its exec, and a `flock` lasts as +//! long as any copy does. Every child this crate starts is started by +//! [`spawn`], and dropping a [`WriterLock`] returns only once every +//! [`spawn`] begun before it has ended (its child has exec'd and the +//! copies are closed), and then the lock is taken afresh and let go +//! (it is free, or another writer has it, or 500 ms passed: the system +//! lets go of a closed file's last copy a moment after the exec), so +//! after a close is answered another open, here or elsewhere, finds +//! the lock free. //! ``` use std::fs::{File, TryLockError}; +use std::process::{Child, Command}; +use std::sync::RwLock; + +/// Held (shared) by every [`spawn`] while it runs, and taken (exclusively, +/// for an instant) by every dropped [`WriterLock`]. +static SPAWNING: RwLock<()> = RwLock::new(()); + +/// Start `command`: returns once the child has exec'd (or failed to), so +/// from then on it holds no copy of any descriptor of this process. +pub fn spawn(command: &mut Command) -> std::io::Result { + let _spawning = SPAWNING.read().unwrap_or_else(|e| e.into_inner()); + command.spawn() +} + +/// A store's exclusive lock on its books, released when this is dropped +/// (INVARIANT 4): the file is closed; every child still between its fork +/// and its exec, which holds a copy of it, is waited for; and the lock is +/// then taken afresh and let go, so it is known to be free (the system +/// lets go of a file's last copy a moment after the exec that closed it). +#[derive(Debug)] +pub(crate) struct WriterLock { + file: Option, + path: std::path::PathBuf, +} + +/// How long a dropped [`WriterLock`] waits for the system to let go of the +/// last copy of its file. +const RELEASE_WITHIN: std::time::Duration = std::time::Duration::from_millis(500); + +impl WriterLock { + pub(crate) fn new(file: File, path: std::path::PathBuf) -> Self { + Self { + file: Some(file), + path, + } + } +} + +impl Drop for WriterLock { + fn drop(&mut self) { + drop(self.file.take()); + drop(SPAWNING.write().unwrap_or_else(|e| e.into_inner())); + // Free, or taken by another writer already (then it is not ours to + // wait for): looked at until it is one or the other. + let until = std::time::Instant::now() + RELEASE_WITHIN; + while let Ok(file) = std::fs::OpenOptions::new().write(true).open(&self.path) { + match file.try_lock() { + Err(TryLockError::WouldBlock) if std::time::Instant::now() < until => { + std::thread::sleep(std::time::Duration::from_millis(1)); + } + Ok(()) => { + let _ = file.unlock(); + return; + } + Err(_) => return, + } + } + } +} /// Take a shared lock on `file` without waiting: whether it is now held. pub fn try_shared(file: &File) -> bool { @@ -34,3 +105,104 @@ pub fn held_elsewhere(file: &File) -> bool { Err(TryLockError::Error(_)) => false, } } + +#[cfg(all(test, unix))] +mod tests { + use super::*; + use std::os::unix::process::CommandExt; + + /// A command whose child lingers between its fork and its exec, as one + /// does on a slow machine, holding a copy of every descriptor; it says + /// so on `said` (a pipe's write end) as it begins to linger. + fn lingering(said: libc::c_int) -> Command { + let mut c = Command::new("true"); + // SAFETY: only `write` and `nanosleep`, both async-signal-safe, run + // in the child between fork and exec. + unsafe { + c.pre_exec(move || { + libc::write(said, b"x".as_ptr().cast(), 1); + let t = libc::timespec { + tv_sec: 0, + tv_nsec: 300_000_000, + }; + libc::nanosleep(&t, std::ptr::null_mut()); + Ok(()) + }); + } + c + } + + fn take(path: &std::path::Path) -> Option { + let file = std::fs::OpenOptions::new() + .create(true) + .truncate(false) + .write(true) + .open(path) + .unwrap(); + file.try_lock().ok().map(|()| file) + } + + /// Start a lingering child (through [`spawn`] or not) and return once + /// it is between its fork and its exec. + fn linger(gated: bool) -> std::thread::JoinHandle<()> { + let mut ends = [0 as libc::c_int; 2]; + // SAFETY: `ends` has room for the two descriptors. + assert_eq!(unsafe { libc::pipe(ends.as_mut_ptr()) }, 0); + let [read, write] = ends; + let started = std::thread::spawn(move || { + let mut c = lingering(write); + let mut child = if gated { + spawn(&mut c).unwrap() + } else { + c.spawn().unwrap() + }; + child.wait().unwrap(); + }); + let mut byte = 0u8; + // SAFETY: reads one byte into `byte` from the pipe made above. + assert_eq!( + unsafe { libc::read(read, (&mut byte as *mut u8).cast(), 1) }, + 1 + ); + // SAFETY: both descriptors are this function's own. + unsafe { + libc::close(read); + libc::close(write); + } + started + } + + /// INVARIANT 4. The control shows the cause (a forked child keeps the + /// `flock` after the file is closed: an open right after a close was + /// refused as "another program has these books open"); the second half + /// shows `spawn` and `WriterLock` close it. + #[test] + fn a_writer_lock_is_free_once_dropped_even_with_a_child_between_fork_and_exec() { + let dir = tempfile::tempdir().unwrap(); + let path = dir.path().join("wanderbooks.lock"); + + // Control: a plain file closed under an ungated spawn stays locked. + let held = take(&path).unwrap(); + let child = linger(false); + drop(held); + assert!( + take(&path).is_none(), + "the child between fork and exec should still hold the lock" + ); + child.join().unwrap(); + assert!( + take(&path).is_some(), + "the lock is free once the child ends" + ); + + // Gated: the drop returns only when the lock really is free. + let held = WriterLock::new(take(&path).unwrap(), path.clone()); + let child = linger(true); + drop(held); + assert!( + take(&path).is_some(), + "a dropped writer lock must be free at once" + ); + child.join().unwrap(); + } +} diff --git a/crates/ledger-store/src/store.rs b/crates/ledger-store/src/store.rs index ec6ae4b5..451a5007 100644 --- a/crates/ledger-store/src/store.rs +++ b/crates/ledger-store/src/store.rs @@ -483,7 +483,7 @@ pub struct Store { /// The exclusive lock on [`LOCK_FILE`], held while the store is open: /// one writer at a time. Released when dropped, or when the process /// exits, even after a crash. - _lock: std::fs::File, + _lock: crate::lock::WriterLock, data_dir: PathBuf, db_path: PathBuf, journal: JournalWriter, @@ -582,7 +582,7 @@ impl Store { Self::write_meta(&conn, "base_currency", base_currency.code())?; Self::write_meta(&conn, "last_applied_seq", "0")?; let mut store = Self { - _lock: lock, + _lock: crate::lock::WriterLock::new(lock, data_dir.join(LOCK_FILE)), data_dir: data_dir.to_path_buf(), db_path, journal: JournalWriter::open(&journal_path)?, @@ -724,7 +724,7 @@ impl Store { Self::write_meta(&conn, "last_applied_seq", "0")?; } let mut store = Self { - _lock: lock, + _lock: crate::lock::WriterLock::new(lock, data_dir.join(LOCK_FILE)), data_dir: data_dir.to_path_buf(), db_path, journal,