fix(store): a store's writer lock is free when it is dropped, even with a child forked from another thread
Cause: a book's writer lock is a flock, which lasts as long as any copy of its open file description does. A child forked by another thread of the server (git, for another book's backup, on a timer) holds a copy of every descriptor from its fork until its exec, so a close of book B, answered after its Store was dropped, could be followed by an open of B that found the lock still held: "another program has these books open". The open's failed attempt left B failed, and the next close found it not open. This broke the unit's rule that a close answers only once the lock is released, and that closing book A never refuses a job on book B (the hammer's maker thread saw it on GitHub's slower runner; with a 40 ms fork-to-exec delay in the backup's git it fails on every cycle). Fix: every child the crate starts goes through lock::spawn, which holds a process-wide gate while it runs (it returns once the child has exec'd); dropping the Store's WriterLock closes the file, waits for the gate to empty of spawns begun before, then takes the lock afresh and lets it go (the kernel lets go of a closed file's last copy a moment after the exec), for 500 ms at most. A test with a child held between fork and exec fails without it.
This commit is contained in:
1 parent
15845f71c8
commit
7801455336
3 files changed
+176
-4
No files matched your search
@@ -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,
|
||||
|
||||
Reference in new issue
Block a user