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

  1. 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).
  2. Restart the peer to reopen file descriptors if the file was closed or the handle went stale after an unclean shutdown.
  3. Inspect peer logs immediately preceding this error for the root-cause OS error surfaced by errors.Wrapf (it contains the original Stat error).
  4. 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

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


AI-assisted analysis of hyperledger/fabric@2736b63f8f (2026-09-04). Data as JSON: /api/errors/0e5851ebca1abca0. Report an issue: GitHub.