{"record":{"id":"3ab1d3e471e2ad48","repo":"hashicorp/packer","slug":"failed-to-close-temporary-file-for-packer-sbom-s","errorCode":null,"errorMessage":"failed to close temporary file for Packer SBOM %s: %s","messagePattern":"failed to close temporary file for Packer SBOM (.+?): (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"packer/provisioner.go","lineNumber":368,"sourceCode":"\nfunc (p *SBOMInternalProvisioner) Provision(\n\tctx context.Context, ui packersdk.Ui, comm packersdk.Communicator,\n\tgeneratedData map[string]interface{},\n) error {\n\t// Original implementation - all logic now in hcp-sbom provisioner\n\tcwd, err := os.Getwd()\n\tif err != nil {\n\t\treturn fmt.Errorf(\"failed to get current working directory for Packer SBOM: %s\", err)\n\t}\n\n\ttmpFile, err := os.CreateTemp(cwd, \"packer-sbom-*.json\")\n\tif err != nil {\n\t\treturn fmt.Errorf(\"failed to create internal temporary file for Packer SBOM: %s\", err)\n\t}\n\n\ttmpFileName := tmpFile.Name()\n\tif err = tmpFile.Close(); err != nil {\n\t\treturn fmt.Errorf(\"failed to close temporary file for Packer SBOM %s: %s\", tmpFileName, err)\n\t}\n\n\tdefer func(name string) {\n\t\tfileRemoveErr := os.Remove(name)\n\t\tif fileRemoveErr != nil {\n\t\t\tlog.Printf(\"Error removing SBOM temporary file %s: %s\", name, fileRemoveErr)\n\t\t}\n\t}(tmpFile.Name())\n\n\tgeneratedData[\"dst\"] = tmpFile.Name()\n\n\terr = p.Provisioner.Provision(ctx, ui, comm, generatedData)\n\tif err != nil {\n\t\treturn err\n\t}\n\n\tpackerSbom, err := os.Open(tmpFileName)\n\tif err != nil {","sourceCodeStart":350,"sourceCodeEnd":386,"githubUrl":"https://github.com/hashicorp/packer/blob/eb36e3c3e48a036f3e8cc94087636ee72e1303c9/packer/provisioner.go#L350-L386","documentation":"Thrown during Provision when Packer's internal SBOM temporary file is created successfully but closing the open file handle fails with an OS-level error (e.g. fd exhaustion or an I/O error on Close). The temporary file's name is included so the operator can locate/clean up the leftover file. It wraps the underlying error from tmpFile.Close().","triggerScenarios":"Running a build where the HCP SBOM provisioner path in packer.Provision creates a temporary file via os.CreateTemp (in Packer's tmp directory) and the subsequent Close() call returns an error, typically due to file-descriptor exhaustion (EMFILE) or disk I/O failure.","commonSituations":"Systems with an exhausted ulimit -n (too many open files); disk errors or a full/failed filesystem; containers with limited fds; sandboxed environments blocking file operations in the temp directory.","solutions":["Raise the open-file limit (ulimit -n) and retry the build, since fd exhaustion is the most common cause","Free disk space / verify the health of the filesystem holding os.Tempdir()","Set PACKER_TMP_DIR (or TMPDIR) to a writable, healthy directory","Check kernel/container logs for I/O errors on the backing storage device"],"exampleFix":"// before\nif err = tmpFile.Close(); err != nil {\n  return fmt.Errorf(\"failed to close temporary file for Packer SBOM %s: %s\", tmpFileName, err)\n}\n// after\nif err = tmpFile.Close(); err != nil {\n  return fmt.Errorf(\"failed to close temporary file for Packer SBOM %s: %w\", tmpFileName, err)\n}","handlingStrategy":"try-catch","validationCode":"// Go: before running the build, ensure the process can allocate more fds\nimport \"syscall\"\nn, err := syscall.Getrlimit(syscall.RLIMIT_NOFILE)\nif err == nil && n.Cur < 4096 {\n    n.Cur = 4096\n    syscall.Setrlimit(syscall.RLIMIT_NOFILE, &n)\n}","typeGuard":null,"tryCatchPattern":"err := provisioner.Run(ctx, ui, comm)\nif err != nil {\n    var pathErr *os.PathError\n    if errors.As(err, &pathErr) && errors.Is(pathErr.Err, syscall.EMFILE) {\n        // raise ulimit / retry\n    }\n    return err\n}","preventionTips":["Run builds with a generous open-file ulimit (e.g. 4096+)","Monitor fd usage in long-running Packer processes","Keep the temp filesystem (TMPDIR / PACKER_TMP_DIR) healthy with free space","Watch system logs for I/O errors on the disk holding temp files"],"tags":["go","filesystem","temp-file","sbom"],"backgroundTag":"file-close-failed","analyzedSha":"eb36e3c3e48a036f3e8cc94087636ee72e1303c9","analyzedAt":"2026-09-05T13:20:43.127Z","contentChangedAt":"2026-09-05T13:20:43.127Z","schemaVersion":2},"datasetVersion":"2026-09-12T17:17:11.597Z"}