{"record":{"id":"13a2fdd74fcc2c4d","repo":"clockworklabs/SpacetimeDB","slug":"no-valid-commits-in-segment","errorCode":null,"errorMessage":"no valid commits in segment","messagePattern":"no valid commits in segment","errorType":"exception","errorClass":"std::io::Error","httpStatus":null,"severity":"critical","filePath":"crates/commitlog/src/repo/mod.rs","lineNumber":340,"sourceCode":"        .open_segment_reader(offset)\n        .map_err(|source| with_segment_context(\"opening segment for resume\", repo, offset, source))?;\n\n    // If the segment at `offset` is empty, remove it and try the previous.\n    // Return an error if no previous segment is found.\n    let len = reader\n        .segment_len()\n        .map_err(|source| with_segment_context(\"determining segment file size for resume\", repo, offset, source))?;\n    if len <= segment::Header::LEN as u64 {\n        debug!(\"repo {}: segment {} is empty\", repo, offset);\n        return Ok(ResumedSegment::Empty);\n    }\n\n    let guard_non_empty = |meta: &Metadata| match meta.tx_range.is_empty() {\n        true => Err(with_segment_context(\n            \"checking metadata\",\n            repo,\n            offset,\n            io::Error::new(io::ErrorKind::InvalidData, \"no valid commits in segment\"),\n        )),\n        false => Ok(()),\n    };\n\n    // The segment is now guaranteed to be non-empty, i.e. contain more bytes\n    // than the segment header.\n    //\n    // Traverse it to gather the `Metadata` and ensure that the segment is safe\n    // to resume, which is the case if:\n    //\n    // - it contains at least one commit\n    // - it does not contain corrupted commits\n    // - the existing segment passes the compatibility check\n    // - the existing segment's version is the same as\n    //   the one requested in `opts`\n    let offset_index = repo.get_offset_index(offset).ok();\n    let meta = match Metadata::extract(offset, &mut reader, offset_index.as_ref()) {\n        Err(error::SegmentMetadata::InvalidCommit { sofar, source }) => {","sourceCodeStart":322,"sourceCodeEnd":358,"githubUrl":"https://github.com/clockworklabs/SpacetimeDB/blob/3653d2ed498fddbd83b7062aa6bf8970e18635ec/crates/commitlog/src/repo/mod.rs#L322-L358","documentation":"While resuming an existing commitlog (repo/mod.rs), a segment file longer than segment::Header::LEN is traversed to build its Metadata. If the traversed tx_range ends up empty, the file has bytes beyond the header but not a single usable commit, and resume fails with io::ErrorKind::InvalidData 'no valid commits in segment'.","triggerScenarios":"Resuming after a crash that left a partially written or torn first commit in a fresh segment; a segment file preallocated with zeroes and longer than the header; on-disk corruption in the first commit record; copying a truncated segment file into the repo directory.","commonSituations":"Unclean shutdown (kill -9, power loss) immediately after segment creation; restoring partial backups; disk corruption or bit rot on the segment's first records.","solutions":["Archive and remove the offending segment so resume continues from the previous valid segment, recovering lost transactions from a replica/upstream","Prevent the torn tail in the first place: ensure graceful shutdown flushes commits, or trim trailing invalid data on open","Verify disk and filesystem health if corruption is suspected","Check that segment files copied into the repo were copied completely (checksum before restore)"],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":null,"typeGuard":"fn is_empty_segment_resume(err: &io::Error) -> bool {\n    err.kind() == io::ErrorKind::InvalidData && err.to_string().contains(\"no valid commits in segment\")\n}","tryCatchPattern":"match commitlog.resume(&repo, &opts) {\n    Ok(state) => Ok(state),\n    Err(e) if is_empty_segment_resume(&e) => {\n        // quarantine the named segment and retry resume without it,\n        // recovering the gap from a replica/upstream\n        quarantine_last_segment(&repo)?;\n        commitlog.resume(&repo, &opts)\n    }\n    Err(e) => Err(e),\n}","preventionTips":["Ensure graceful shutdown flushes the current segment, or trim trailing partial writes on open","Keep a replica or upstream source so a quarantined segment's data can be re-streamed","Checksum backup files before restoring them into the repo directory"],"tags":["rust","commitlog","corruption","resume","invalid-data"],"backgroundTag":"corrupt-segment-recovery","analyzedSha":"3653d2ed498fddbd83b7062aa6bf8970e18635ec","analyzedAt":"2026-08-20T06:08:37.179Z","contentChangedAt":"2026-08-20T06:08:37.179Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}