{"record":{"id":"3dfa7045b9403527","repo":"clockworklabs/SpacetimeDB","slug":"unexpected-eof-while-validating-commit-at-byte-off","errorCode":null,"errorMessage":"unexpected EOF while validating commit at byte offset {byte_offset}","messagePattern":"unexpected EOF while validating commit at byte offset (.+?)","errorType":"exception","errorClass":"io::Error","httpStatus":null,"severity":"error","filePath":"crates/commitlog/src/segment.rs","lineNumber":540,"sourceCode":"\n/// Try to extract the commit header from the asked position without advancing seek.\n/// `IndexFileMut` fsync asynchoronously, which makes it important for reader to verify its entry\nfn validate_commit_at_byte_offset<Reader: io::Read + io::Seek>(\n    mut reader: &mut Reader,\n    byte_offset: u64,\n) -> io::Result<commit::Header> {\n    let pos = reader.stream_position()?;\n    reader.seek(SeekFrom::Start(byte_offset))?;\n\n    let hdr_or_error = StoredCommit::decode(&mut reader).and_then(|maybe_commit| {\n        let StoredCommit {\n            min_tx_offset,\n            epoch,\n            n,\n            records,\n            ..\n        } = maybe_commit.ok_or_else(|| {\n            io::Error::new(\n                ErrorKind::UnexpectedEof,\n                format!(\"unexpected EOF while validating commit at byte offset {byte_offset}\"),\n            )\n        })?;\n\n        Ok(commit::Header {\n            min_tx_offset,\n            epoch,\n            n,\n            len: records.len() as u32,\n        })\n    });\n\n    // Restore the original position\n    reader.seek(SeekFrom::Start(pos))?;\n\n    hdr_or_error\n}","sourceCodeStart":522,"sourceCodeEnd":558,"githubUrl":"https://github.com/clockworklabs/SpacetimeDB/blob/3653d2ed498fddbd83b7062aa6bf8970e18635ec/crates/commitlog/src/segment.rs#L522-L558","documentation":"validate_commit_at_byte_offset seeks to the byte offset recorded in the offset index and decodes a StoredCommit. Decode returned None (no bytes available), so the index points beyond the end of the actual commit data, typically into a truncated or preallocated tail, yielding ErrorKind::UnexpectedEof with the byte offset in the message.","triggerScenarios":"Index entry written before its commit data was flushed, then a crash left the data absent; segment truncated externally after the index was built; seeking to an entry that points past the last valid commit.","commonSituations":"Kill -9 mid-commit with the async index fsync ordering; disk-level truncation; replaying a directory restored from a snapshot taken mid-write.","solutions":["Rebuild the offset index from segment data (delete index files and reopen)","Treat trailing invalid data as recoverable: trim to the last valid commit and rebuild","Fall back to scanning the segment sequentially instead of trusting the index","Verify fsync ordering (data before index) in your build/deployment if this recurs"],"exampleFix":null,"handlingStrategy":"fallback","validationCode":null,"typeGuard":"fn is_index_eof(err: &io::Error) -> bool {\n    err.kind() == io::ErrorKind::UnexpectedEof\n        && err.to_string().contains(\"validating commit at byte offset\")\n}","tryCatchPattern":"match seek_with_index(&mut segment, &mut index, key) {\n    Ok(pos) => Some(pos),\n    Err(e) if is_index_eof(&e) => {\n        // index points past valid data: scan sequentially instead\n        scan_segment_for_tx(&mut segment, key).await.ok()\n    }\n    Err(e) => return Err(e),\n}","preventionTips":["Bound index entries to flushed commit data only (data fsync before index fsync)","Trim trailing invalid data on recovery so indexes never point into the tail","Rebuild indexes after any crash recovery event"],"tags":["rust","commitlog","offset-index","unexpected-eof","truncated"],"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"}