{"record":{"id":"884e1093a570fbb1","repo":"hyperledger/fabric","slug":"could-not-truncate-current-file-to-known-size-in-d","errorCode":null,"errorMessage":"Could not truncate current file to known size in db: %s","messagePattern":"Could not truncate current file to known size in db: (.+?)","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"critical","filePath":"common/ledger/blkstorage/blockfile_mgr.go","lineNumber":129,"sourceCode":"\t\t\tpanic(fmt.Sprintf(\"Could not build blockfilesInfo info from block files: %s\", err))\n\t\t}\n\t\tlogger.Debugf(\"Info constructed by scanning the blocks dir = %s\", spew.Sdump(blockfilesInfo))\n\t} else {\n\t\tlogger.Debug(`Syncing block information from block storage (if needed)`)\n\t\tsyncBlockfilesInfoFromFS(rootDir, blockfilesInfo)\n\t}\n\terr = mgr.saveBlkfilesInfo(blockfilesInfo, true)\n\tif err != nil {\n\t\tpanic(fmt.Sprintf(\"Could not save next block file info to db: %s\", err))\n\t}\n\n\tcurrentFileWriter, err := newBlockfileWriter(deriveBlockfilePath(rootDir, blockfilesInfo.latestFileNumber))\n\tif err != nil {\n\t\tpanic(fmt.Sprintf(\"Could not open writer to current file: %s\", err))\n\t}\n\terr = currentFileWriter.truncateFile(blockfilesInfo.latestFileSize)\n\tif err != nil {\n\t\tpanic(fmt.Sprintf(\"Could not truncate current file to known size in db: %s\", err))\n\t}\n\tif mgr.index, err = newBlockIndex(indexConfig, indexStore); err != nil {\n\t\tpanic(fmt.Sprintf(\"error in block index: %s\", err))\n\t}\n\n\tmgr.blockfilesInfo = blockfilesInfo\n\tbsi, err := loadBootstrappingSnapshotInfo(rootDir)\n\tif err != nil {\n\t\treturn nil, err\n\t}\n\tmgr.bootstrappingSnapshotInfo = bsi\n\tmgr.currentFileWriter = currentFileWriter\n\tmgr.blkfilesInfoCond = sync.NewCond(&sync.Mutex{})\n\n\tif err := mgr.syncIndex(); err != nil {\n\t\treturn nil, err\n\t}\n","sourceCodeStart":111,"sourceCodeEnd":147,"githubUrl":"https://github.com/hyperledger/fabric/blob/2736b63f8fd5932511d56fe68b7039d15977f7f6/common/ledger/blkstorage/blockfile_mgr.go#L111-L147","documentation":"Panic when currentFileWriter.truncateFile cannot truncate the current block file to latestFileSize recorded in the DB checkpoint, ensuring the file length matches committed data. A mismatch means uncommitted bytes from a crash would be appended to, so startup aborts.","triggerScenarios":"truncateFile fails (I/O error, permission denied, file shorter/absent, read-only fs) aligning the block file to blockfilesInfo.latestFileSize in newBlockfileMgr.","commonSituations":"Block file missing or shrunk after a partial restore/backup mismatch (DB says file is 50MB, FS has 10MB); disk errors; permission issues on the chains dir.","solutions":["Restore the block files to match the index DB checkpoint (same backup) so the recorded latestFileSize is valid","If the FS is the source of truth, reset/sync the index DB (delete index so the peer rescans with constructBlockfilesInfo)","Fix permissions and filesystem health, then restart the peer"],"exampleFix":"// before: restoring chains files from an older backup than the index DB\n// after: restore both dirs atomically from one snapshot\n// systemctl stop peer && rsync -a snapshot/ledgersData/ ledgersData/ && systemctl start peer","handlingStrategy":"validation","validationCode":"fi, err := os.Stat(deriveBlockfilePath(rootDir, latestFileNumber))\nif err != nil { return err }\nif fi.Size() < recordedLatestFileSize {\n    return fmt.Errorf(\"block file %d smaller (%d) than db checkpoint (%d)\", latestFileNumber, fi.Size(), recordedLatestFileSize)\n}","typeGuard":null,"tryCatchPattern":"defer func() {\n    if r := recover(); r != nil {\n        err = fmt.Errorf(\"truncate to checkpoint size failed: %v\", r)\n    }\n}()","preventionTips":["Restore index DB and block files from the same backup point","Take backups only when the peer is stopped","Keep block files writable by the peer user","Monitor storage for I/O errors"],"tags":["filesystem","corruption","panic"],"backgroundTag":"file-truncate-failed","analyzedSha":"2736b63f8fd5932511d56fe68b7039d15977f7f6","analyzedAt":"2026-09-04T08:52:36.465Z","contentChangedAt":"2026-09-04T08:52:36.465Z","schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}