{"record":{"id":"4a292824f0af70b2","repo":"apache/hadoop","slug":"no-more-corrupt-file-blocks","errorCode":null,"errorMessage":"No more corrupt file blocks","messagePattern":"No more corrupt file blocks","errorType":"exception","errorClass":"NoSuchElementException","httpStatus":null,"severity":"warning","filePath":"hadoop-hdfs-project/hadoop-hdfs-client/src/main/java/org/apache/hadoop/hdfs/client/impl/CorruptFileBlockIterator.java","lineNumber":97,"sourceCode":"      // there are no more corrupt file blocks\n      nextPath = null;\n    } else {\n      nextPath = string2Path(files[fileIdx]);\n      fileIdx++;\n    }\n  }\n\n\n  @Override\n  public boolean hasNext() {\n    return nextPath != null;\n  }\n\n\n  @Override\n  public Path next() throws IOException {\n    if (!hasNext()) {\n      throw new NoSuchElementException(\"No more corrupt file blocks\");\n    }\n\n    Path result = nextPath;\n    loadNext();\n\n    return result;\n  }\n}\n","sourceCodeStart":79,"sourceCodeEnd":106,"githubUrl":"https://github.com/apache/hadoop/blob/2add9630210752f88ceb1bb74eb65e37bf41da8e/hadoop-hdfs-project/hadoop-hdfs-client/src/main/java/org/apache/hadoop/hdfs/client/impl/CorruptFileBlockIterator.java#L79-L106","documentation":"CorruptFileBlockIterator paginates the NameNode's list of corrupt files (listCorruptFileBlocks) and implements the standard Iterator contract: next() throws NoSuchElementException once nextPath is null. Because nextPath starts null when the first listing returns zero corrupt files, any unguarded next() call — first or after exhaustion — is a caller bug, not a cluster problem.","triggerScenarios":"Calling next() without guarding hasNext() (do/while loops, assumed non-empty listing); calling next() immediately on a healthy cluster where the first listCorruptFileBlocks response contains zero paths.","commonSituations":"Custom monitoring code scanning for corrupt files; loops converted from for-each to manual iterator handling; scripts that assume at least one corrupt file exists when corruption is suspected.","solutions":["Always guard with hasNext(): while (it.hasNext()) { Path p = it.next(); ... }.","Handle the zero-iteration case gracefully — an empty iteration is a healthy result.","Keep pagination looping until hasNext() is false; the iterator fetches further batches itself."],"exampleFix":"// before\ndo {\n  Path p = it.next();\n  audit(p);\n} while (true);\n\n// after\nwhile (it.hasNext()) {\n  Path p = it.next();\n  audit(p);\n}","handlingStrategy":"validation","validationCode":"RemoteIterator<Path> it = dfs.listCorruptFileBlocks(path);\nwhile (it.hasNext()) { // mandatory guard: listing may be empty\n  Path corrupt = it.next();\n  handle(corrupt);\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Never use NoSuchElementException for flow control — always guard with hasNext().","Handle zero iterations as the healthy case in corrupt-file scanners.","Prefer while-loop idioms over do-while when consuming RemoteIterator."],"tags":["hdfs","iterator","corrupt-files","api-misuse"],"backgroundTag":"iterator-exhausted","analyzedSha":"2add9630210752f88ceb1bb74eb65e37bf41da8e","analyzedAt":"2026-08-22T19:55:07.957Z","schemaVersion":2},"datasetVersion":"2026-08-23T01:17:44.959Z"}