{"record":{"id":"76978868849e1788","repo":"apache/hadoop","slug":"attempted-to-seek-or-read-past-the-end-of-the-file-769788","errorCode":null,"errorMessage":"Attempted to seek or read past the end of the file","messagePattern":"Attempted to seek or read past the end of the file","errorType":"validation","errorClass":"EOFException","httpStatus":null,"severity":"error","filePath":"hadoop-tools/hadoop-azure/src/main/java/org/apache/hadoop/fs/azurebfs/services/AbfsInputStream.java","lineNumber":779,"sourceCode":"  }\n\n  /**\n   * Seek to given position in stream.\n   * @param n position to seek to\n   * @throws IOException if there is an error\n   * @throws EOFException if attempting to seek past end of file\n   */\n  @Override\n  public synchronized void seek(long n) throws IOException {\n    LOG.debug(\"requested seek to position {}\", n);\n    if (closed) {\n      throw new IOException(FSExceptionMessages.STREAM_IS_CLOSED);\n    }\n    if (n < 0) {\n      throw new EOFException(FSExceptionMessages.NEGATIVE_SEEK);\n    }\n    if (n > contentLength) {\n      throw new EOFException(FSExceptionMessages.CANNOT_SEEK_PAST_EOF);\n    }\n\n    if (streamStatistics != null) {\n      streamStatistics.seek(n, fCursor);\n    }\n\n    // next read will read from here\n    nextReadPos = n;\n    LOG.debug(\"set nextReadPos to {}\", nextReadPos);\n  }\n\n  @Override\n  public synchronized long skip(long n) throws IOException {\n    if (closed) {\n      throw new IOException(FSExceptionMessages.STREAM_IS_CLOSED);\n    }\n    long currentPos = getPos();\n    if (currentPos == contentLength) {","sourceCodeStart":761,"sourceCodeEnd":797,"githubUrl":"https://github.com/apache/hadoop/blob/2add9630210752f88ceb1bb74eb65e37bf41da8e/hadoop-tools/hadoop-azure/src/main/java/org/apache/hadoop/fs/azurebfs/services/AbfsInputStream.java#L761-L797","documentation":"AbfsInputStream.seek(long n) throws EOFException(\"Attempted to seek or read past the end of the file\") when n > contentLength. contentLength is the length captured when the stream was opened (appends after open are not reflected), so the check uses the stream's snapshot, not a live stat. Seeking exactly to contentLength is legal (EOF position).","triggerScenarios":"Calling seek(n) with n greater than the file's length: hardcoded expected sizes, indexes built against a longer version of the file, stale FileStatus/FileLength from an earlier stat, or a file truncated/overwritten between stat and seek; off-by-one bugs seeking to len+1.","commonSituations":"File replaced concurrently by a writer producing a shorter file; directory metadata caches handing out old lengths; merge/compaction jobs that shrink files while readers hold open streams; input-split computation done from outdated FileStatus.","solutions":["Clamp the seek target to the stream's own length: n = Math.min(n, in.length())","Stat fresh before seeking when files can change: fs.getFileStatus(path).getLen()","If the file legitimately shrank, reopen the stream so contentLength is refreshed","Handle the end case explicitly: if target >= length, treat as EOF instead of seeking"],"exampleFix":"// before\nin.seek(offsetFromIndexFile);  // offset > current file length -> EOFException\n\n// after\nlong target = Math.min(offsetFromIndexFile, in.length());\nif (target >= in.length()) {\n  return -1; // or handle EOF explicitly\n}\nin.seek(target);","handlingStrategy":"validation","validationCode":"// Bound every seek by the stream's own length snapshot\nlong len = in.length();\nif (target > len) target = len;          // clamp to EOF\nif (target >= len && needData) {\n  // nothing left to read — treat as EOF, don't seek\n}\nin.seek(target);","typeGuard":null,"tryCatchPattern":"try {\n  in.seek(target);\n} catch (EOFException e) {\n  long len = fs.getFileStatus(path).getLen();   // file may have shrunk\n  if (target > len) { /* stale index/length — rebuild it */ }\n  else throw e;\n}","preventionTips":["Re-stat files that can be rewritten concurrently before trusting cached lengths","Validate index offsets against fs.getFileStatus(path).getLen() before use","Treat seek-to-EOF as a normal end condition, not an error path"],"tags":["azure-blob","abfs","seek","eof","file-length","validation"],"backgroundTag":"seek-past-eof","analyzedSha":"2add9630210752f88ceb1bb74eb65e37bf41da8e","analyzedAt":"2026-08-22T19:55:07.957Z","schemaVersion":2},"datasetVersion":"2026-08-22T20:17:22.307Z"}