{"record":{"id":"28cf88971cd0caf9","repo":"tursodatabase/turso","slug":"invalid-mvcc-logical-log-frame-end-magic-at-offset","errorCode":null,"errorMessage":"invalid MVCC logical log frame end magic at offset {offset}","messagePattern":"invalid MVCC logical log frame end magic at offset (.+?)","errorType":"validation","errorClass":"anyhow::Error","httpStatus":null,"severity":"error","filePath":"cli/sync_server.rs","lineNumber":1096,"sourceCode":"        .and_then(|value| value.checked_add(payload_size))\n        .and_then(|value| value.checked_add(extension_size))\n        .ok_or_else(|| anyhow!(\"MVCC logical log frame offset overflow\"))?;\n    let frame_end = trailer_start\n        .checked_add(MVCC_TX_TRAILER_SIZE)\n        .ok_or_else(|| anyhow!(\"MVCC logical log frame end overflow\"))?;\n    if frame_end > log.len() {\n        return Ok(None);\n    }\n    let expected_crc = crc32c::crc32c_append(running_crc, &log[offset..trailer_start]);\n    let stored_crc = read_u32_le(log, trailer_start)?;\n    if stored_crc != expected_crc {\n        return Err(anyhow!(\n            \"MVCC logical log frame checksum mismatch at offset {offset}\"\n        ));\n    }\n    let end_magic = read_u32_le(log, trailer_start + 4)?;\n    if end_magic != MVCC_TX_END_MAGIC {\n        return Err(anyhow!(\n            \"invalid MVCC logical log frame end magic at offset {offset}\"\n        ));\n    }\n    Ok(Some((frame_end, stored_crc)))\n}\n\nfn read_u32_le(buf: &[u8], offset: usize) -> Result<u32> {\n    let bytes = buf\n        .get(offset..offset + 4)\n        .ok_or_else(|| anyhow!(\"buffer too short for u32 at offset {offset}\"))?;\n    Ok(u32::from_le_bytes(bytes.try_into().unwrap()))\n}\n\nfn read_u64_le(buf: &[u8], offset: usize) -> Result<u64> {\n    let bytes = buf\n        .get(offset..offset + 8)\n        .ok_or_else(|| anyhow!(\"buffer too short for u64 at offset {offset}\"))?;\n    Ok(u64::from_le_bytes(bytes.try_into().unwrap()))","sourceCodeStart":1078,"sourceCodeEnd":1114,"githubUrl":"https://github.com/tursodatabase/turso/blob/bad083fafbefdeae9a42ec19bdaaad8918dcf411/cli/sync_server.rs#L1078-L1114","documentation":"After the chained CRC check passes, the 8-byte trailer must end with magic 0x4554564D (MVCC_TX_END_MAGIC), read at trailer_start + 4. A CRC that matches while the end magic fails pinpoints corruption in the trailer's magic bytes themselves (the CRC does not cover the trailer), or a producer writing the two trailer fields in the wrong order. The scan stops at that frame.","triggerScenarios":"A frame whose stored trailer CRC matches but whose second trailer u32 is not 0x4554564D — trailer magic bytes damaged after writing, or a hand-built producer that writes end-magic before CRC in the 8-byte trailer.","commonSituations":"Tail-of-file damage (the trailer is the last 8 bytes written); producer code swapping trailer field order; partial writes of the 8-byte trailer after a crash; fuzzed trailers.","solutions":["Re-pull the log; the trailer is corrupt even though the frame body verified.","Hexdump the 8-byte trailer at the reported offset: crc32c u32 then 0x4554564D.","If writing a test producer, append the trailer in the fixed order CRC then end magic."],"exampleFix":"// before (log producer)\ntrailer.extend_from_slice(&MVCC_TX_END_MAGIC.to_le_bytes()); // end magic first\ntrailer.extend_from_slice(&frame_crc.to_le_bytes());\n\n// after\ntrailer.extend_from_slice(&frame_crc.to_le_bytes()); // CRC first\ntrailer.extend_from_slice(&MVCC_TX_END_MAGIC.to_le_bytes());","handlingStrategy":"try-catch","validationCode":null,"typeGuard":"fn frame_end_magic_ok(log: &[u8], trailer_start: usize) -> bool {\n    log.get(trailer_start + 4..trailer_start + 8)\n        .map(|b| u32::from_le_bytes(b.try_into().unwrap()))\n        .is_some_and(|m| m == MVCC_TX_END_MAGIC)\n}","tryCatchPattern":"match scan_mvcc_log(&log) {\n    Ok(snapshot) => { /* serve deltas */ }\n    Err(err) if err.to_string().contains(\"frame end magic\") => {\n        // trailer corrupt despite passing CRC: re-pull the log\n    }\n    Err(err) => return Err(err),\n}","preventionTips":["In producers, write the trailer as CRC then end magic, in that fixed order.","Write the full 8-byte trailer atomically with the frame body.","Treat tail-of-file damage (last bytes written) as the first suspect."],"tags":["mvcc","turso","sync","magic-number","log-corruption"],"backgroundTag":"invalid-file-magic","analyzedSha":"bad083fafbefdeae9a42ec19bdaaad8918dcf411","analyzedAt":"2026-08-16T23:12:11.798Z","schemaVersion":2},"datasetVersion":"2026-08-17T04:17:16.089Z"}