hyperledger/fabric · critical
error truncating the file [%s] to size [%d]
Error message
error truncating the file [%s] to size [%d]
What it means
blockfileWriter.truncateFile wraps any failure while inspecting or truncating the underlying block file down to a target size (used by addBlock when recovering from a partial write after a crash). The error wraps either a Stat() failure or reports the target size so operators can identify which segment file and offset were involved.
Source
Thrown at common/ledger/blkstorage/blockfile_rw.go:30
"github.com/hyperledger/fabric/internal/fileutil"
"github.com/pkg/errors"
)
// // WRITER ////
type blockfileWriter struct {
filePath string
file *os.File
}
func newBlockfileWriter(filePath string) (*blockfileWriter, error) {
writer := &blockfileWriter{filePath: filePath}
return writer, writer.open()
}
func (w *blockfileWriter) truncateFile(targetSize int) error {
fileStat, err := w.file.Stat()
if err != nil {
return errors.Wrapf(err, "error truncating the file [%s] to size [%d]", w.filePath, targetSize)
}
if fileStat.Size() > int64(targetSize) {
w.file.Truncate(int64(targetSize))
}
return nil
}
func (w *blockfileWriter) append(b []byte, sync bool) error {
_, err := w.file.Write(b)
if err != nil {
return err
}
if sync {
return w.file.Sync()
}
return nil
}
View on GitHub (pinned to 2736b63f8f)
Solutions
- Check filesystem health and permissions on the ledger's blockfiles directory (ls/stat the reported file path, verify disk is mounted read-write and not full).
- Restart the peer to reopen file descriptors if the file was closed or the handle went stale after an unclean shutdown.
- Inspect peer logs immediately preceding this error for the root-cause OS error surfaced by errors.Wrapf (it contains the original Stat error).
- If the blockstore is corrupt, restore the ledger data from backup or reset/re-bootstrap the peer for the channel.
Example fix
// root-cause: ledger dir not writable // before # peer starts, addBlock -> Stat fails on read-only mount // after $ mount -o remount,rw /var/hyperledger $ chown -R peer:peer /var/hyperledger/production # then restart peer
Defensive patterns
Strategy: try-catch
Validate before calling
path := filepath.Join(ledgerDir, "blockfile_000000")
if _, err := os.Stat(path); err != nil {
return fmt.Errorf("blockstore file inaccessible before peer start: %w", err)
}
if err := unix.Access(ledgerDir, unix.W_OK); err != nil {
return fmt.Errorf("ledger dir not writable: %w", err)
} Try / catch
err := mgr.addBlock(block)
if err != nil {
var wrapped interface{ Unwrap() error }
if errors.As(err, &target) && strings.Contains(err.Error(), "error truncating the file") {
logger.Criticalf("blockfile truncation failed: %v — check disk, permissions, and file state; restart peer", err)
}
return err
} Prevention
- Monitor disk health, free space, and mount read-only state on the ledger directory
- Keep ledger data files owned by the peer process user with correct permissions
- Avoid external processes touching/deleting files under the blockstore directory
- After unclean shutdowns, check peer logs for truncation/Stat errors before accepting traffic
- Back up the ledger directory so corruption can be restored without data loss
When it happens
Trigger: addBlock calls truncateFile to roll back a partially written block; os.File.Stat() fails (file closed/deleted, permissions, I/O error) and the wrapped error propagates out of the block commit path.
Common situations: Disk or filesystem errors on the peer's ledger directory; ledger data files removed or locked while the peer is running; recovery after an unclean shutdown where the file descriptor state is bad; permission changes on /var/hyperledger/production.
Related errors
- error opening block file writer for file %s
- failed writing file %s: %v
- error writing output
- error writing config update to output
- error opening block file reader for file %s
AI-assisted analysis of hyperledger/fabric@2736b63f8f (2026-09-04).
Data as JSON: /api/errors/0e5851ebca1abca0.
Report an issue: GitHub.