{"record":{"id":"1c1006aec7fc9e3b","repo":"dgraph-io/badger","slug":"initindex-crashed-v-s","errorCode":null,"errorMessage":"initIndex crashed: %v\n%s","messagePattern":"initIndex crashed: (.+?)\n(.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"critical","filePath":"table/table.go","lineNumber":349,"sourceCode":"\t\treturn nil, err\n\t}\n\treturn t, nil\n}\n\nfunc (t *Table) initBiggestAndSmallest() (err error) {\n\t// This defer will help gathering debugging info in case initIndex crashes.\n\tdefer func() {\n\t\tif r := recover(); r != nil {\n\t\t\tvar debugBuf bytes.Buffer\n\n\t\t\t// Best-effort debug collection: a panic here must not escape and\n\t\t\t// re-trigger the fatalpanic we are trying to fix. Setting err inside\n\t\t\t// this defer (rather than after the reads below) ensures whatever\n\t\t\t// debug info was gathered so far is attached even if one of the\n\t\t\t// reads panics.\n\t\t\tdefer func() {\n\t\t\t\t_ = recover()\n\t\t\t\terr = fmt.Errorf(\"initIndex crashed: %v\\n%s\", r, debugBuf.String())\n\t\t\t}()\n\n\t\t\t// Get the count of null bytes at the end of file. This is to make sure if there was an\n\t\t\t// issue with mmap sync or file copy.\n\t\t\tcount := 0\n\t\t\tfor i := len(t.Data) - 1; i >= 0; i-- {\n\t\t\t\tif t.Data[i] != 0 {\n\t\t\t\t\tbreak\n\t\t\t\t}\n\t\t\t\tcount++\n\t\t\t}\n\n\t\t\tfmt.Fprintf(&debugBuf, \"\\n== Recovering from initIndex crash ==\\n\")\n\t\t\tfmt.Fprintf(&debugBuf, \"File Info: [ID: %d, Size: %d, Zeros: %d]\\n\",\n\t\t\t\tt.id, t.tableSize, count)\n\n\t\t\tfmt.Fprintf(&debugBuf, \"isEnrypted: %v \", t.shouldDecrypt())\n","sourceCodeStart":331,"sourceCodeEnd":367,"githubUrl":"https://github.com/dgraph-io/badger/blob/2a001d466f6b71a917319a1db41f99860e16e269/table/table.go#L331-L367","documentation":"This message is produced by a deferred recover() handler in Table.initIndex. When opening a table (SST) file panics — typically because the mmap-slice t.Data contains zeros from a truncated, partially-synced, or botched file copy — initIndex converts the panic into this error, appending captured debug buffer contents for diagnosis.","triggerScenarios":"Calling db.Open (or badger.Open) where loading a table file panics inside initIndex; t.Data ends in null bytes because mmap sync failed, the file was copied while being written, or the file was truncated and re-extended with zeros.","commonSituations":"Copying SST files between machines without stopping Badger; crash/power-loss during mmap sync; restoring data directory from a partial backup; disk full during flush causing zero-padded files.","solutions":["Inspect the debug buffer text after 'crashed:' to identify the exact panic cause","Delete or move the corrupted table file out of the data directory and reopen the DB","Re-restore the data directory from a verified backup","Check for filesystem/disk issues and verify file sizes match the manifest","Never copy the data directory while the database is running; use DB.Backup/Stream instead"],"exampleFix":"// before: manually copying live SST files\ncp -r /data/badger /backup/badger\n\n// after: use a consistent snapshot\nopt := badger.DefaultOptions(\"/data/badger\").WithReadOnly(true)\ndb, _ := badger.Open(opt)\ndefer db.Close()\ndb.Backup(w, 0) // consistent backup instead of raw file copy","handlingStrategy":"validation","validationCode":"info, err := os.Stat(tablePath)\nif err != nil || info.Size() == 0 {\n    return fmt.Errorf(\"table %s is empty/truncated\", tablePath)\n}\n// stop Badger before copying directories; prefer db.Backup over file copies","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Never copy the data directory while Badger is running","Use db.Backup/db.Stream for backups instead of raw file copies","Ensure clean shutdown (db.Close) before host shutdown; handle SIGTERM","Monitor the debug text after 'crashed:' to find the root panic"],"tags":["data-corruption","panic","mmap","storage"],"backgroundTag":"sst-file-corruption","analyzedSha":"2a001d466f6b71a917319a1db41f99860e16e269","analyzedAt":"2026-09-05T13:00:02.264Z","contentChangedAt":"2026-09-05T13:00:02.264Z","schemaVersion":2},"datasetVersion":"2026-09-12T17:17:11.597Z"}