{"record":{"id":"5cea7b1718415bf3","repo":"apache/hadoop","slug":"failed-to-for-on-because-the-file-is-u","errorCode":null,"errorMessage":"Failed to {} {} for {} on {} because the file is under construction but no leases found.","messagePattern":"Failed to (.+?) (.+?) for (.+?) on (.+?) because the file is under construction but no leases found\\.","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":2992,"sourceCode":"\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\n        LOG.info(\"recoverLease: \" + lease + \", src=\" + src +\n          \" from client \" + clientName);\n        return internalReleaseLease(lease, src, iip, holder);\n      } else {\n        assert lease.getHolder().equals(clientName) :\n          \"Current lease holder \" + lease.getHolder() +\n          \" does not match file creator \" + clientName;\n        //\n        // If the original holder has not renewed in the last SOFTLIMIT \n        // period, then start lease recovery.\n        //\n        if (lease.expiredSoftLimit()) {","sourceCodeStart":2974,"sourceCodeEnd":3010,"githubUrl":"https://github.com/apache/hadoop/blob/2add9630210752f88ceb1bb74eb65e37bf41da8e/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/namenode/FSNamesystem.java#L2974-L3010","documentation":"The file's INode still carries FileUnderConstructionFeature with a recorded clientName, but the LeaseManager has no live lease for that client — write state is inconsistent (UC feature persisted, lease gone). Re-opening therefore fails with AlreadyBeingCreatedException('the file is under construction but no leases found.')","triggerScenarios":"startFile/create against an under-construction file whose owning lease disappeared — typically right after a NameNode failover/restart while lease tables are rebuilt from the edit log, or after lease-checkpoint edge cases.","commonSituations":"Writer process died and a NameNode failover followed; a retried job hits the orphaned under-construction file before the new NN finishes internal recovery; rare races between hard-limit lease monitor cleanup and re-opens.","solutions":["Force closure: DistributedFileSystem#recoverLease(path) or `hdfs debug recoverLease -path <path>`, wait for isFileClosed(path), then re-create","If the NameNode just failed over, wait briefly — lease reconstruction from the edit log often finalizes the file on its own","If unrecoverable, move/delete the orphaned file (its data is incomplete anyway) and rewrite it"],"exampleFix":"// before\nFSDataOutputStream out = fs.create(path, true);   // AlreadyBeingCreatedException: no leases found\n// after\nDistributedFileSystem dfs = (DistributedFileSystem) fs;\nif (!dfs.isFileClosed(path)) { dfs.recoverLease(path); }\nwhile (!dfs.isFileClosed(path)) { Thread.sleep(1000); }\nFSDataOutputStream out = fs.create(path, true);","handlingStrategy":"try-catch","validationCode":"DistributedFileSystem dfs = (DistributedFileSystem) fs;\nif (!dfs.isFileClosed(path)) { dfs.recoverLease(path); /* wait for isFileClosed before re-create */ }","typeGuard":null,"tryCatchPattern":"catch (AlreadyBeingCreatedException e) { if (e.getMessage() != null && e.getMessage().contains(\"no leases found\")) { dfs.recoverLease(src); waitForFileClosed(dfs, src); retryCreate(); } else throw e; }","preventionTips":["After a NameNode failover, expect a settling window before re-creating in-flight write files","Design write paths so they can be abandoned and retried under new names"],"tags":["hdfs","lease","orphaned-lease","namenode-failover","under-construction"],"backgroundTag":"hdfs-lease-conflict","analyzedSha":"2add9630210752f88ceb1bb74eb65e37bf41da8e","analyzedAt":"2026-08-22T19:55:07.957Z","schemaVersion":2},"datasetVersion":"2026-08-22T20:17:22.307Z"}