{"record":{"id":"91641fdc5634cce7","repo":"apache/hadoop","slug":"failed-to-for-on-because-is-already","errorCode":null,"errorMessage":"Failed to {} {} for {} on {} because {} is already the current lease holder.","messagePattern":"Failed to (.+?) (.+?) for (.+?) on (.+?) because (.+?) is already the current lease holder\\.","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":2980,"sourceCode":"\n  boolean recoverLeaseInternal(RecoverLeaseOp op, INodesInPath iip,\n      String src, String holder, String clientMachine, boolean force)\n      throws IOException {\n    assert hasWriteLock(RwLockMode.GLOBAL);\n    INodeFile file = iip.getLastINode().asFile();\n    if (file.isUnderConstruction()) {\n      //\n      // If the file is under construction , then it must be in our\n      // leases. Find the appropriate lease record.\n      //\n      Lease lease = leaseManager.getLease(holder);\n\n      if (!force && lease != null) {\n        Lease leaseFile = leaseManager.getLease(file);\n        if (leaseFile != null && leaseFile.equals(lease)) {\n          // We found the lease for this file but the original\n          // holder is trying to obtain it again.\n          throw new AlreadyBeingCreatedException(\n              op.getExceptionMessage(src, holder, clientMachine,\n                  holder + \" is already the current lease holder.\"));\n        }\n      }\n      //\n      // Find the original holder.\n      //\n      FileUnderConstructionFeature uc = file.getFileUnderConstructionFeature();\n      String clientName = uc.getClientName();\n      lease = leaseManager.getLease(clientName);\n      if (lease == null) {\n        throw new AlreadyBeingCreatedException(\n            op.getExceptionMessage(src, holder, clientMachine,\n                \"the file is under construction but no leases found.\"));\n      }\n      if (force) {\n        // close now: no need to wait for soft lease expiration and \n        // close only the file src","sourceCodeStart":2962,"sourceCodeEnd":2998,"githubUrl":"https://github.com/apache/hadoop/blob/2add9630210752f88ceb1bb74eb65e37bf41da8e/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/namenode/FSNamesystem.java#L2962-L2998","documentation":"In the startFile/recoverFileLease path for an under-construction file: when the requesting holder already owns the file's lease and force=false, re-acquisition is refused with AlreadyBeingCreatedException('<holder> is already the current lease holder.') — the client is trying to open for write a file it itself already has open.","triggerScenarios":"The same clientName (DFSClient identity) calls create() on a path it currently holds a lease for without closing the first stream: duplicate job instance, task retry reusing the job's client name, or two threads in one JVM sharing a DFSClient and the same path.","commonSituations":"Speculative or retried tasks re-creating the same output file; apps that pin the DFS client name for idempotency and then run two writers; a service restarted in place re-creating its output path while the old handle is still open.","solutions":["Enforce one writer per file: reuse the existing stream or close it before re-creating","Use unique per-writer paths (part files keyed by attempt id) plus atomic rename at commit","If the earlier writer is dead but its lease lingers, call DistributedFileSystem#recoverLease(path), wait for isFileClosed, then re-create"],"exampleFix":"// before: retried tasks create the same file with the same client name\nfs.create(outPath, true);   // AlreadyBeingCreatedException\n// after: write private, commit by rename\nPath tmp = new Path(outPath.getParent(), \".tmp/\" + attemptId + \".\" + outPath.getName());\ntry (FSDataOutputStream out = fs.create(tmp, true)) { /* write */ }\nfs.rename(tmp, outPath);","handlingStrategy":"try-catch","validationCode":"if (!((DistributedFileSystem) fs).isFileClosed(path)) {\n  // something (possibly this client) still holds a lease: reuse, recover, or pick a new path\n}","typeGuard":null,"tryCatchPattern":"catch (AlreadyBeingCreatedException e) { if (e.getMessage() != null && e.getMessage().contains(\"already the current lease holder\")) { closePriorStream(); retryCreate(); } else throw e; }","preventionTips":["Track open streams in-process: one writer per path","Use unique temp names plus atomic rename instead of fixed output names for retried tasks"],"tags":["hdfs","lease","already-being-created","single-writer","concurrency"],"backgroundTag":"hdfs-lease-conflict","analyzedSha":"2add9630210752f88ceb1bb74eb65e37bf41da8e","analyzedAt":"2026-08-22T19:55:07.957Z","schemaVersion":2},"datasetVersion":"2026-08-23T01:17:44.959Z"}