{"record":{"id":"c371783444a77d9b","repo":"hyperledger/fabric","slug":"error-writing-package-code-bytes-to-tar","errorCode":null,"errorMessage":"error writing package code bytes to tar","messagePattern":"error writing package code bytes to tar","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/peer/lifecycle/chaincode/package.go","lineNumber":183,"sourceCode":"\tmetadataBytes, err := toJSON(normalizedPath, p.Input.Type, p.Input.Label)\n\tif err != nil {\n\t\treturn nil, err\n\t}\n\terr = writeBytesToPackage(tw, \"metadata.json\", metadataBytes)\n\tif err != nil {\n\t\treturn nil, errors.Wrap(err, \"error writing package metadata to tar\")\n\t}\n\n\tcodeBytes, err := p.PlatformRegistry.GetDeploymentPayload(strings.ToUpper(p.Input.Type), p.Input.Path)\n\tif err != nil {\n\t\treturn nil, errors.WithMessage(err, \"error getting chaincode bytes\")\n\t}\n\n\tcodePackageName := \"code.tar.gz\"\n\n\terr = writeBytesToPackage(tw, codePackageName, codeBytes)\n\tif err != nil {\n\t\treturn nil, errors.Wrap(err, \"error writing package code bytes to tar\")\n\t}\n\n\terr = tw.Close()\n\tif err == nil {\n\t\terr = gw.Close()\n\t}\n\tif err != nil {\n\t\treturn nil, errors.Wrapf(err, \"failed to create tar for chaincode\")\n\t}\n\n\treturn payload.Bytes(), nil\n}\n\nfunc writeBytesToPackage(tw *tar.Writer, name string, payload []byte) error {\n\terr := tw.WriteHeader(&tar.Header{\n\t\tName: name,\n\t\tSize: int64(len(payload)),\n\t\tMode: 0o100644,","sourceCodeStart":165,"sourceCodeEnd":201,"githubUrl":"https://github.com/hyperledger/fabric/blob/2736b63f8fd5932511d56fe68b7039d15977f7f6/internal/peer/lifecycle/chaincode/package.go#L165-L201","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Inspect the wrapped inner error; a prior metadata write failure often surfaces here.","Check host memory available to the peer CLI and retry with fewer concurrent operations.","Verify GetDeploymentPayload succeeded — an 'error getting chaincode bytes' upstream (bad --path, unsupported --lang) is often the real cause.","Rebuild/update the peer binary for your fabric version."],"exampleFix":"// before\nerr = writeBytesToPackage(tw, codePackageName, codeBytes)\nif err != nil {\n    return nil, errors.Wrap(err, \"error writing package code bytes to tar\")\n}\n// after: guard against empty payload from packager\nif len(codeBytes) == 0 {\n    return nil, errors.New(\"empty chaincode payload from platform registry\")\n}\nerr = writeBytesToPackage(tw, codePackageName, codeBytes)\nif err != nil {\n    return nil, errors.Wrap(err, \"error writing package code bytes to tar\")\n}","handlingStrategy":"try-catch","validationCode":"// Guard the deployment payload before tar packaging\npayload, err := registry.GetDeploymentPayload(strings.ToUpper(input.Type), input.Path)\nif err != nil {\n    return nil, errors.WithMessage(err, \"error getting chaincode bytes\")\n}\nif len(payload) == 0 {\n    return nil, errors.New(\"empty deployment payload; check --path and --lang\")\n}","typeGuard":null,"tryCatchPattern":"pkgBytes, err := getTarGzBytes()\nif err != nil {\n    if strings.Contains(err.Error(), \"error writing package code bytes to tar\") {\n        logger.Errorf(\"code.tar.gz tar write failed: %v\", err) // often caused by earlier write error or OOM\n    }\n    return err\n}","preventionTips":["Fix upstream GetDeploymentPayload errors first (bad --path/--lang).","Provide sufficient memory for packaging large chaincode payloads.","Retry on transient failures before re-diagnosing.","Validate the chaincode source builds locally before packaging."],"tags":["hyperledger-fabric","tar","packaging","io-error"],"backgroundTag":"tar-write-failed","analyzedSha":"2736b63f8fd5932511d56fe68b7039d15977f7f6","analyzedAt":"2026-09-04T08:52:36.465Z","contentChangedAt":"2026-09-04T08:52:36.465Z","schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}