{"record":{"id":"a084b209e9b14576","repo":"clockworklabs/SpacetimeDB","slug":"failed-to-read-segment-header-bytes","errorCode":null,"errorMessage":"failed to read segment header ({} bytes): {}","messagePattern":"failed to read segment header \\((.+?) bytes\\): (.+?)","errorType":"exception","errorClass":"io::Error","httpStatus":null,"severity":"error","filePath":"crates/commitlog/src/segment.rs","lineNumber":50,"sourceCode":"pub struct Header {\n    pub log_format_version: u8,\n    pub checksum_algorithm: u8,\n}\n\nimpl Header {\n    pub const LEN: usize = MAGIC.len() + /* log_format_version + checksum_algorithm + reserved + reserved */ 4;\n\n    pub fn write<W: io::Write>(&self, mut out: W) -> io::Result<()> {\n        out.write_all(&MAGIC)?;\n        out.write_all(&[self.log_format_version, self.checksum_algorithm, 0, 0])?;\n\n        Ok(())\n    }\n\n    pub fn decode<R: io::Read>(mut read: R) -> io::Result<Self> {\n        let mut buf = [0; Self::LEN];\n        read.read_exact(&mut buf).map_err(|e| {\n            io::Error::new(\n                e.kind(),\n                format!(\"failed to read segment header ({} bytes): {}\", Self::LEN, e),\n            )\n        })?;\n\n        if !buf.starts_with(&MAGIC) {\n            return Err(io::Error::new(\n                io::ErrorKind::InvalidData,\n                format!(\n                    \"segment header does not start with magic: expected {:02x?}, got {:02x?}\",\n                    MAGIC,\n                    &buf[..MAGIC.len()]\n                ),\n            ));\n        }\n\n        Ok(Self {\n            log_format_version: buf[MAGIC.len()],","sourceCodeStart":32,"sourceCodeEnd":68,"githubUrl":"https://github.com/clockworklabs/SpacetimeDB/blob/3653d2ed498fddbd83b7062aa6bf8970e18635ec/crates/commitlog/src/segment.rs#L32-L68","documentation":"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.","triggerScenarios":"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.","commonSituations":"Crash immediately after file creation; fallocated-but-never-written files; wrong path passed to the repo (pointing at random files); partial copies.","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"],"exampleFix":"// before\nlet header = segment::Header::decode(&mut file)?;\n\n// after\nlet len = file.metadata()?.len();\nif len <= segment::Header::LEN as u64 {\n    // treat as empty/stub segment: recreate instead of decode\n    return Ok(None);\n}\nlet header = segment::Header::decode(&mut file)?;","handlingStrategy":"validation","validationCode":"let meta = std::fs::metadata(&path)?;\nif meta.len() <= segment::Header::LEN as u64 {\n    // empty or header-only file: treat as absent, do not decode\n    return Ok(None);\n}\nlet header = segment::Header::decode(&mut std::fs::File::open(&path)?)?;","typeGuard":"fn is_short_header_read(err: &io::Error) -> bool {\n    err.to_string().contains(\"failed to read segment header\")\n}","tryCatchPattern":"match segment::Header::decode(&mut file) {\n    Ok(h) => Ok(Some(h)),\n    Err(e) if is_short_header_read(&e) && e.kind() == io::ErrorKind::UnexpectedEof => {\n        Ok(None) // stub segment: caller recreates it\n    }\n    Err(e) => Err(e),\n}","preventionTips":["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"],"tags":["rust","commitlog","segment-header","unexpected-eof","truncated-file"],"backgroundTag":"unexpected-eof","analyzedSha":"3653d2ed498fddbd83b7062aa6bf8970e18635ec","analyzedAt":"2026-08-20T06:08:37.179Z","contentChangedAt":"2026-08-20T06:08:37.179Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}