hyperledger/fabric · error

failed to create tar for chaincode

Error message

failed to create tar for chaincode

What it means

At the end of getTarGzBytes, closing the tar.Writer and gzip.Writer flushes trailers/finalizes the archive. If either Close returns an error, it is wrapped as 'failed to create tar for chaincode'. The resulting in-memory package is considered invalid and is not returned.

Source

Thrown at internal/peer/lifecycle/chaincode/package.go:191

	codeBytes, err := p.PlatformRegistry.GetDeploymentPayload(strings.ToUpper(p.Input.Type), p.Input.Path)
	if err != nil {
		return nil, errors.WithMessage(err, "error getting chaincode bytes")
	}

	codePackageName := "code.tar.gz"

	err = writeBytesToPackage(tw, codePackageName, codeBytes)
	if err != nil {
		return nil, errors.Wrap(err, "error writing package code bytes to tar")
	}

	err = tw.Close()
	if err == nil {
		err = gw.Close()
	}
	if err != nil {
		return nil, errors.Wrapf(err, "failed to create tar for chaincode")
	}

	return payload.Bytes(), nil
}

func writeBytesToPackage(tw *tar.Writer, name string, payload []byte) error {
	err := tw.WriteHeader(&tar.Header{
		Name: name,
		Size: int64(len(payload)),
		Mode: 0o100644,
	})
	if err != nil {
		return err
	}

	_, err = tw.Write(payload)
	if err != nil {
		return err

View on GitHub (pinned to 2736b63f8f)

Solutions

  1. Read the wrapped inner error; it names whether tar.Close or gzip.Close failed.
  2. Free memory or increase limits and retry the package command.
  3. Fix any earlier write error first (metadata/code.tar.gz) since writer errors are sticky.
  4. If persistent, rebuild the peer CLI binaries from your pinned fabric version.

Example fix

// before
err = tw.Close()
if err == nil {
    err = gw.Close()
}
if err != nil {
    return nil, errors.Wrapf(err, "failed to create tar for chaincode")
}
// after: also close gzip regardless, and check both
tarErr := tw.Close()
gzErr := gw.Close()
if tarErr != nil || gzErr != nil {
    return nil, errors.Wrapf(errors.Join(tarErr, gzErr), "failed to create tar for chaincode")
}
Defensive patterns

Strategy: try-catch

Try / catch

pkgBytes, err := getTarGzBytes()
if err != nil {
    if strings.Contains(err.Error(), "failed to create tar for chaincode") {
        logger.Errorf("tar/gzip close failed: %v — retrying once", err)
        pkgBytes, err = getTarGzBytes() // transient memory issues often clear
    }
    if err != nil {
        return err
    }
}

Prevention

When it happens

Trigger: gzip.Writer.Close or tar.Writer.Close failing — typically after earlier write errors, or an I/O failure on the underlying bytes.Buffer (effectively only memory exhaustion).

Common situations: OOM pressure while packaging large chaincode payloads; a poisoned writer chain from earlier failures; corrupted/old fabric binaries.

Related errors


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