{"record":{"id":"496559b2651481ef","repo":"apache/hadoop","slug":"could-not-rename-temporary-file-to-due-to-fa","errorCode":null,"errorMessage":"Could not rename temporary file {} to {} due to failure in native rename. {}","messagePattern":"Could not rename temporary file (.+?) to (.+?) due to failure in native rename\\. (.+?)","errorType":"exception","errorClass":"IOException","httpStatus":null,"severity":"error","filePath":"hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/util/AtomicFileOutputStream.java","lineNumber":94,"sourceCode":"      triedToClose = true;\n      super.close();\n      success = true;\n    } finally {\n      if (success) {\n        boolean renamed = tmpFile.renameTo(origFile);\n        if (!renamed) {\n          // On windows, renameTo does not replace.\n          if (origFile.exists()) {\n            try {\n              Files.delete(origFile.toPath());\n            } catch (IOException e) {\n              throw new IOException(\"Could not delete original file \" + origFile, e);\n            }\n          }\n          try {\n            NativeIO.renameTo(tmpFile, origFile);\n          } catch (NativeIOException e) {\n            throw new IOException(\"Could not rename temporary file \" + tmpFile\n              + \" to \" + origFile + \" due to failure in native rename. \"\n              + e.toString());\n          }\n        }\n      } else {\n        if (!triedToClose) {\n          // If we failed when flushing, try to close it to not leak an FD\n          IOUtils.closeStream(out);\n        }\n        // close wasn't successful, try to delete the tmp file\n        if (!tmpFile.delete()) {\n          LOG.warn(\"Unable to delete tmp file \" + tmpFile);\n        }\n      }\n    }\n  }\n\n  /**","sourceCodeStart":76,"sourceCodeEnd":112,"githubUrl":"https://github.com/apache/hadoop/blob/2add9630210752f88ceb1bb74eb65e37bf41da8e/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/util/AtomicFileOutputStream.java#L76-L112","documentation":"AtomicFileOutputStream buffers output in a sibling '<name>.tmp' file and only publishes it over the target on close(), after flush+fsync. This IOException means close() completed the data writes but publication failed: plain File.renameTo returned false, the original file was deleted (the Windows fallback path), and NativeIO.renameTo then threw a NativeIOException whose errno text is appended. All written bytes still exist in the .tmp file; the target path may now be missing.","triggerScenarios":"Calling close() (directly or via try-with-resources) when the target is held open by another process (typical on Windows, where an open handle blocks replace), when the JVM user lacks write permission on the directory, when a stale '<name>.tmp' or the target is undeletable, or when the directory sits on a filesystem where rename fails (NFS, cross-device). The 'Could not delete original file' sibling error precedes it when origFile cannot be removed first.","commonSituations":"Writing fsimage.md5 or checkpoint metadata on Windows while antivirus/backup/indexing holds the file; read-only or root-owned dfs namenode directories after a permission change; storage on NFS mounts with non-POSIX rename semantics; leftover .tmp files from a previous crashed write.","solutions":["Identify the blocker: on Linux run 'lsof <target>' and 'lsof <target>.tmp'; on Windows use Process Explorer and exclude the Hadoop data dirs from antivirus scans, then retry the operation.","Verify directory permissions for the JVM user (chmod/chown on the parent directory, the target, and any leftover .tmp file).","Ensure the target directory is a local filesystem (same filesystem as the .tmp file); move Hadoop metadata off NFS or cross-device mounts.","If close() already failed, recover manually: clear the blocker, then 'mv <target>.tmp <target>' and regenerate any checksum sidecar (e.g., MD5FileUtils.saveMD5File) instead of re-running the whole job.","For transient locks (Windows brief opens), catch the IOException and retry close() once after a short delay before surfacing it."],"exampleFix":"// before\ntry (AtomicFileOutputStream out = new AtomicFileOutputStream(md5File)) {\n  out.write(md5Line.getBytes(StandardCharsets.UTF_8));\n} // close() may throw 'Could not rename temporary file' and leave data only in .tmp\n\n// after - pre-flight the publish path, then recover the .tmp on failure\nFile tmp = new File(md5File.getParentFile(), md5File.getName() + \".tmp\");\nif (tmp.exists() && !tmp.delete()) throw new IOException(\"Stale tmp undeletable: \" + tmp);\nif (md5File.exists() && !md5File.canWrite()) throw new IOException(\"Target not replaceable: \" + md5File);\ntry (AtomicFileOutputStream out = new AtomicFileOutputStream(md5File)) {\n  out.write(md5Line.getBytes(StandardCharsets.UTF_8));\n} catch (IOException e) {\n  // bytes survive in tmp; republish after clearing the lock rather than losing them\n  LOG.error(\"Publish failed; recover {} manually: {}\", tmp, e.getMessage());\n  throw e;\n}","handlingStrategy":"validation","validationCode":"File dst = new File(dir, \"image.md5\");\nFile tmp = new File(dst.getParentFile(), dst.getName() + \".tmp\");\nif (tmp.exists() && !tmp.delete()) throw new IOException(\"Cannot clear stale tmp: \" + tmp);\nif (dst.exists() && !dst.canWrite()) throw new IOException(\"Target not replaceable (locked?): \" + dst);\nif (!dst.getAbsoluteFile().getParentFile().canWrite())\n  throw new IOException(\"Directory not writable: \" + dst.getParentFile());","typeGuard":null,"tryCatchPattern":"try (AtomicFileOutputStream out = new AtomicFileOutputStream(dst)) {\n  out.write(data);\n} catch (IOException e) {\n  // message embeds tmpFile, origFile, and the NativeIOException errno text;\n  // written bytes survive in <dst>.tmp - recover them after clearing the lock\n  throw new IOException(\"Atomic publish of \" + dst + \" failed, recover \"\n      + dst + \".tmp after resolving: \" + e.getMessage(), e);\n}","preventionTips":["On Windows, exclude Hadoop data/metadata directories from antivirus, backup, and indexer scans so target files are never held open during close().","Always use try-with-resources so close() (the publish step) runs even when a write fails.","Keep atomic-published files on local filesystems, not NFS or cross-device mounts.","Delete stale '<name>.tmp' files before creating a new AtomicFileOutputStream for the same target."],"tags":["filesystem","rename","windows","native-io","close"],"backgroundTag":"file-rename-failed","analyzedSha":"2add9630210752f88ceb1bb74eb65e37bf41da8e","analyzedAt":"2026-08-22T19:55:07.957Z","schemaVersion":2},"datasetVersion":"2026-08-22T20:17:22.307Z"}