{"record":{"id":"493f8b433062b342","repo":"apache/hadoop","slug":"attempted-to-seek-or-read-past-the-end-of-the-file-493f8b","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":"exception","errorClass":"EOFException","httpStatus":null,"severity":"error","filePath":"hadoop-cloud-storage-project/hadoop-cos/src/main/java/org/apache/hadoop/fs/cosn/CosNInputStream.java","lineNumber":150,"sourceCode":"    this.preReadPartSize = conf.getLong(\n        CosNConfigKeys.READ_AHEAD_BLOCK_SIZE_KEY,\n        CosNConfigKeys.DEFAULT_READ_AHEAD_BLOCK_SIZE);\n    this.maxReadPartNumber = conf.getInt(\n        CosNConfigKeys.READ_AHEAD_QUEUE_SIZE,\n        CosNConfigKeys.DEFAULT_READ_AHEAD_QUEUE_SIZE);\n\n    this.readAheadExecutorService = readAheadExecutorService;\n    this.readBufferQueue = new ArrayDeque<>(this.maxReadPartNumber);\n    this.closed = false;\n  }\n\n  private synchronized void reopen(long pos) throws IOException {\n    long partSize;\n\n    if (pos < 0) {\n      throw new EOFException(FSExceptionMessages.NEGATIVE_SEEK);\n    } else if (pos > this.fileSize) {\n      throw new EOFException(FSExceptionMessages.CANNOT_SEEK_PAST_EOF);\n    } else {\n      if (pos + this.preReadPartSize > this.fileSize) {\n        partSize = this.fileSize - pos;\n      } else {\n        partSize = this.preReadPartSize;\n      }\n    }\n\n    this.buffer = null;\n\n    boolean isRandomIO = true;\n    if (pos == this.nextPos) {\n      isRandomIO = false;\n    } else {\n      while (this.readBufferQueue.size() != 0) {\n        if (this.readBufferQueue.element().getStart() != pos) {\n          this.readBufferQueue.poll();\n        } else {","sourceCodeStart":132,"sourceCodeEnd":168,"githubUrl":"https://github.com/apache/hadoop/blob/2add9630210752f88ceb1bb74eb65e37bf41da8e/hadoop-cloud-storage-project/hadoop-cos/src/main/java/org/apache/hadoop/fs/cosn/CosNInputStream.java#L132-L168","documentation":"reopen(pos) raises EOFException('Attempted to seek or read past the end of the file') when pos exceeds fileSize — the stream knows the object length (store.getFileLength) and refuses positions beyond it. The boundary matters: pos == fileSize is allowed (positioned at EOF), only pos > fileSize throws. Reads at EOF then return -1 rather than throwing.","triggerScenarios":"seek(len + 1) after an off-by-one (seek(len) is legal); readers computing end offsets with '<=' loops; seeking to a stale larger length after the object was replaced by a shorter one; tail-like tools polling a growing file before the new length is visible.","commonSituations":"Split end computed as start + splitSize without clamping to file length; file shrank between status call and read; loop conditions using pos <= len instead of pos < len.","solutions":["Fix exclusive bounds: seeking to len is legal, beyond it is not — change <= to <.","Re-stat the file (getFileStatus().getLen()) if its length may have changed since open.","Clamp: seek(Math.min(pos, fs.getFileStatus(path).getLen()))."],"exampleFix":"// before\nlong len = fs.getFileStatus(p).getLen();\nfor (long pos = 0; pos <= len; pos += chunk) { in.seek(pos); } // throws at pos == len + ... \n\n// after\nfor (long pos = 0; pos < len; pos += chunk) { in.seek(pos); }","handlingStrategy":"validation","validationCode":"long len = in.getPos() >= 0 ? fs.getFileStatus(p).getLen() : 0;\nif (pos > len) {\n  pos = len; // position at EOF; subsequent read() returns -1\n}\nin.seek(pos);","typeGuard":"static boolean isSeekPastEof(Throwable t) {\n  return t instanceof EOFException && t.getMessage() != null\n      && t.getMessage().contains('past the end');\n}","tryCatchPattern":"try {\n  in.seek(pos);\n} catch (EOFException e) {\n  if (e.getMessage() != null && e.getMessage().contains('past the end')) {\n    in.seek(fs.getFileStatus(p).getLen()); // clamp to EOF\n  } else { throw e; }\n}","preventionTips":["Use exclusive loop bounds: pos < len, never pos <= len","Re-stat the file length if the object can be replaced or grow","Clamp split end offsets to the file length at split computation time"],"tags":["cosn","hadoop","input-stream","seek","eof","off-by-one"],"backgroundTag":"seek-past-eof","analyzedSha":"2add9630210752f88ceb1bb74eb65e37bf41da8e","analyzedAt":"2026-08-22T19:55:07.957Z","schemaVersion":2},"datasetVersion":"2026-08-22T20:17:22.307Z"}