{"record":{"id":"74cda6784ab4d91f","repo":"apache/hadoop","slug":"failed-to-for-on-because-this-file-lea","errorCode":null,"errorMessage":"Failed to {} {} for {} on {} because this file lease is currently owned by {} on {}","messagePattern":"Failed to (.+?) (.+?) for (.+?) on (.+?) because this file lease is currently owned by (.+?) on (.+?)","errorType":"exception","errorClass":"AlreadyBeingCreatedException","httpStatus":null,"severity":"error","filePath":"hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/namenode/FSNamesystem.java","lineNumber":3029,"sourceCode":"          LOG.info(\"startFile: recover \" + lease + \", src=\" + src + \" client \"\n              + clientName);\n          if (internalReleaseLease(lease, src, iip, null)) {\n            return true;\n          } else {\n            throw new RecoveryInProgressException(\n                op.getExceptionMessage(src, holder, clientMachine,\n                    \"lease recovery is in progress. Try again later.\"));\n          }\n        } else {\n          final BlockInfo lastBlock = file.getLastBlock();\n          if (lastBlock != null\n              && lastBlock.getBlockUCState() == BlockUCState.UNDER_RECOVERY) {\n            throw new RecoveryInProgressException(\n                op.getExceptionMessage(src, holder, clientMachine,\n                    \"another recovery is in progress by \"\n                        + clientName + \" on \" + uc.getClientMachine()));\n          } else {\n            throw new AlreadyBeingCreatedException(\n                op.getExceptionMessage(src, holder, clientMachine,\n                    \"this file lease is currently owned by \"\n                        + clientName + \" on \" + uc.getClientMachine()));\n          }\n        }\n      }\n    } else {\n      return true;\n     }\n  }\n\n  /**\n   * Append to an existing file in the namespace.\n   */\n  LastBlockWithStatus appendFile(String srcArg, String holder,\n      String clientMachine, EnumSet<CreateFlag> flag, boolean logRetryCache)\n      throws IOException {\n    final String operationName = \"append\";","sourceCodeStart":3011,"sourceCodeEnd":3047,"githubUrl":"https://github.com/apache/hadoop/blob/2add9630210752f88ceb1bb74eb65e37bf41da8e/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/namenode/FSNamesystem.java#L3011-L3047","documentation":"The file's lease is alive, held by a different client, and has not passed the soft limit, so pre-emption is refused: AlreadyBeingCreatedException('this file lease is currently owned by <client> on <machine>.') — HDFS enforces single-writer semantics until the holder's lease lapses, is released, or is recovered.","triggerScenarios":"A second writer calls create/append on a file whose lease is being actively renewed by clientName from another machine, within dfs.namenode.lease-soft-limit-sec (default 60s) of the last renewal, with the last block not under recovery.","commonSituations":"Duplicate app instances pointed at the same output file; a stuck-but-alive previous writer still renewing; deployment scripts that restart a job while the old instance still heartbeats.","solutions":["Stop the current holder — its lease releases on clean close, or lapses at the soft limit after death — then retry","If the holder is known-dead, call recoverLease(path): the NN recovers at/after the soft limit and closes the file; then append or re-create","Adopt unique output paths plus atomic rename so two writers can never collide"],"exampleFix":"// before\nFSDataOutputStream out = fs.create(path, true);   // owned by another client\n// after\nDistributedFileSystem dfs = (DistributedFileSystem) fs;\nif (!dfs.isFileClosed(path)) { dfs.recoverLease(path); }\nwhile (!dfs.isFileClosed(path)) { Thread.sleep(5000L); }\nFSDataOutputStream out = fs.append(path);   // or fs.create(path, true)","handlingStrategy":"try-catch","validationCode":"DistributedFileSystem dfs = (DistributedFileSystem) fs;\nif (!dfs.isFileClosed(path)) { dfs.recoverLease(path); /* recovers at/after the 60s soft limit */ }","typeGuard":null,"tryCatchPattern":"catch (AlreadyBeingCreatedException e) { if (e.getMessage() != null && e.getMessage().contains(\"currently owned by\")) { dfs.recoverLease(src); waitForCloseThenAppend(); } else throw e; }","preventionTips":["Guarantee one writer per path via app-level locking or unique names","Confirm old writers are dead before restarting writes to the same path"],"tags":["hdfs","lease","single-writer","already-being-created","concurrency"],"backgroundTag":"hdfs-lease-conflict","analyzedSha":"2add9630210752f88ceb1bb74eb65e37bf41da8e","analyzedAt":"2026-08-22T19:55:07.957Z","schemaVersion":2},"datasetVersion":"2026-08-22T20:17:22.307Z"}