{"record":{"id":"6060cf2ea48d810e","repo":"spacejam/sled","slug":"encountered-corrupted-settings-cookie-with-mismatched-crc","errorCode":null,"errorMessage":"encountered corrupted settings cookie with mismatched CRC.","messagePattern":"encountered corrupted settings cookie with mismatched CRC\\.","errorType":"error_code","errorClass":null,"httpStatus":null,"severity":"critical","filePath":"src/heap.rs","lineNumber":264,"sourceCode":"            Err(e) if e.kind() == std::io::ErrorKind::NotFound => {\n                std::fs::write(settings_path, self.serialize())\n            }\n            Err(e) => Err(e),\n        }\n    }\n\n    fn deserialize(buf: &[u8]) -> io::Result<PersistentSettings> {\n        let mut cursor = buf;\n        let mut buf = [0_u8; 64];\n        cursor.read_exact(&mut buf)?;\n\n        let version = u16::from_le_bytes([buf[0], buf[1]]);\n\n        let crc_actual = (crc32fast::hash(&buf[0..60]) ^ 0xAF).to_le_bytes();\n        let crc_expected = &buf[60..];\n\n        if crc_actual != crc_expected {\n            return Err(io::Error::new(\n                io::ErrorKind::InvalidData,\n                \"encountered corrupted settings cookie with mismatched CRC.\",\n            ));\n        }\n\n        match version {\n            1 => {\n                let leaf_fanout =\n                    u64::from_le_bytes(buf[2..10].try_into().unwrap());\n                Ok(PersistentSettings::V1 { leaf_fanout })\n            }\n            _ => Err(io::Error::new(\n                io::ErrorKind::InvalidData,\n                \"encountered unknown version number when reading settings cookie\",\n            )),\n        }\n    }\n","sourceCodeStart":246,"sourceCodeEnd":282,"githubUrl":"https://github.com/spacejam/sled/blob/e449d17111f4a097e1c66b6db241962ccb6a4136/src/heap.rs#L246-L282","documentation":"The persistent settings cookie (the on-disk block recording format version and leaf fanout) embeds a CRC32 checksum. On deserialize, the stored CRC does not match the CRC computed over the first 60 bytes, so the settings block is considered corrupt and opening fails rather than proceeding with garbage configuration.","triggerScenarios":"Opening a database whose settings cookie bytes were corrupted: torn writes during a crash/power loss, disk/bit rot, or manual byte editing of the database file.","commonSituations":"Recovering a database after an unclean shutdown or power failure; copying database files with a tool that truncates or corrupts them; hardware failure on the storage device.","solutions":["Restore the database from a backup, since the settings block is unrecoverable in place","Check disk health (SMART) and filesystem integrity before retrying","If the data is expendable, delete the database files and re-create/re-import it","Verify file transfer integrity (checksums) if the corruption appeared after copying"],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":"// before opening, sanity-check the file exists and has a plausible size\nlet meta = std::fs::metadata(&path)?;\nif meta.len() < 64 { return Err(anyhow!(\"db file suspiciously small/corrupt\")); }","typeGuard":null,"tryCatchPattern":"match Db::open(&path) {\n    Ok(db) => db,\n    Err(e) if e.to_string().contains(\"corrupted settings cookie\") => {\n        // restore from backup or recreate\n        restore_backup(&path)?;\n        Db::open(&path)?\n    }\n    Err(e) => return Err(e.into()),\n}","preventionTips":["Always keep verified backups of the database directory","Shut down cleanly; avoid killing the process with SIGKILL during writes","Use reliable storage and monitor SMART/fsck status","Checksum database files when copying between machines"],"tags":["rust","corruption","crc","storage"],"backgroundTag":"checksum-mismatch","analyzedSha":"e449d17111f4a097e1c66b6db241962ccb6a4136","analyzedAt":"2026-09-12T01:23:10.985Z","contentChangedAt":"2026-09-12T01:23:10.985Z","schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}