hyperledger/fabric · error

error writing package code bytes to tar

Error message

error writing package code bytes to tar

What it means

In getTarGzBytes the chaincode deployment payload returned by the platform registry is written into the tar as code.tar.gz. If writeBytesToPackage fails here, the error is wrapped with 'error writing package code bytes to tar'. This means the tar/gzip writer rejected the chaincode payload write.

Source

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

	metadataBytes, err := toJSON(normalizedPath, p.Input.Type, p.Input.Label)
	if err != nil {
		return nil, err
	}
	err = writeBytesToPackage(tw, "metadata.json", metadataBytes)
	if err != nil {
		return nil, errors.Wrap(err, "error writing package metadata to tar")
	}

	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,

View on GitHub (pinned to 2736b63f8f)

Solutions

  1. Inspect the wrapped inner error; a prior metadata write failure often surfaces here.
  2. Check host memory available to the peer CLI and retry with fewer concurrent operations.
  3. Verify GetDeploymentPayload succeeded — an 'error getting chaincode bytes' upstream (bad --path, unsupported --lang) is often the real cause.
  4. Rebuild/update the peer binary for your fabric version.

Example fix

// before
err = writeBytesToPackage(tw, codePackageName, codeBytes)
if err != nil {
    return nil, errors.Wrap(err, "error writing package code bytes to tar")
}
// after: guard against empty payload from packager
if len(codeBytes) == 0 {
    return nil, errors.New("empty chaincode payload from platform registry")
}
err = writeBytesToPackage(tw, codePackageName, codeBytes)
if err != nil {
    return nil, errors.Wrap(err, "error writing package code bytes to tar")
}
Defensive patterns

Strategy: try-catch

Validate before calling

// Guard the deployment payload before tar packaging
payload, err := registry.GetDeploymentPayload(strings.ToUpper(input.Type), input.Path)
if err != nil {
    return nil, errors.WithMessage(err, "error getting chaincode bytes")
}
if len(payload) == 0 {
    return nil, errors.New("empty deployment payload; check --path and --lang")
}

Try / catch

pkgBytes, err := getTarGzBytes()
if err != nil {
    if strings.Contains(err.Error(), "error writing package code bytes to tar") {
        logger.Errorf("code.tar.gz tar write failed: %v", err) // often caused by earlier write error or OOM
    }
    return err
}

Prevention

When it happens

Trigger: The tar writer is already in an error state (e.g. from an earlier failed write or close), or the in-memory buffer write fails during packaging of an unusually large chaincode payload.

Common situations: Very large chaincode build payloads exhausting memory (OOM kills buffer growth); a poisoned writer from the preceding metadata.json write; binary mismatch in custom-built peer binaries.

Related errors


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