apache/hadoop · error · EOFException
Cannot seek to a negative offset
Error message
Cannot seek to a negative offset
What it means
AbfsInputStream.seek(long n) throws EOFException(FSExceptionMessages.NEGATIVE_SEEK, "Cannot seek to a negative offset") when n < 0. The check runs after the closed-stream check and before any state changes, so the stream position (nextReadPos) is untouched. It is an argument-validation failure, not an I/O problem.
Source
Thrown at hadoop-tools/hadoop-azure/src/main/java/org/apache/hadoop/fs/azurebfs/services/AbfsInputStream.java:776
if (statistics != null) {
statistics.incrementReadOps(1);
}
}
/**
* Seek to given position in stream.
* @param n position to seek to
* @throws IOException if there is an error
* @throws EOFException if attempting to seek past end of file
*/
@Override
public synchronized void seek(long n) throws IOException {
LOG.debug("requested seek to position {}", n);
if (closed) {
throw new IOException(FSExceptionMessages.STREAM_IS_CLOSED);
}
if (n < 0) {
throw new EOFException(FSExceptionMessages.NEGATIVE_SEEK);
}
if (n > contentLength) {
throw new EOFException(FSExceptionMessages.CANNOT_SEEK_PAST_EOF);
}
if (streamStatistics != null) {
streamStatistics.seek(n, fCursor);
}
// next read will read from here
nextReadPos = n;
LOG.debug("set nextReadPos to {}", nextReadPos);
}
@Override
public synchronized long skip(long n) throws IOException {
if (closed) {
throw new IOException(FSExceptionMessages.STREAM_IS_CLOSED);View on GitHub (pinned to 2add963021)
Solutions
- Clamp the target before calling seek: long target = Math.max(0, desiredPos)
- Audit every seek argument and assert desiredPos >= 0 in debug builds
- Do offset arithmetic in long, never int, to avoid overflow producing negatives
Example fix
// before long backoff = headerLen; // headerLen > current pos at file start in.seek(in.getPos() - backoff); // EOFException: negative seek // after long target = Math.max(0, in.getPos() - backoff); in.seek(target);
Defensive patterns
Strategy: validation
Validate before calling
// Clamp and type-check offsets before seeking
long safeSeek(FSDataInputStream in, long pos) throws IOException {
if (pos < 0) pos = 0; // never seek negative
in.seek(pos);
return pos;
} Try / catch
try {
in.seek(desired);
} catch (EOFException e) {
if (desired < 0) {
in.seek(0); // fall back to file start
} else {
throw e; // genuine EOF problem
}
} Prevention
- Do all byte-offset arithmetic in long, never int
- Compute relative seeks as Math.max(0, getPos() - backoff)
- Reject negative offsets at your API boundary before they reach the stream
When it happens
Trigger: seek(getPos() - delta) where delta is larger than the current position (rewind past start); passing -1 as a 'reset to beginning' sentinel; computing offsets in int arithmetic that overflows to a negative value then widening to long; upstream parsers handing through a negative byte offset.
Common situations: Backward-seeking parsers (e.g., re-reading a record header) that underflow at position 0; porting code that used mark/reset semantics and emulates them with seek(pos - n); int-to-long conversion bugs in offset math on files larger than 2GB.
Related errors
- Attempted to seek or read past the end of the file
- Cannot seek to a negative offset " + targetPos
- Cannot seek to a negative offset
- Invalid seek offset: position value (%d) must be >= 0 for '%
- Cannot seek to a negative offset %s
AI-assisted analysis of apache/hadoop@2add963021 (2026-08-22).
Data as JSON: /api/errors/ad8ccac340928665.
Report an issue: GitHub.