Skip to content

Preserve the current position after seeking from EOF - #194

Open
omrzk wants to merge 1 commit into
ncw:masterfrom
omrzk:fix/seek-end-current-position
Open

omrzk wants to merge 1 commit into
ncw:masterfrom
omrzk:fix/seek-end-current-position

Conversation

@omrzk

@omrzk omrzk commented Nov 20, 2026

Copy link
Copy Markdown

After Seek(0, io.SeekEnd) returns the object length, Seek(0, io.SeekCurrent) still returns the old read position. The position query also clears the virtual EOF flag, allowing reads from the old response.

Store the new end position, preserve virtual EOF through position queries and no-op seeks, and clear it after a successful range reopen. Update the outdated Seek documentation. Fixes #193.

The regression exercises fresh and partially read objects, exact and beyond EOF, repeated queries/no-op seeks, relative backwards reads and rewind. It fails all four cases on unchanged master. The full root package suite passes on Go 1.24.13 and 1.25.14; go build ./..., configured golangci-lint 1.64.8 and formatting checks pass. Independent HTTP caller and race controls also pass. These use local HTTP fixtures; Docker/live Swift integration was not run because the Docker socket is unavailable to this user.

Seeking back from the corrected end position now reopens the response. As with other actual seeks, that uses the existing behavior that disables whole-object hash validation after reopening.

@ncw

ncw commented Nov 24, 2026

Copy link
Copy Markdown
Owner

Thanks you for the PR :-)

I'd like to fix it differently, though. The common way to get an object's size is:

size, _ := f.Seek(ctx, 0, io.SeekEnd)
_, _ = f.Seek(ctx, 0, io.SeekStart)

On master this happens to be free, because the second seek does nothing. With this PR it closes and reopens the connection, which costs an extra request and turns off the MD5 check :-(

In rclone we deal with this by letting the caller seek wherever they like and only reopening the stream when a read actually happens. Could you rework the PR that way? Roughly:

  • Store the ctx passed to ObjectOpen in ObjectOpenFile and use it for any reopen. Read has no ctx, and the open body already depends on that one anyway.

  • Track two positions: pos, the logical position set by Seek and advanced by Read, and bodyPos, the position of the currently open body.

  • Seek only works out the new position. It returns an error for a negative position or for SeekEnd when the length is unknown. Otherwise it sets pos and returns it, with no network activity. SeekStart and SeekCurrent can go past the end too, as with os.File.

  • Read: if pos != bodyPos, then:

    • if the length is known and pos >= length, return io.EOF;
    • otherwise reopen with Range: bytes=pos-, set seeked = true and checkHash = false, then read as usual.

    After the read, bodyPos equals pos.

  • If the length is unknown and the reopen gets a 416 Range Not Satisfiable, return io.EOF.

  • Remove overSeeked. It isn't needed any more.

  • Close behaves as it does now: it closes the current body and checks the hash only if the object was read to EOF without ever being reopened.

This does mean errors from reopening, such as a deleted object, show up on the next Read rather than on Seek. That's fine.

Please keep your existing test and add these cases:

  • SeekEnd followed by SeekStart back to the original position reads the data correctly and doesn't reopen (seeked stays false).
  • Several seeks in a row followed by one read cause only one reopen.
  • SeekStart or SeekCurrent past the end, then Read, returns io.EOF.
  • Please update the Seek doc comment to describe the new behaviour.

Thanks again!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ObjectOpenFile.Seek reports the old position immediately after a successful SeekEnd

2 participants