clockworklabs/SpacetimeDB · error · std::io::Error
repo : segment already exists and is non-empty
Error message
repo {}: segment {} already exists and is non-empty What it means
Filesystem Repo::create_segment permits re-creating a segment file only if it is zero-length (a leftover empty shell); if the target path already exists with bytes in it, creation is refused with AlreadyExists to avoid clobbering committed data. The message includes the repo path and the segment offset.
Solutions
- Use a fresh data directory when initializing a new log instead of reusing one with existing segment files.
- If intentional re-initialization is required, remove the old segment (and its .lock and index files) first.
- Check for and stop any concurrent writer on the same data directory.
Defensive patterns
Strategy: validation
Validate before calling
// Only attempt creation when the segment file is absent or empty
match std::fs::metadata(&segment_path) {
Err(e) if e.kind() == std::io::ErrorKind::NotFound => repo.create_segment(off, hdr),
Ok(m) if m.len() == 0 => repo.create_segment(off, hdr),
_ => repo.open_segment_writer(off), // existing non-empty segment: open instead
} Try / catch
match repo.create_segment(off, hdr) {
Ok(seg) => seg,
Err(e) if e.kind() == std::io::ErrorKind::AlreadyExists => repo.open_segment_writer(off)?,
Err(e) => return Err(e.into()),
} Prevention
- Treat the data directory as owned by a single log instance; never re-initialize in place.
- Compute new segment offsets from the log head, not from a cached or constant value.
- After restores, start with a clean directory or verify existing segments match the bookkeeping.
When it happens
Trigger: Calling create_segment for an offset that already has a populated file - e.g. re-initializing a log at an offset computed from stale state, epoch/truncation bookkeeping out of sync with the directory contents, or a second writer creating the segment first (the lock serializes but does not silence the second attempt).
Common situations: Restarting after a restore with mismatched segment files left in the directory; running two nodes on one directory; tools that recreate segments from an old offset map.
Related errors
- refusing to compress mutable segment
- repo
- repo : error getting file metadata for segment
- repo : segment already exists and is non-empty
- repo : too many empty segments
AI-assisted analysis of clockworklabs/SpacetimeDB@3653d2ed49 (2026-08-20).
Data as JSON: /api/errors/9a0fba9530712995.
Report an issue: GitHub.
Appendix: source
Thrown at crates/commitlog/src/repo/fs.rs:299
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),
));
}
}
Err(e) => {
if e.kind() != io::ErrorKind::NotFound {
return Err(io::Error::new(
e.kind(),
format!(
"repo {}: error getting file metadata for segment {}: {}",
self, offset, e
),
));
}
}
}
View on GitHub (pinned to 3653d2ed49)