VictoriaMetrics/VictoriaMetrics · error

cannot seek to offset=%d for %q: %w

Error message

cannot seek to offset=%d for %q: %w

What it means

OpenReaderAt opens a file and seeks to the requested absolute offset before returning the reader. If the underlying f.Seek(offset, io.SeekStart) syscall fails, the file is closed and this wrapped error is returned. It means the OS rejected the seek itself (bad fd, I/O error, or invalid arguments), not that the seek landed elsewhere.

Source

Thrown at lib/filestream/filestream.go:84

	f  *os.File
	br *bufio.Reader
	st streamTracker
}

// Path returns the path to r
func (r *Reader) Path() string {
	return r.f.Name()
}

// OpenReaderAt opens the file at the given path in nocache mode at the given offset.
//
// If nocache is set, then the reader doesn't pollute OS page cache.
func OpenReaderAt(path string, offset int64, nocache bool) (*Reader, error) {
	r := MustOpen(path, nocache)
	n, err := r.f.Seek(offset, io.SeekStart)
	if err != nil {
		r.MustClose()
		return nil, fmt.Errorf("cannot seek to offset=%d for %q: %w", offset, path, err)
	}
	if n != offset {
		r.MustClose()
		return nil, fmt.Errorf("invalid seek offset for %q; got %d; want %d", path, n, offset)
	}
	return r, nil
}

// MustOpen opens the file from the given path in nocache mode.
//
// If nocache is set, then the reader doesn't pollute OS page cache.
func MustOpen(path string, nocache bool) *Reader {
	f, err := os.Open(path)
	if err != nil {
		logger.Panicf("FATAL: cannot open file: %s", err)
	}
	r := &Reader{
		f:  f,

View on GitHub (pinned to 5079fb58f1)

Solutions

  1. Check the offset passed in: it must be >= 0 and within the file size; fix the stored/computed offset
  2. Verify the path points to a regular, seekable file, not a pipe/socket/device
  3. Run fsck / check disk and mount health if the wrapped errno is EIO
  4. Retry opening once — transient NFS/EIO seek failures may succeed on a fresh open

Example fix

// before
offset := int64(-1) // corrupted persisted offset
r, err := filestream.OpenReaderAt(path, offset, false)
// after
if fi, statErr := os.Stat(path); statErr == nil {
    if offset < 0 || offset > fi.Size() {
        offset = 0
    }
}
r, err := filestream.OpenReaderAt(path, offset, false)
Defensive patterns

Strategy: validation

Validate before calling

fi, err := os.Stat(path)
if err != nil {
    return err
}
if offset < 0 || offset > fi.Size() {
    return fmt.Errorf("offset %d out of range for %s (size %d)", offset, path, fi.Size())
}

Type guard

func isSeekableRegularFile(fi os.FileInfo) bool {
    return fi != nil && fi.Mode().IsRegular()
}

Try / catch

r, err := filestream.OpenReaderAt(path, offset, nocache)
if err != nil && strings.Contains(err.Error(), "cannot seek to offset") {
    // wrapped OS seek error — log the underlying errno and fall back to offset 0
    r, err = filestream.OpenReaderAt(path, 0, nocache)
}

Prevention

When it happens

Trigger: Calling filestream.OpenReaderAt (directly or via NewReadCloser / tryOpeningQueue when opening queue file streams) with a negative offset, or when the opened file cannot be seeked (fd invalid after open failure, I/O error, ESPIPE on non-seekable special files, EIO on failing disks/NFS).

Common situations: Queue/persistent-queue code resuming from a stored offset that was serialized as negative or corrupted; files on network mounts or failing storage returning I/O errors during seek; opening non-regular files (pipes/devices) as streams.

Related errors


AI-assisted analysis of VictoriaMetrics/VictoriaMetrics@5079fb58f1 (2026-09-03). Data as JSON: /api/errors/53c11574840a1763. Report an issue: GitHub.