diff --git a/CHANGELOG.md b/CHANGELOG.md index c4f7cbf3..3892869f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,12 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/). [0.2.0]: https://github.com/runedeck/rune/compare/v0.1.0...v0.2.0 [0.1.0]: https://github.com/runedeck/rune/releases/tag/v0.1.0 +## [Unreleased] + +### Fixed + +- Release the spec archive lock when a transaction ends. A child process of another thread could hold the lock, so an immediate retry failed. + ## [0.7.1] - 2026-11-19 ### Fixed diff --git a/rune-docs/src/spec/transaction/mod.rs b/rune-docs/src/spec/transaction/mod.rs index eb1cae7a..e1c7117b 100644 --- a/rune-docs/src/spec/transaction/mod.rs +++ b/rune-docs/src/spec/transaction/mod.rs @@ -157,11 +157,24 @@ pub(crate) struct Transaction { root: PathBuf, state_directory: PathBuf, journal_path: PathBuf, - _lock: File, + _lock: HeldLock, io: I, recovery: Option, } +/// The held `LOCK_FILE`, unlocked before its descriptor closes. The lock +/// belongs to the open file description, and a child process that any +/// thread spawns holds a copy of the descriptor until its exec. A close +/// alone leaves the lock held until that child execs or exits. +struct HeldLock(File); + +impl Drop for HeldLock { + fn drop(&mut self) { + // The close that follows still releases the lock if this fails. + let _ = self.0.unlock(); + } +} + /// The failure-injection seam, and the deliberate exception to RUST-0008 /// (no traits for internal types): recovery is testable only if every /// phase can fail on demand. Production holds [`SystemIo`] alone; the @@ -243,6 +256,7 @@ impl Transaction { }; Error::new(ErrorKind::Io, message) })?; + let lock = HeldLock(lock); let state_directory = root.join(TRANSACTION_DIRECTORY); reject_symlink(&state_directory)?; let journal_path = state_directory.join(JOURNAL_FILE); @@ -627,6 +641,7 @@ pub(super) fn health_findings( fs::TryLockError::Error(error) => Err(io_error("lock", &lock_path, error)), }; } + let lock = lock.map(HeldLock); let state_directory = root.join(TRANSACTION_DIRECTORY); if !state_directory.exists() { @@ -656,7 +671,7 @@ fn incomplete_transaction_findings( root: PathBuf, state_directory: PathBuf, journal_path: PathBuf, - lock: File, + lock: HeldLock, ) -> Result, Error> { let content = fs::read_to_string(&journal_path) .map_err(|error| io_error("read", &journal_path, error))?; diff --git a/rune-docs/src/spec/transaction/tests.rs b/rune-docs/src/spec/transaction/tests.rs index 9d93c3eb..a1c56016 100644 --- a/rune-docs/src/spec/transaction/tests.rs +++ b/rune-docs/src/spec/transaction/tests.rs @@ -298,6 +298,24 @@ fn lock_contention_preserves_the_permanent_lock_file() { assert!(fixture.spec_root.base().join(LOCK_FILE).is_file()); } +/// A child process that another test thread spawns holds a copy of the +/// lock descriptor until its exec. The duplicate here stands for that copy. +#[test] +#[cfg_attr( + windows, + ignore = "the inherited copy is a Unix descriptor that shares the flock" +)] +fn a_dropped_transaction_releases_the_lock_that_a_duplicate_descriptor_shares() { + let fixture = transaction_fixture(); + let transaction = acquire(&fixture.spec_root).unwrap(); + let duplicate = transaction._lock.0.try_clone().unwrap(); + + drop(transaction); + + drop(acquire(&fixture.spec_root).unwrap()); + drop(duplicate); +} + #[test] fn prepared_recovery_discards_staged_state() { let fixture = fail_at_phase(Phase::Prepared);