{"record":{"id":"5704e044b5cdaa8d","repo":"hyperledger/fabric","slug":"error-peeking-d-bytes-from-block-file","errorCode":null,"errorMessage":"error peeking [%d] bytes from block file","messagePattern":"error peeking \\[(.+?)\\] bytes from block file","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"common/ledger/blkstorage/block_stream.go","lineNumber":106,"sourceCode":"\n\tif fileInfo, err = s.file.Stat(); err != nil {\n\t\treturn nil, nil, errors.Wrapf(err, \"error getting block file stat\")\n\t}\n\tif s.currentOffset == fileInfo.Size() {\n\t\tlogger.Debugf(\"Finished reading file number [%d]\", s.fileNum)\n\t\treturn nil, nil, nil\n\t}\n\tremainingBytes := fileInfo.Size() - s.currentOffset\n\t// Peek 8 or smaller number of bytes (if remaining bytes are less than 8)\n\t// Assumption is that a block size would be small enough to be represented in 8 bytes varint\n\tpeekBytes := 8\n\tif remainingBytes < int64(peekBytes) {\n\t\tpeekBytes = int(remainingBytes)\n\t\tmoreContentAvailable = false\n\t}\n\tlogger.Debugf(\"Remaining bytes=[%d], Going to peek [%d] bytes\", remainingBytes, peekBytes)\n\tif lenBytes, err = s.reader.Peek(peekBytes); err != nil {\n\t\treturn nil, nil, errors.Wrapf(err, \"error peeking [%d] bytes from block file\", peekBytes)\n\t}\n\tlength, n := protowire.ConsumeVarint(lenBytes)\n\tif n <= 0 {\n\t\t// proto.DecodeVarint did not consume any byte at all which means that the bytes\n\t\t// representing the size of the block are partial bytes\n\t\tif !moreContentAvailable {\n\t\t\treturn nil, nil, ErrUnexpectedEndOfBlockfile\n\t\t}\n\t\tpanic(errors.Errorf(\"Error in decoding varint bytes [%#v]\", lenBytes))\n\t}\n\tbytesExpected := int64(n) + int64(length)\n\tif bytesExpected > remainingBytes {\n\t\tlogger.Debugf(\"At least [%d] bytes expected. Remaining bytes = [%d]. Returning with error [%s]\",\n\t\t\tbytesExpected, remainingBytes, ErrUnexpectedEndOfBlockfile)\n\t\treturn nil, nil, ErrUnexpectedEndOfBlockfile\n\t}\n\t// skip the bytes representing the block size\n\tif _, err = s.reader.Discard(n); err != nil {","sourceCodeStart":88,"sourceCodeEnd":124,"githubUrl":"https://github.com/hyperledger/fabric/blob/2736b63f8fd5932511d56fe68b7039d15977f7f6/common/ledger/blkstorage/block_stream.go#L88-L124","documentation":"Wraps the bufio.Reader.Peek error when fewer than the requested peekBytes are available in the buffer/file. peekBytes is clamped to remainingBytes beforehand, so this normally signals a real read/buffer problem rather than plain EOF. Peek returns io.EOF when it cannot fill the requested count.","triggerScenarios":"nextBlockBytesAndPlacementInfo calls s.reader.Peek(peekBytes) where peekBytes was already clamped to remainingBytes (fileSize - currentOffset); an error means the buffered reader could not return even the clamped byte count.","commonSituations":"File truncated concurrently after the Stat call (reader sees fewer bytes than expected); I/O error from the underlying file; racing writers repositioning the file.","solutions":["Verify the blockfile is not being truncated or modified while the stream is open (stop peer or external writers)","Retry the stream from a stable offset; transient I/O errors may resolve","If the file shrank (rolled back improperly), run peer rollback/rebuild to resync ledger files with the checkpoint"],"exampleFix":null,"handlingStrategy":"retry","validationCode":"if info, err := os.Stat(blockfilePath); err == nil && info.Size() < currentOffset {\n    return fmt.Errorf(\"file shrank below offset %d; rollback needed\", currentOffset)\n}","typeGuard":null,"tryCatchPattern":"blk, err := stream.nextBlockBytes()\nif err != nil {\n    if errors.Is(errors.Cause(err), io.EOF) || errors.Is(errors.Cause(err), io.ErrUnexpectedEOF) {\n        return rescanFromLastCheckpoint()\n    }\n    return err\n}","preventionTips":["Keep the peer stopped during any file-level maintenance","Use peer rollback when ledger files and checkpoint diverge instead of hand-editing","Monitor for concurrent truncation of ledger files"],"tags":["fabric","ledger","bufio","io"],"backgroundTag":"bufio-peek-short-read","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"}