{"record":{"id":"b1d4b995e75f2a77","repo":"hyperledger/fabric","slug":"error-reading-block-file-for-offset-d-and-length","errorCode":null,"errorMessage":"error reading block file for offset %d and length %d","messagePattern":"error reading block file for offset (.+?) and length (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"common/ledger/blkstorage/blockfile_rw.go","lineNumber":83,"sourceCode":"// //  READER ////\ntype blockfileReader struct {\n\tfile *os.File\n}\n\nfunc newBlockfileReader(filePath string) (*blockfileReader, error) {\n\tfile, err := os.OpenFile(filePath, os.O_RDONLY, 0o600)\n\tif err != nil {\n\t\treturn nil, errors.Wrapf(err, \"error opening block file reader for file %s\", filePath)\n\t}\n\treader := &blockfileReader{file}\n\treturn reader, nil\n}\n\nfunc (r *blockfileReader) read(offset int, length int) ([]byte, error) {\n\tb := make([]byte, length)\n\t_, err := r.file.ReadAt(b, int64(offset))\n\tif err != nil {\n\t\treturn nil, errors.Wrapf(err, \"error reading block file for offset %d and length %d\", offset, length)\n\t}\n\treturn b, nil\n}\n\nfunc (r *blockfileReader) close() error {\n\treturn errors.WithStack(r.file.Close())\n}\n","sourceCodeStart":65,"sourceCodeEnd":91,"githubUrl":"https://github.com/hyperledger/fabric/blob/2736b63f8fd5932511d56fe68b7039d15977f7f6/common/ledger/blkstorage/blockfile_rw.go#L65-L91","documentation":"blockfileReader.read wraps errors from file.ReadAt(b, offset): any failure reading `length` bytes at `offset` from an open block file. Beyond ordinary IO errors, ReadAt returns io.EOF when the requested range extends past end-of-file, so this error also indicates a truncated or corrupt block file.","triggerScenarios":"fetchRawBytes/read resolving an index pointer (fileLocPointer) whose offset+length exceeds the actual file size, or a low-level read failure (EBADF, EIO) on the open file descriptor.","commonSituations":"Truncated blockfile after a crash or disk-full during write; corrupted index entries pointing past EOF; ledger files manually edited or partially restored from backup; failing disk returning IO errors.","solutions":["Check the wrapped cause: io.EOF/'EOF' means the file is shorter than the index expects (truncation/corruption); EIO points to hardware/storage trouble.","Verify blockfile size against expectations (ls -l); a truncated file means restore ledgersData from a backup taken while the peer was stopped.","Rebuild the ledger: remove the affected channel's data and re-join/re-sync from orderers, or restore from a snapshot.","Check storage health (dmesg, smartctl) if EIO errors recur before re-provisioning the peer."],"exampleFix":"// before: querying block whose file was truncated by disk-full crash\n// ERROR: error reading block file for offset 104857600 and length 329: EOF\n// after (shell): restore full backup then restart\nsystemctl stop peer\nrsync -a /backup/ledgersData/chains/chains/mychannel/ /var/hyperledger/production/ledgersData/chains/chains/mychannel/\nsystemctl start peer","handlingStrategy":"fallback","validationCode":"// Go: sanity-check offset+length against file size before deep reads\nfunc rangeValid(path string, offset, length int) error {\n\tst, err := os.Stat(path)\n\tif err != nil {\n\t\treturn err\n\t}\n\tif int64(offset+length) > st.Size() {\n\t\treturn fmt.Errorf(\"requested range [%d,%d) exceeds file size %d\", offset, offset+length, st.Size())\n\t}\n\treturn nil\n}","typeGuard":null,"tryCatchPattern":"// Go: treat EOF as corruption and fall back to resync, retry other IO errors\nb, err := reader.read(offset, length)\nif err != nil {\n\tif err == io.EOF || errors.Cause(err) == io.EOF {\n\t\t// truncated/corrupt blockfile: trigger ledger restore or resync from peers\n\t} else if isRetryableIO(errors.Cause(err)) {\n\t\t// brief backoff and retry\n\t}\n\treturn err\n}","preventionTips":["Avoid hard power-offs/disk-full during block commits; monitor volume capacity.","Restore ledgersData only from full, consistent backups taken offline.","Watch for EIO in peer logs as an early disk-failure signal.","Use checksummed snapshots instead of manual file edits for ledger relocation."],"tags":["filesystem","io","corruption","ledger"],"backgroundTag":"file-read-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"}