{"record":{"id":"02de6c601eb36b5d","repo":"hyperledger/fabric","slug":"failed-to-create-tar-for-chaincode-02de6c","errorCode":null,"errorMessage":"failed to create tar for chaincode","messagePattern":"failed to create tar for chaincode","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/peer/lifecycle/chaincode/package.go","lineNumber":191,"sourceCode":"\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,\n\t})\n\tif err != nil {\n\t\treturn err\n\t}\n\n\t_, err = tw.Write(payload)\n\tif err != nil {\n\t\treturn err","sourceCodeStart":173,"sourceCodeEnd":209,"githubUrl":"https://github.com/hyperledger/fabric/blob/2736b63f8fd5932511d56fe68b7039d15977f7f6/internal/peer/lifecycle/chaincode/package.go#L173-L209","documentation":"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.","triggerScenarios":"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).","commonSituations":"OOM pressure while packaging large chaincode payloads; a poisoned writer chain from earlier failures; corrupted/old fabric binaries.","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."],"exampleFix":"// before\nerr = tw.Close()\nif err == nil {\n    err = gw.Close()\n}\nif err != nil {\n    return nil, errors.Wrapf(err, \"failed to create tar for chaincode\")\n}\n// after: also close gzip regardless, and check both\ntarErr := tw.Close()\ngzErr := gw.Close()\nif tarErr != nil || gzErr != nil {\n    return nil, errors.Wrapf(errors.Join(tarErr, gzErr), \"failed to create tar for chaincode\")\n}","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"pkgBytes, err := getTarGzBytes()\nif err != nil {\n    if strings.Contains(err.Error(), \"failed to create tar for chaincode\") {\n        logger.Errorf(\"tar/gzip close failed: %v — retrying once\", err)\n        pkgBytes, err = getTarGzBytes() // transient memory issues often clear\n    }\n    if err != nil {\n        return err\n    }\n}","preventionTips":["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."],"tags":["hyperledger-fabric","tar","gzip","packaging"],"backgroundTag":"tar-close-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"}