{"record":{"id":"2fe2039f6cd1531e","repo":"apache/hadoop","slug":"failed-to-create-temporary-file-for-file-s","errorCode":null,"errorMessage":"Failed to create temporary file for {}.  File {} should not be present, but is.","messagePattern":"Failed to create temporary file for (.+?)\\.  File (.+?) should not be present, but is\\.","errorType":"exception","errorClass":"IOException","httpStatus":null,"severity":"error","filePath":"hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/datanode/DatanodeUtil.java","lineNumber":66,"sourceCode":"   */ \n  static IOException getCauseIfDiskError(IOException ioe) {\n    if (ioe.getMessage()!=null && ioe.getMessage().startsWith(DISK_ERROR)) {\n      return (IOException)ioe.getCause();\n    } else {\n      return null;\n    }\n  }\n\n  /**\n   * Create a new file.\n   * @throws IOException \n   * if the file already exists or if the file cannot be created.\n   */\n  public static File createFileWithExistsCheck(\n      FsVolumeSpi volume, Block b, File f,\n      FileIoProvider fileIoProvider) throws IOException {\n    if (fileIoProvider.exists(volume, f)) {\n      throw new IOException(\"Failed to create temporary file for \" + b\n          + \".  File \" + f + \" should not be present, but is.\");\n    }\n    // Create the zero-length temp file\n    final boolean fileCreated;\n    try {\n      fileCreated = fileIoProvider.createFile(volume, f);\n    } catch (IOException ioe) {\n      throw new IOException(DISK_ERROR + \"Failed to create \" + f, ioe);\n    }\n    if (!fileCreated) {\n      throw new IOException(\"Failed to create temporary file for \" + b\n          + \".  File \" + f + \" should be creatable, but is already present.\");\n    }\n    return f;\n  }\n  \n  /**\n   * @return the meta name given the block name and generation stamp.","sourceCodeStart":48,"sourceCodeEnd":84,"githubUrl":"https://github.com/apache/hadoop/blob/2add9630210752f88ceb1bb74eb65e37bf41da8e/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/datanode/DatanodeUtil.java#L48-L84","documentation":"IOException from DatanodeUtil.createFileWithExistsCheck: the DataNode is about to create the zero-length temporary file for a block being written, but a pre-check finds the target file already exists on the volume. Block temp files are expected to be unique for a new write; an existing file at that path means a stale leftover (previous write crashed before cleanup) or duplicate allocation of the same block+generation stamp.","triggerScenarios":"A client starts writing block b whose rbw temp file path (under current/BP-.../ subdir with in-progress name) already exists: typically after a DataNode crash/kill -9 mid-write so finalizing/cleanup never ran, or after aborted append/recovery sequences that left the temp file behind.","commonSituations":"DataNode power loss or OOM kill during heavy writes; repeated failed pipelines for the same block; volume state where a previous incarnation of the same block id and generation stamp was not cleaned; rare duplicate block allocation after NN failover.","solutions":["Trigger block recovery: restart the client application or let lease recovery / block recovery run, which finalizes or deletes the orphaned rbw file","Restart the DataNode - startup scanning and the block report reconcile in-progress files against the NameNode","If the block is already abandoned, delete the orphaned temp file by hand (path is printed in the message) once you confirm the block is not in any NN's block map (hdfs fsck)","Run hdfs fsck to confirm no corruption and let re-replication restore redundancy"],"exampleFix":"# before: write fails - 'File /data/dfs/current/BP-.../subdir12/blk_1073741825 should not be present, but is.'\n\n# after: verify the block is orphaned, then remove it and retry the write\nhdfs fsck / -files -blocks | grep blk_1073741825 || \\\n  rm /data/dfs/current/BP-*/subdir12/blk_1073741825   # stop DN first\nhdfs --daemon start datanode","handlingStrategy":"validation","validationCode":"// Before writing a block, confirm no stale temp file occupies the target path\nFile rbw = new File(blockDir, block.getBlockName() + \"__m\" + metaSuffix);\nif (rbw.exists()) {\n  // stale from a crashed writer: trigger/await block recovery instead of writing over it\n  throw new IOException(\"Stale rbw file present, run block recovery: \" + rbw);\n}","typeGuard":null,"tryCatchPattern":"try {\n  datanodeUtil.createFileWithExistsCheck(volume, block, file, fileIoProvider);\n} catch (IOException e) {\n  if (e.getMessage() != null && e.getMessage().contains(\"should not be present, but is\")) {\n    // stale temp file from a prior crashed write: coordinate lease/block recovery\n    // or remove the orphan after confirming the block is unknown to the NN\n  }\n}","preventionTips":["Stop DataNodes cleanly (hdfs --daemon stop) rather than kill -9 during write-heavy periods","After crashes, let lease recovery and block reports finish before restarting heavy write jobs","Never hand-delete in-progress (blk_*__*) files while the DN runs - go through recovery or a stopped DN","Periodically run hdfs fsck so orphaned in-progress blocks are surfaced and re-replicated"],"tags":["hdfs","datanode","temp-file","stale-state","block-write","crash-recovery"],"backgroundTag":"file-already-exists","analyzedSha":"2add9630210752f88ceb1bb74eb65e37bf41da8e","analyzedAt":"2026-08-22T19:55:07.957Z","schemaVersion":2},"datasetVersion":"2026-08-22T20:17:22.307Z"}