{"record":{"id":"88eacdb371966698","repo":"BoundaryML/baml","slug":"corrupt-tail-segment-in-profiling-stream","errorCode":null,"errorMessage":"corrupt tail segment in profiling stream","messagePattern":"corrupt tail segment in profiling stream","errorType":"validation","errorClass":"io::Error","httpStatus":null,"severity":"error","filePath":"baml_language/crates/bex_prof_store/src/prof/backend/store.rs","lineNumber":1030,"sourceCode":"        let Some(stem) = name.strip_suffix(&suffix) else {\n            continue;\n        };\n        if stem.len() != 20 || !stem.bytes().all(|byte| byte.is_ascii_digit()) {\n            continue;\n        }\n        let Ok(sequence) = stem.parse::<u64>() else {\n            continue;\n        };\n        if highest.as_ref().is_none_or(|(high, _)| sequence > *high) {\n            highest = Some((sequence, entry.path()));\n        }\n    }\n    let Some((sequence, path)) = highest else {\n        return Ok(0);\n    };\n    let bytes = fs::read(&path)?;\n    validate_checksum(&bytes).map_err(|_| {\n        io::Error::new(\n            io::ErrorKind::InvalidData,\n            \"corrupt tail segment in profiling stream\",\n        )\n    })?;\n    Ok(sequence)\n}\n\npub(crate) fn thread_ref_bytes(thread: ThreadRef) -> [u8; 32] {\n    let mut bytes = [0u8; 32];\n    bytes[..16].copy_from_slice(&thread.process_euid.0);\n    bytes[16..24].copy_from_slice(&thread.engine_id.0.to_be_bytes());\n    bytes[24..32].copy_from_slice(&thread.thread_id.0.to_be_bytes());\n    bytes\n}\n\n#[derive(Clone, Copy, Debug, PartialEq, Eq)]\nenum EncodeError {\n    StringTooLong,","sourceCodeStart":1012,"sourceCodeEnd":1048,"githubUrl":"https://github.com/BoundaryML/baml/blob/bd85ce9dee1463ff04d27efd20531013a4ff46c1/baml_language/crates/bex_prof_store/src/prof/backend/store.rs#L1012-L1048","documentation":"The profiling store's tail-segment reader finds the highest-numbered segment file on disk and validates its SHA-based checksum before reporting its sequence number. If validate_checksum rejects the bytes, the store raises InvalidData 'corrupt tail segment in profiling stream' because the last segment can no longer be trusted for sequencing.","triggerScenarios":"Calling the API that recovers the latest sequence number (fs::read of the highest segment path followed by validate_checksum) when the tail segment file is truncated, partially written after a crash, or otherwise fails its checksum.","commonSituations":"Process killed mid-write leaving a partial tail segment; disk corruption; copying or rsyncing the profiling store directory while writes were in flight; running out of disk space during segment flush.","solutions":["Delete or archive the corrupt tail segment file so recovery can fall back to the previous intact segment, then reopen the store.","Restore the profiling store directory from a known-good backup.","Check disk health/free space and rerun the workload after removing the damaged segment.","Avoid killing the process or copying the store directory while profiling writes are active."],"exampleFix":"// before\nlet seq = store.highest_sequence()?; // fails: tail segment corrupt\n// after\nlet seq = match store.highest_sequence() {\n    Ok(seq) => seq,\n    Err(e) if e.kind() == io::ErrorKind::InvalidData => {\n        fs::remove_file(tail_path)?; // drop corrupt tail, fall back to prior segment\n        store.highest_sequence()?\n    }\n};","handlingStrategy":"try-catch","validationCode":"let tail = find_highest_segment(&store_dir)?;\nif let Some(bytes) = tail.and_then(|p| fs::read(p).ok()) {\n    if validate_checksum(&bytes).is_err() {\n        // quarantine the corrupt tail before opening the store\n    }\n}","typeGuard":null,"tryCatchPattern":"match store.highest_sequence() {\n    Ok(seq) => seq,\n    Err(e) if e.kind() == io::ErrorKind::InvalidData && e.to_string().contains(\"corrupt tail segment\") => {\n        remove_tail_segment(&store_dir)?;\n        store.highest_sequence()?\n    }\n    Err(e) => return Err(e.into()),\n}","preventionTips":["Never kill the process or copy the store directory while profiling writes are active","Monitor free disk space so segment flushes are not truncated","Back up the store while quiescent, not mid-write"],"tags":["rust","io","corruption","profiling"],"backgroundTag":"checksum-mismatch","analyzedSha":"bd85ce9dee1463ff04d27efd20531013a4ff46c1","analyzedAt":"2026-09-12T03:38:25.718Z","contentChangedAt":"2026-09-12T03:38:25.718Z","schemaVersion":2},"datasetVersion":"2026-09-14T11:17:12.474Z"}