clockworklabs/SpacetimeDB · error · io::Error
failed to read segment header
Error message
failed to read segment header ({} bytes): {} What it means
segment::Header::decode performs read_exact of Header::LEN (10 bytes: 6-byte '(ds)^2' magic plus 4 config bytes). If the read fails, typically with UnexpectedEof on a short file, the error is re-wrapped with this message including the byte count and the original error. It means the file is too short to even contain a segment header.
Solutions
- Check the file length before decoding; files of Header::LEN bytes or less are treated as empty by the repo's own resume logic
- Delete the stub segment and let the log recreate it
- Verify the path really names a commitlog segment before feeding it to Header::decode
Example fix
// before
let header = segment::Header::decode(&mut file)?;
// after
let len = file.metadata()?.len();
if len <= segment::Header::LEN as u64 {
// treat as empty/stub segment: recreate instead of decode
return Ok(None);
}
let header = segment::Header::decode(&mut file)?; Defensive patterns
Strategy: validation
Validate before calling
let meta = std::fs::metadata(&path)?;
if meta.len() <= segment::Header::LEN as u64 {
// empty or header-only file: treat as absent, do not decode
return Ok(None);
}
let header = segment::Header::decode(&mut std::fs::File::open(&path)?)?; Type guard
fn is_short_header_read(err: &io::Error) -> bool {
err.to_string().contains("failed to read segment header")
} Try / catch
match segment::Header::decode(&mut file) {
Ok(h) => Ok(Some(h)),
Err(e) if is_short_header_read(&e) && e.kind() == io::ErrorKind::UnexpectedEof => {
Ok(None) // stub segment: caller recreates it
}
Err(e) => Err(e),
} Prevention
- Treat files of Header::LEN bytes or less as empty, mirroring the repo's own resume logic
- Write and flush the segment header immediately after creating a segment file
- Never point the repo at directories that may contain unrelated short files
When it happens
Trigger: Decoding a zero-byte or sub-10-byte file as a segment: a segment created but whose header write never flushed (crash between create and write), a file truncated externally, or opening an arbitrary non-segment file by mistake.
Common situations: Crash immediately after file creation; fallocated-but-never-written files; wrong path passed to the repo (pointing at random files); partial copies.
Related errors
- unexpected EOF while validating commit at byte offset
- all-zeroes commit header
- allocation size overflow
- Delete for non-existent row when replaying transaction
- [ ]
AI-assisted analysis of clockworklabs/SpacetimeDB@3653d2ed49 (2026-08-20).
Data as JSON: /api/errors/a084b209e9b14576.
Report an issue: GitHub.
Appendix: source
Thrown at crates/commitlog/src/segment.rs:50
pub struct Header {
pub log_format_version: u8,
pub checksum_algorithm: u8,
}
impl Header {
pub const LEN: usize = MAGIC.len() + /* log_format_version + checksum_algorithm + reserved + reserved */ 4;
pub fn write<W: io::Write>(&self, mut out: W) -> io::Result<()> {
out.write_all(&MAGIC)?;
out.write_all(&[self.log_format_version, self.checksum_algorithm, 0, 0])?;
Ok(())
}
pub fn decode<R: io::Read>(mut read: R) -> io::Result<Self> {
let mut buf = [0; Self::LEN];
read.read_exact(&mut buf).map_err(|e| {
io::Error::new(
e.kind(),
format!("failed to read segment header ({} bytes): {}", Self::LEN, e),
)
})?;
if !buf.starts_with(&MAGIC) {
return Err(io::Error::new(
io::ErrorKind::InvalidData,
format!(
"segment header does not start with magic: expected {:02x?}, got {:02x?}",
MAGIC,
&buf[..MAGIC.len()]
),
));
}
Ok(Self {
log_format_version: buf[MAGIC.len()],View on GitHub (pinned to 3653d2ed49)