{"record":{"id":"e3bae66195c8fec7","repo":"apache/hadoop","slug":"lease-mismatch-is-accessed-by-a-non-lease-hold","errorCode":null,"errorMessage":"Lease mismatch: {} is accessed by a non lease holder {}","messagePattern":"Lease mismatch: (.+?) is accessed by a non lease holder (.+?)","errorType":"exception","errorClass":"LeaseExpiredException","httpStatus":null,"severity":"error","filePath":"hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/namenode/FSNamesystem.java","lineNumber":6057,"sourceCode":"    }\n    if (storedBlock.getBlockUCState() != BlockUCState.UNDER_CONSTRUCTION) {\n      throw new IOException(\"Unexpected BlockUCState: \" + block\n          + \" is \" + storedBlock.getBlockUCState()\n          + \" but not \" + BlockUCState.UNDER_CONSTRUCTION);\n    }\n    \n    // check file inode\n    final INodeFile file = getBlockCollection(storedBlock);\n    if (file == null || !file.isUnderConstruction() || isFileDeleted(file)) {\n      throw new IOException(\"The file \" + storedBlock + \n          \" belonged to does not exist or it is not under construction.\");\n    }\n    \n    // check lease\n    if (clientName == null\n        || !clientName.equals(file.getFileUnderConstructionFeature()\n            .getClientName())) {\n      throw new LeaseExpiredException(\"Lease mismatch: \" + block + \n          \" is accessed by a non lease holder \" + clientName); \n    }\n\n    return file;\n  }\n  \n  /**\n   * Client is reporting some bad block locations.\n   */\n  void reportBadBlocks(LocatedBlock[] blocks) throws IOException {\n    checkOperation(OperationCategory.WRITE);\n    writeLock(RwLockMode.BM);\n    try {\n      checkOperation(OperationCategory.WRITE);\n      for (int i = 0; i < blocks.length; i++) {\n        ExtendedBlock blk = blocks[i].getBlock();\n        DatanodeInfo[] nodes = blocks[i].getLocations();\n        String[] storageIDs = blocks[i].getStorageIDs();","sourceCodeStart":6039,"sourceCodeEnd":6075,"githubUrl":"https://github.com/apache/hadoop/blob/2add9630210752f88ceb1bb74eb65e37bf41da8e/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/namenode/FSNamesystem.java#L6039-L6075","documentation":"checkUCBlock throws LeaseExpiredException when the clientName argument is null or differs from the client name in the file's FileUnderConstructionFeature: a non-lease-holder is attempting to update or recover the block. This is the single-writer enforcement at the block level.","triggerScenarios":"updateBlock/nextGenerationStamp invoked by a client that is not the writer: the lease expired (idle past soft/hard limits) and was recovered by someone else, or the writer is a duplicate attempt using a different client name.","commonSituations":"Writer paused longer than dfs.namenode.lease-hard-limit-sec (default ~1h); readers triggering recoverLease to unblock reads of an open file; speculative duplicate tasks writing the same output.","solutions":["Keep the lease alive during long pauses: hflush/hsync periodically or DFSClient keepalive","On catching LeaseExpiredException, call recoverLease, wait for isFileClosed, then append to continue","Use one writer per path with a unique client identity"],"exampleFix":"// before\nout.write(chunk); // hours later: LeaseExpiredException on next addBlock/close\n\n// after\n// renew the lease during idle periods\nif (now - lastWrite > leaseSoftLimit) { out.hflush(); } // or dfsClient.renewLease()","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try {\n  out.close(); // or addBlock path\n} catch (LeaseExpiredException e) {\n  // not the lease holder anymore: recover lease, wait, append to continue\n  if (dfs.recoverLease(path)) {\n    while (!dfs.isFileClosed(path)) { Thread.sleep(500); }\n    out = dfs.append(path);\n  }\n}","preventionTips":["Periodically hflush/hsync or renew the lease during long writes","Ensure only the owning client performs block updates","Design writers to resume via append after lease loss"],"tags":["hdfs","namenode","lease","single-writer","pipeline-recovery"],"backgroundTag":"lease-expired","analyzedSha":"2add9630210752f88ceb1bb74eb65e37bf41da8e","analyzedAt":"2026-08-22T19:55:07.957Z","schemaVersion":2},"datasetVersion":"2026-08-22T20:17:22.307Z"}