Hmbown/CodeWhale · error

Fleet ledger lock was replaced; reopen the workspace before…

Error message

Fleet ledger lock was replaced; reopen the workspace before continuing

What it means

The Fleet ledger keeps an open handle to its advisory lock file. open_lock_file re-opens the lock and compares it (via fs::same_file) to the handle captured at workspace open; if the inode differs, the lock file was deleted and recreated (or replaced) on disk, so continuing under the old handle would guard nothing. The ledger refuses to proceed and asks the caller to reopen the workspace.

Solutions

  1. Close the current FleetLedger/workspace handle and reopen the workspace so a fresh lock file and handle are captured, then retry the operation.
  2. Identify what deleted/recreated the lock file (cleaner daemon, competing tooling) and exclude the workspace lock path from it.
  3. If the workspace directory was rebuilt, restart all processes using it so every holder shares the same lock inode.
  4. Avoid manually deleting lock files under an active fleet run; stop the run first.

Example fix

// before
let ledger = FleetLedger::open(&path)?;
// ...someone deletes the lock file...
ledger.append(...)?; // fails
// after
let ledger = FleetLedger::open(&path)?; // reopen after the lock file was replaced
ledger.append(...)?;
Defensive patterns

Strategy: retry

Try / catch

match ledger.append(record) {
    Err(e) if e.to_string().contains("lock was replaced") => {
        let ledger = FleetLedger::open(&workspace)?; // reopen and retry once
        ledger.append(record)
    }
    other => other,
}

Prevention

When it happens

Trigger: Any ledger operation that takes a read or write lock after the ledger's lock file at lock_path was unlinked and recreated — e.g. an external process deleted the lock file, a cleanup job pruned it, or the workspace directory was rebuilt while the ledger was open.

Common situations: A tmp-cleaning daemon or `make clean`-style script removed lock files; the workspace was moved/re-synced (rsync, container restart) while a process still held it; two tooling generations use different lock-file lifecycles in the same directory.

Understand the failure class

Background: "This is a bug, please report it": internal invariant violations, unreachable panics, and SNH errors explained — this error's family across 47 libraries.

Related errors


AI-assisted analysis of Hmbown/CodeWhale@73e0f67d83 (2026-09-22). Data as JSON: /api/errors/94aca232dcd8dc63. Report an issue: GitHub.

Appendix: source

Thrown at crates/tui/src/fleet/ledger.rs:403

    #[cfg(test)]
    pub(crate) fn fail_next_start_append_after_callback(&self) {
        self.fail_start_append_after_callback
            .store(true, std::sync::atomic::Ordering::SeqCst);
    }

    #[cfg(test)]
    pub(crate) fn fail_next_restart_append_after_callback(&self) {
        self.fail_restart_append_after_callback
            .store(true, std::sync::atomic::Ordering::SeqCst);
    }

    fn open_lock_file(&self) -> Result<std::fs::File> {
        let file = self
            .lock_file
            .open_update(false, false)
            .with_context(|| format!("opening fleet ledger lock {}", self.lock_path.display()))?;
        if !same_file(&file, &self.original_lock)? {
            bail!("Fleet ledger lock was replaced; reopen the workspace before continuing");
        }
        Ok(file)
    }

    fn with_read_lock<T>(&self, action: impl FnOnce() -> Result<T>) -> Result<T> {
        let lock_file = self.open_lock_file()?;
        let lock = fd_lock::RwLock::new(lock_file);
        let _guard = lock
            .read()
            .with_context(|| format!("read-locking fleet ledger {}", self.ledger_path.display()))?;
        action()
    }

    fn with_write_lock<T>(&self, action: impl FnOnce() -> Result<T>) -> Result<T> {
        let lock_file = self.open_lock_file()?;
        let mut lock = fd_lock::RwLock::new(lock_file);
        let _guard = lock.write().with_context(|| {
            format!("write-locking fleet ledger {}", self.ledger_path.display())

View on GitHub (pinned to 73e0f67d83)