clockworklabs/SpacetimeDB · error · std::io::Error

repo

Error message

repo {}: {}: {}

What it means

Filesystem Repo::create_segment guards segment creation with an advisory lock file (<segment>.lock, flock-based) to prevent a create/check TOCTOU race. If acquiring the lock fails, the error is re-wrapped as 'repo {path}: {error}: {source}', keeping the original error kind so the real cause (permissions, IO) is still visible. The repo path prefix lets operators tell multiple data directories apart in logs.

Solutions

  1. Ensure exactly one writer process per commitlog directory - stop the other node or tool first.
  2. Verify the data directory is writable by the node's user (permissions, mount options, disk space).
  3. If a dead process left a stale lock, confirm it is really dead (ps/lsof), then remove the stale .lock file and retry.
Defensive patterns

Strategy: try-catch

Validate before calling

// Fail fast if the data directory is not writable before segment operations
let probe = repo_dir.join(".write-probe");
std::fs::write(&probe, b"")?;
std::fs::remove_file(&probe)?;

Try / catch

match repo.create_segment(offset, header) {
    Ok(seg) => seg,
    Err(e) => {
        // lock acquisition failure: another writer or a permissions/disk problem - stop, do not retry-loop
        tracing::error!("segment lock failed: {e}");
        return Err(e.into());
    }
}

Prevention

When it happens

Trigger: Another process holds or created the .lock file concurrently (two nodes, or node plus CLI, on one data directory); the data directory is unwritable so the lock file cannot be created; a hung process left the lock held; disk full or IO error while creating the lock file.

Common situations: Accidentally starting a second node on the same database directory; running repair/maintenance tools against a live node; container permission mismatches on mounted volumes.

Related errors


AI-assisted analysis of clockworklabs/SpacetimeDB@3653d2ed49 (2026-08-20). Data as JSON: /api/errors/b7e976d44e754429. Report an issue: GitHub.

Appendix: source

Thrown at crates/commitlog/src/repo/fs.rs:284

            Ok(len)
        }
    }
}

impl Repo for Fs {
    type SegmentWriter = File;
    type SegmentReader = ReadOnlySegment;

    fn create_segment(&self, offset: u64, header: segment::Header) -> io::Result<Self::SegmentWriter> {
        let path = self.segment_path(offset);

        // We need to check if the segment already exists,
        // so use file locking to prevent a TOCTOU race.
        // Using `flock` means we don't need to worry about stale lockfiles.
        let lock_path = path.0.with_extension("lock");
        let _lock = scopeguard::guard(
            lockfile::advisory::LockedFile::lock(&lock_path)
                .map_err(|e| io::Error::new(e.source.kind(), format!("repo {}: {}: {}", self, e, e.source)))?,
            |lockfile| {
                if let Err(e) = lockfile.release(true) {
                    // It's ok if removing the file fails, but print a warning
                    // anyways.
                    warn!("repo {}: failed to remove {}: {}", self, lock_path.display(), e);
                }
            },
        );

        // Check whether the segment already exists.
        // Overwrite it if its length is zero.
        match fs::metadata(&path) {
            Ok(stat) => {
                if stat.len() > 0 {
                    return Err(io::Error::new(
                        io::ErrorKind::AlreadyExists,
                        format!("repo {}: segment {} already exists and is non-empty", self, offset),
                    ));

View on GitHub (pinned to 3653d2ed49)