{"record":{"id":"946b02a881544493","repo":"ipfs/kubo","slug":"closing-temp-file-w","errorCode":null,"errorMessage":"closing temp file: %w","messagePattern":"closing temp file: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"core/commands/update.go","lineNumber":727,"sourceCode":"\tif _, err := af.Write(data); err != nil {\n\t\t_ = af.Abort()\n\t\treturn err\n\t}\n\n\treturn af.Close()\n}\n\n// writeBinaryToTempFile writes data to a uniquely named executable file\n// in the system temp directory and returns its path.\nfunc writeBinaryToTempFile(data []byte, ver string) (path string, err error) {\n\tpattern := migrations.ExeName(fmt.Sprintf(\"ipfs-%s-*\", ver))\n\tf, err := os.CreateTemp(\"\", pattern)\n\tif err != nil {\n\t\treturn \"\", fmt.Errorf(\"creating temp file: %w\", err)\n\t}\n\tdefer func() {\n\t\tif cerr := f.Close(); cerr != nil && err == nil {\n\t\t\terr = fmt.Errorf(\"closing temp file: %w\", cerr)\n\t\t}\n\t\tif err != nil {\n\t\t\tos.Remove(f.Name())\n\t\t}\n\t}()\n\n\tif _, err = f.Write(data); err != nil {\n\t\treturn \"\", fmt.Errorf(\"writing temp file: %w\", err)\n\t}\n\tif err = f.Sync(); err != nil {\n\t\treturn \"\", fmt.Errorf(\"syncing temp file: %w\", err)\n\t}\n\tif err = f.Chmod(0o755); err != nil {\n\t\treturn \"\", fmt.Errorf(\"chmod temp file: %w\", err)\n\t}\n\treturn f.Name(), nil\n}\n","sourceCodeStart":709,"sourceCodeEnd":745,"githubUrl":"https://github.com/ipfs/kubo/blob/329838acdfafae224582930457efe80aa217afc0/core/commands/update.go#L709-L745","documentation":"In `writeBinaryToTempFile`, a deferred closure surfaces a non-nil error from `f.Close()` only when no earlier error was set, wrapping it with this message. A failed close on the temp executable means buffered data may not have been handed to the OS and the file cannot be trusted; the path result is discarded and the temp file is removed.","triggerScenarios":"`ipfs update` binary staging when closing the temp file fails: file descriptor exhaustion (EMFILE), an I/O error already pending on the descriptor (disk full surfaced at close for buffered writers), or the filesystem being unmounted mid-write.","commonSituations":"Very high fd usage in long-lived wrappers; disk filling during the update (free space ran out between create and close); network filesystems dropping connections during the download-to-temp flow.","solutions":["Free disk space on the temp filesystem and retry: `df -h \"$TMPDIR\"`.","Check for fd leaks/limits: `ulimit -n`, `ls /proc/self/fd | wc -l`; raise the soft limit if near it.","Inspect `dmesg` for underlying storage errors on the temp filesystem.","Simply rerun the update after the transient condition clears — the failed temp file is removed automatically."],"exampleFix":"# before\nulimit -n 256\nipfs update install v0.30.0   # closing temp file: too many open files\n\n# after\nulimit -n 4096\nipfs update install v0.30.0","handlingStrategy":"retry","validationCode":"// raise fd headroom before long-running update wrappers\nif n, _ := rlimit.Nofile(); n.Cur < 1024 {\n\trlimit.SetNofile(1024) // or ulimit -n 4096 in shell\n}","typeGuard":null,"tryCatchPattern":"path, err := writeBinaryToTempFile(data, ver)\nif err != nil {\n\tvar perr *os.PathError\n\tif errors.As(err, &perr) && (errors.Is(perr.Err, syscall.EMFILE) || errors.Is(perr.Err, syscall.EIO)) {\n\t\t// transient: raise limits / wait for disk, then retry once\n\t\treturn retryWithBackoff(updateOnce, 1)\n\t}\n\treturn err\n}","preventionTips":["Run updates from a clean environment without hundreds of open file descriptors.","Keep a healthy fd limit (>= 1024) for the process running the update.","Watch for EIO trends via dmesg and replace failing storage proactively.","Retry once after transient failures — partial temp files are cleaned up automatically."],"tags":["io","file-close","temp-file","update"],"backgroundTag":"file-close-failed","analyzedSha":"329838acdfafae224582930457efe80aa217afc0","analyzedAt":"2026-09-03T18:30:52.135Z","contentChangedAt":"2026-09-03T18:30:52.135Z","schemaVersion":2},"datasetVersion":"2026-09-11T00:17:11.886Z"}