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 errView on GitHub (pinned to 2736b63f8f)
Solutions
- Read the wrapped inner error; it names whether tar.Close or gzip.Close failed.
- Free memory or increase limits and retry the package command.
- Fix any earlier write error first (metadata/code.tar.gz) since writer errors are sticky.
- 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
- Treat as transient first: retry the package command.
- Check memory availability when packaging large payloads.
- Clear earlier write errors — writer errors are sticky through Close.
- Verify tar.gz integrity with `tar -tzf basic.tar.gz` on success.
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
- Error writing %s to tar: %s
- failed to create tar for chaincode
- error writing package metadata to tar
- error writing package code bytes to tar
- illegal file name in payload: %s
AI-assisted analysis of hyperledger/fabric@2736b63f8f (2026-09-04).
Data as JSON: /api/errors/02de6c601eb36b5d.
Report an issue: GitHub.