{"record":{"id":"62509b651bf12503","repo":"hyperledger/fabric","slug":"error-closing-temp-file-s","errorCode":null,"errorMessage":"error closing temp file '%s'","messagePattern":"error closing temp file '(.+?)'","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"core/chaincode/persistence/persistence.go","lineNumber":65,"sourceCode":"\t}\n\ttmpFile, err := os.CreateTemp(path, \".ccpackage.\")\n\tif err != nil {\n\t\treturn errors.Wrapf(err, \"error creating temp file in directory '%s'\", path)\n\t}\n\tdefer os.Remove(tmpFile.Name())\n\n\tif n, err := tmpFile.Write(data); err != nil || n != len(data) {\n\t\tif err == nil {\n\t\t\terr = errors.Errorf(\n\t\t\t\t\"failed to write the entire content of the file, expected %d, wrote %d\",\n\t\t\t\tlen(data), n,\n\t\t\t)\n\t\t}\n\t\treturn errors.Wrapf(err, \"error writing to temp file '%s'\", tmpFile.Name())\n\t}\n\n\tif err := tmpFile.Close(); err != nil {\n\t\treturn errors.Wrapf(err, \"error closing temp file '%s'\", tmpFile.Name())\n\t}\n\n\tif err := os.Rename(tmpFile.Name(), filepath.Join(path, name)); err != nil {\n\t\treturn errors.Wrapf(err, \"error renaming temp file '%s'\", tmpFile.Name())\n\t}\n\n\treturn nil\n}\n\n// Remove removes a file from the filesystem - used for rolling back an in-flight\n// Save operation upon a failure\nfunc (f *FilesystemIO) Remove(name string) error {\n\treturn os.Remove(name)\n}\n\n// ReadFile reads a file from the filesystem\nfunc (f *FilesystemIO) ReadFile(filename string) ([]byte, error) {\n\treturn os.ReadFile(filename)","sourceCodeStart":47,"sourceCodeEnd":83,"githubUrl":"https://github.com/hyperledger/fabric/blob/2736b63f8fd5932511d56fe68b7039d15977f7f6/core/chaincode/persistence/persistence.go#L47-L83","documentation":"After writing data, WriteFile explicitly closes the temp file before renaming. A close failure can indicate buffered data failed to flush to disk, so the operation aborts with this wrapped error rather than renaming an incompletely persisted file.","triggerScenarios":"tmpFile.Close() returns an error — typically ENOSPC when flushing buffered writes at close, or an I/O error on the underlying file descriptor.","commonSituations":"Disk filling up between the write and the close; NFS/network storage transient errors; file descriptor exhaustion affecting the peer process (EMFILE).","solutions":["Check and free disk space on the volume holding the package directory","Check the peer process's open file descriptor limit (ulimit -n) and raise if EMFILE","Inspect the wrapped OS error for the exact close failure and address storage-level issues, then retry"],"exampleFix":null,"handlingStrategy":"retry","validationCode":"if err := ensureDiskSpace(path, uint64(len(data))+1<<20); err != nil { return err }\nif err := checkFdLimit(); err != nil { return err }","typeGuard":null,"tryCatchPattern":"if err := io.WriteFile(path, name, data); err != nil {\n    var perr *os.PathError\n    if errors.As(err, &perr) && (errors.Is(perr.Err, syscall.ENOSPC) || errors.Is(perr.Err, syscall.EMFILE)) {\n        // free space / raise ulimit, then retry\n    }\n    return err\n}","preventionTips":["Keep disk headroom; close failures usually mean flush hit ENOSPC","Raise the peer process's file descriptor limit (ulimit -n)","Prefer local block storage over flaky network mounts for the peer data directory"],"tags":["filesystem","io","flush"],"backgroundTag":"disk-full","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"}