{"record":{"id":"91820d9fd44a675c","repo":"apache/hadoop","slug":"could-not-find-target-position","errorCode":null,"errorMessage":"Could not find target position {}","messagePattern":"Could not find target position (.+?)","errorType":"exception","errorClass":"EOFException","httpStatus":null,"severity":"error","filePath":"hadoop-hdfs-project/hadoop-hdfs-client/src/main/java/org/apache/hadoop/hdfs/DFSInputStream.java","lineNumber":517,"sourceCode":"    return fetchBlockAt(offset, 0, false); // don't use cache\n  }\n\n  /** Fetch a block from namenode and cache it */\n  private LocatedBlock fetchBlockAt(long offset, long length, boolean useCache)\n      throws IOException {\n    maybeRegisterBlockRefresh();\n    synchronized(infoLock) {\n      int targetBlockIdx = locatedBlocks.findBlock(offset);\n      if (targetBlockIdx < 0) { // block is not cached\n        targetBlockIdx = LocatedBlocks.getInsertIndex(targetBlockIdx);\n        useCache = false;\n      }\n      if (!useCache) { // fetch blocks\n        final LocatedBlocks newBlocks = (length == 0)\n            ? dfsClient.getLocatedBlocks(src, offset)\n            : dfsClient.getLocatedBlocks(src, offset, length);\n        if (newBlocks == null || newBlocks.locatedBlockCount() == 0) {\n          throw new EOFException(\"Could not find target position \" + offset);\n        }\n        // Update the LastLocatedBlock, if offset is for last block.\n        if (offset >= locatedBlocks.getFileLength()) {\n          setLocatedBlocksFields(newBlocks, getLastBlockLength(newBlocks));\n          // After updating the locatedBlock, the block to which the offset belongs\n          // should be researched like {@link DFSInputStream#getBlockAt(long)}.\n          if (offset >= locatedBlocks.getFileLength()) {\n            return locatedBlocks.getLastLocatedBlock();\n          } else {\n            targetBlockIdx = locatedBlocks.findBlock(offset);\n            assert targetBlockIdx >= 0 && targetBlockIdx < locatedBlocks.locatedBlockCount();\n          }\n        } else {\n          locatedBlocks.insertRange(targetBlockIdx,\n              newBlocks.getLocatedBlocks());\n        }\n      }\n      return locatedBlocks.get(targetBlockIdx);","sourceCodeStart":499,"sourceCodeEnd":535,"githubUrl":"https://github.com/apache/hadoop/blob/2add9630210752f88ceb1bb74eb65e37bf41da8e/hadoop-hdfs-project/hadoop-hdfs-client/src/main/java/org/apache/hadoop/hdfs/DFSInputStream.java#L499-L535","documentation":"EOFException from fetchBlockAt(): the client asked the NameNode for located blocks covering 'offset' and got back null or an empty list. There is simply no block at that position from the NN's point of view - typically the offset is past the data the NN knows because the file shrank or the cached length is stale.","triggerScenarios":"Reading/seeking at an offset beyond the file's real extent after a concurrent truncate; offset computed from a cached locatedBlocks set that outlived a file replacement; reading at exactly EOF during refresh.","commonSituations":"Tail loops that cache file length and keep reading after a log file was truncated/rotated; readers surviving a file being replaced while holding stale metadata.","solutions":["Bounds-check offset against a freshly fetched file length before each read/seek","Treat EOFException here as end-of-input in tail loops: re-stat the file and back off","Avoid concurrent truncate of files being read, or re-open after truncate"],"exampleFix":null,"handlingStrategy":"validation","validationCode":"long len = fs.getFileStatus(path).getLen();\nif (position >= len) {\n  return; // nothing to read past EOF\n}\nint n = in.read(position, buf, 0, buf.length);","typeGuard":"static boolean isTargetPositionMissing(IOException e) {\n  return e instanceof EOFException\n      && e.getMessage() != null && e.getMessage().contains(\"Could not find target position\");\n}","tryCatchPattern":"try {\n  /* read at offset */\n} catch (EOFException e) {\n  // file shrank or offset is past data known to the NN: re-stat and back off\n  revalidateFileLengthAndReopen();\n}","preventionTips":["Cache file length only briefly in tail loops; re-stat before each cycle","Do not truncate files that have active readers","Treat empty LocatedBlocks responses as EOF, not as an error to propagate"],"tags":["hdfs","eof","offset","read"],"backgroundTag":"end-of-file","analyzedSha":"2add9630210752f88ceb1bb74eb65e37bf41da8e","analyzedAt":"2026-08-22T19:55:07.957Z","schemaVersion":2},"datasetVersion":"2026-08-22T20:17:22.307Z"}