Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
19 changes: 17 additions & 2 deletions rune-docs/src/spec/transaction/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,11 +157,24 @@ pub(crate) struct Transaction<I: TransactionIo = SystemIo> {
root: PathBuf,
state_directory: PathBuf,
journal_path: PathBuf,
_lock: File,
_lock: HeldLock,
io: I,
recovery: Option<RecoveryOutcome>,
}

/// 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
Expand Down Expand Up @@ -243,6 +256,7 @@ impl<I: TransactionIo> Transaction<I> {
};
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);
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -656,7 +671,7 @@ fn incomplete_transaction_findings(
root: PathBuf,
state_directory: PathBuf,
journal_path: PathBuf,
lock: File,
lock: HeldLock,
) -> Result<Vec<TransactionHealthFinding>, Error> {
let content = fs::read_to_string(&journal_path)
.map_err(|error| io_error("read", &journal_path, error))?;
Expand Down
18 changes: 18 additions & 0 deletions rune-docs/src/spec/transaction/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Loading