Skip to content

return the reader's io.ErrUnexpectedEOF from File.ReadFrom - #674

Merged
puellanivis merged 3 commits into
pkg:masterfrom
christhomas:fix-readfrom-source-error
Oct 19, 2026
Merged

puellanivis merged 3 commits into
pkg:masterfrom
christhomas:fix-readfrom-source-error

Conversation

@christhomas

Copy link
Copy Markdown
Contributor

Fixes #673

This builds on #670, which changes the same lines, so its commit is included here. Only the last commit is new. I'll rebase once #670 is merged, or merge the two if you prefer one PR.

ReadFrom used io.ReadFull to fill its buffer and treated io.ErrUnexpectedEOF as the end of the data. That also hid an io.ErrUnexpectedEOF returned by the reader itself, such as a cut-off HTTP body. Both paths now fill the buffer with a small loop that returns the reader's error unchanged, and only io.EOF ends the copy without an error.

The test fails without the change for a short chunk, a full chunk and the concurrent path. It passes with -race, -integration and -testserver.

When the reader ends part-way through the buffer, io.ReadFull returns
io.ErrUnexpectedEOF with the last bytes. ReadFrom kept that error over
the error from writing those bytes and then returned nil for it, so a
refused final write was reported as success.

Only replace it when the write failed, so the loop doesn't read r again
after it has ended.

Fixes pkg#668
ReadFrom filled its buffer with io.ReadFull and treated
io.ErrUnexpectedEOF as the end of the data, because io.ReadFull returns
it when the reader ends part way through the buffer. An
io.ErrUnexpectedEOF from the reader itself was dropped as well. For
example, copying an HTTP response body that was cut off into a File
returned a nil error, and left a truncated file.

Fill the buffer with a loop that returns the reader's error unchanged,
and only treat io.EOF as the end of the data.

Fixes pkg#673
@christhomas
christhomas force-pushed the fix-readfrom-source-error branch from 3d29963 to e5604b2 Compare October 17, 2026 10:20
Comment thread client.go Outdated
Comment on lines +2122 to +2125
// readFull reads from r into b until b is full or r returns an error.
// Unlike io.ReadFull, it returns the error from r unchanged. io.ReadFull
// turns an io.EOF after a partial read into io.ErrUnexpectedEOF, which
// can't be told apart from an io.ErrUnexpectedEOF returned by r itself.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While there are plenty of comments that don’t follow semantic breaking, we should generally strive to make sure that new comments do.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok updated

@christhomas

Copy link
Copy Markdown
Contributor Author

Updated the comment to explain the semantic difference directly: io.ReadFull converts EOF after a partial read to io.ErrUnexpectedEOF, so that result cannot be distinguished from a reader that returned io.ErrUnexpectedEOF itself. The comment now uses line breaks at those related points to make the distinction easier to scan.

@puellanivis
puellanivis merged commit 6cc5209 into pkg:master Oct 19, 2026
4 checks passed
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.

File.ReadFrom returns nil when the reader fails with io.ErrUnexpectedEOF

2 participants