Repository navigation
ReadFrom: keep the bytes returned together with a read error - #33
Merged
smallnest merged 1 commit intoSep 28, 2026
Merged
Conversation
io.Reader may return n > 0 bytes along with a non-EOF error, and those bytes should be processed before the error. ReadFrom set the error and broke out without committing them or counting them in n, so io.Copy into a ring buffer from a source ending in io.ErrUnexpectedEOF under-reported what it took and dropped the last chunk. WriteTo already counts partial bytes on error (smallnest#20).
This branch was successfully deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
io.Readermay returnn > 0together with a non-EOF error, and the caller should use those bytes before looking at the error.ReadFromsets the error and breaks out without committing them or adding them ton(ring_buffer.go:553). Soio.Copy(rb, src)from a source that ends withio.ErrUnexpectedEOF(a truncated HTTP body or flate stream) reports fewer bytes than it read, and the last chunk is lost.WriteToalready counts partial bytes on error since #20.This commits and counts
nrbefore setting the error. Theerrormockreader now returns its bytes together with the error, the way itsWritealready does, which makes three rows ofTestRingBuffer_ReadFrom_Errorfail on master;TestRingBuffer_ReadFrom_ErrorKeepsDataalso checksLength().go vet,gofmt -s -landgo test -race ./...pass. One note on the last:TestRingBuffer_Copyhit its 60s timeout on my machine under heavy load, on master as well (3 of 3 isolated-raceruns took 68-70s); its stream only returnsnilorio.EOF, so it never reaches this branch. With it skipped the-racesuite passes.Written with AI assistance (Claude); I ran the checks above locally and reviewed the change.