Repository navigation
progress: fix never reading bytes with sufficiently small files - #1955
Merged
Merged
Conversation
ttaylorr
force-pushed
the
zero-bytes-forever
branch
from
February 17, 2017 04:18
f38d315 to
835fa61
Compare
ttaylorr
force-pushed
the
zero-bytes-forever
branch
from
February 17, 2017 04:28
835fa61 to
a116374
Compare
technoweenie
left a comment
Contributor
There was a problem hiding this comment.
The changes look good, but the tests still pass. That's because a *bytes.Reader only returns EOF if somehow the index has surpassed the inner []byte size. It may make sense to make a custom reader implementation to trigger the bug.
Contributor
Author
Good call. I was thinking of doing this last night, but I'm glad you made the recommendation. Here's a failing test on and on xref: 591307d. |
technoweenie
approved these changes
Feb 17, 2017
Merged
chrisd8088
added a commit
that referenced
this pull request
Aug 5, 2026
In PR #1955 we resolved a problem in the Read() methods of the interfaces we use to invoke callback functions that write entries to the progress log file defined by the GIT_LFS_PROGRESS environment variable. (These methods are currently located in our "tools" package, but at the time they were defined in a package named "progress".) The BodyWithCallback and CallbackReader structure types in our "tools" package both implement the Reader interface from the "io" package of the Go standard library by passing through calls from their Read() methods to the Read() method of another implementation of the Reader interface. Depending on the return values from that call, our structures' Read() methods may then invoke a callback function to write an entry in the progress log file. In commit a2a7b09 of PR #1955 we fixed a bug in the Read() methods of our BodyWithCallback and CallbackReader structures whereby the methods would fail to invoke their callback functions if the underlying Read() method read a non-zero number of bytes but also encountered an EOF condition. The change from commit a2a7b09 now ensures the callback function is invoked under these circumstances, but it also replaces the EOF error with the error returned by the callback function, which will usually be nil. As a result, the callers of our structures' Read() methods then have to perform a second call to determine that the EOF condition has been reached. In this additional call, our structures' Read() methods will not invoke their callback functions because the underlying Read() method reads zero bytes; our methods will then return the EOF error from the underlying Read() method call. We will address this issue in a subsequent commit in this PR, and make several other revisions to the Read() methods of the BodyWithCallback and CallbackReader structures. To ensure these changes are effective, we will also expand the set of tests we perform, both in our "tools" package and in our "lfs" package. As such, we expect to make use of the EOFReader test helper structure type added in commit 591307d of PR #1955, but we first need to move the definition of the structure into a common location where it can be imported into both the "tools" and "lfs" packages. Our EOFReader structure was introduced to help verify that the changes implemented in commit a2a7b09 were effective. As such, the structure's Read() method always returns an EOF error as soon as possible, even if a non-zero number of bytes were also read. In the tests we expect to add in subsequent commits in this PR, we will also need a test helper structure whose Read() method consistently returns an EOF error as late as possible, meaning that it only returns the EOF error when no bytes have also been read. To distinguish these two test helpers, and to help clarify the purpose of the existing EOFReader structure, we now rename it to EagerEOFByteReader. We also introduce a NewEagerEOFByteReader() function which we can use to initialize a new structure, so all of our test functions will not have to explicitly populate the structure's fields. As well, because we will need to use this structure (plus a complementary DeferredEOFByteReader structure) in the test functions from multiple packages, we create a new, common "internal/testutil" package and move the definition of the EagerEOFByteReader and its sole test function into this package. Note that in future, we may begin to move some of our other, existing packages into the "internal" directory, as we do not intend for their APIs or ABIs to be used in third-party applications. In PR #4582 we added a note to our "go.mod" file to caution developers not to rely on our packages having stable APIs or ABIs, but a more comprehensive approach would be to move the majority of our packages into the "internal" directory. However, we defer this work for the present time as it far exceeds the scope of this PR.
chrisd8088
added a commit
that referenced
this pull request
Aug 5, 2026
In PR #1955 we resolved a problem in the Read() methods of the interfaces we use to invoke callback functions that write entries to the progress log file defined by the GIT_LFS_PROGRESS environment variable. (These methods are currently located in our "tools" package, but at the time they were defined in a package named "progress".) The BodyWithCallback and CallbackReader structure types in our "tools" package both implement the Reader interface from the "io" package of the Go standard library by passing through calls from their Read() methods to the Read() method of another implementation of the Reader interface. Depending on the return values from that call, our structures' Read() methods may then invoke a callback function to write an entry in the progress log file. In commit a2a7b09 of PR #1955 we fixed a bug in the Read() methods of our BodyWithCallback and CallbackReader structures whereby the methods would fail to invoke their callback functions if the underlying Read() method read a non-zero number of bytes but also encountered an EOF condition. In the same PR #1955 we then added a new Go test function named TestCopyCallbackReaderCallsCallbackUnderfilledBuffer() which verifies that the Read() method of the CallbackReader structure invokes the callback function after the underlying Read() method returns the EOF error after reading a non-zero number of bytes. However, we did not include a corresponding test of the Read() method of the BodyWithCallback structure, so we now revise the existing test to verify the behaviour of both the CallbackReader structure and the BodyWithCallback structure. (Note that in a future PR, we may eliminate one of these two similar structures, at which point this test will only need to check the behaviour of that common structure's methods.) Since the BodyWithCallback structure implements the ReadSeekCloser interface from the "io" package, we need to add simple Seek() and Close() methods for the EagerEOFByteReader structure in our "internal/testutil" package. Fortunately, the Seek() method only needs to exist for the purposes of our test, which allows us to just define a stub version of the method. Otherwise, we would likely need to mirror the complete implementation from the Seek() method of the Reader structure in the Go standard library's "bytes" package: https://github.com/golang/go/blob/go1.26.4/src/bytes/reader.go#L115-L134 To alter the TestCopyCallbackReaderCallsCallbackUnderfilledBuffer() function to check the behaviour of both the CallbackReader and BodyWithCallback structures, we first create one of each using the same initial settings. We then iterate over the pair of these structures and perform identical checks on each one, including an additional check that after EOF has been reached and a final callback has been made, no further callback occurs on a subsequent invocation of the Read() method when no bytes are available to be read. We also simplify the test in several respects. First, because our structures' Read() methods invoke their callback functions synchronously within a single goroutine, there is no need for us to use the "sync/atomic" package from the Go standard library to increment the count of the number of times the callbacks occur. Second, we revise the test to avoid the use of hard-coded constant values in favour of local variables whose values are dependent on the size of the byte slice read by the structures' Read() methods. Third, where possible we check "int" values using just the Equal() function from the "github.com/stretchr/testify/assert" package, as we can avoid the use of the EqualValues() function except when the integer types of the expected and actual values differ. And fourth, to clarify the purpose of the test and its expanded scope, we rename it to TestBothCallbackReadersInvokeCallbackOnEagerEOF(). In commit 589dffb of PR #1958 we added two other functions to test the Read() method of the BodyWithCallback structure. One of these test functions, the TestBodyCallbackReaderUpdatesOffsetOnSeek() function, only pertains to the specific behaviour of the BodyWithCallback structure's Read() method. However, the TestBodyCallbackReaderCountsReads() test function checks that the BodyWithCallback structure's Read() method correctly updates its internal count of the number of bytes read, which is an operation also performed by the CallbackReader structure's Read() method. We therefore revise this function in a similar manner to we did for the TestCopyCallbackReaderCallsCallbackUnderfilledBuffer() function so that it now checks that both structures' Read() methods correctly update the internal counts of the number of bytes read. We likewise rename the function to TestBothCallbackReadersCountReads() and replace the use of hard-coded constants with local variables wherever possible. We also alter the test so that it performs multiple calls to each structure's Read() method and checks the internal count after each call, rather than just performing a single call to the Read() method which consumes all the available data. Finally, we take the opportunity to also amend the existing TestBodyCallbackReaderUpdatesOffsetOnSeek() function so that it uses the same buffer initialization steps as the other two test functions, avoids hard-coded constants in favour of local variables, and checks that the results from the Read() method match our expectations after the read position has been modified by one or more Seek() method calls.
chrisd8088
added a commit
that referenced
this pull request
Aug 5, 2026
Our "tools" package defines two structures which perform similar purposes, the BodyWithCallback and CallbackReader structures, whose Read() methods are identical and invoke a callback function after first calling the Read() method of an underlying resource. The only significant difference between the two structures is that the BodyWithCallback structure's methods implement the ReadSeekCloser interface from the Go standard library's "io" package, while the CallbackReader structure's methods only implement the Reader interface from the "io" package. We introduced the first version of our CallbackReader structure in commit d7a787d of PR #97, along with a Read() method quite similar to the one currently defined for the structure. Later, in commit e04316f of PR #1839, we added the initial version of what is now the BodyWithCallback structure, at which time we simply copied the Read() method of the CallbackReader structure to create an identical method for the BodyWithCallback structure. The implementation of the two Read() methods is identical, so when we fix a bug in one, we have to make the same change in the other also. For instance, in commit a2a7b09 of PR #1955 we revised both methods to address a problem whereby they would fail to invoke their callback functions if the underlying Read() method read a non-zero number of bytes but also encountered an EOF condition. In subsequent commits in this PR we intend to alter the design of the Read() methods of the BodyWithCallback and CallbackReader structures, and then to make further changes to handle instances where the total size of a Git LFS object is initially unknown. As we would prefer to avoid making these revisions in two places, and to avoid future problems where we might accidentally update only one of the two Read() methods, we begin by refactoring both the methods and the BodyWithCallback and CallbackReader structures so they share a common internal implementation. Specifically, we introduce a "callbackState" structure whose fields represent the current state for a given callback function, and then we embed this structure in the BodyWithCallback and CallbackReader structures. These structures now only have one unique field each to store their underlying data source. We also define a read() method for the "callbackState" structure, with an identical design to the existing Read() methods of the BodyWithCallback and CallbackReader structures except that it accepts a parameter of the Reader type from the "io" package. We then revise the Read() methods of the BodyWithCallback and CallbackReader structures to simply invoke this new read() method and pass their respective data sources as its Reader type parameter. Note that in a future PR we may further refactor the implementation of the BodyWithCallback and CallbackReader structures to replace them entirely with a single common structure. However, as this change would affect a number of our packages, it is outside the scope of this PR, so for the time being we just revise the existing structures to share a new, common internal structure and its read() method.
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.
This pull-requests fixes #1926, a bug which would cause push/pull operations with small objects to look like this:
The issue lies in the fact that
io.Readers, when at the end of a stream, can either return([]byte, nil)then(nil, io.EOF), or([]byte, io.EOF)From the Go documentation[1]:
Particularly, the problem lied here:
errcould beio.EOFeven when there was a positive number of bytesnto progress upon. This happens when the buffer "p" being read into is larger than the available data on the stream. In other words, small files make this incredibly common.However, changing
err == niltoerr == nil || err == io.EOFisn't quite the right fix, since(nil, io.EOF)is a valid return value from io.Readers. Instead, let's check for positive values ofn, and increment based on that. This is semantically equivalent toerr == nil || (err == io.EOF && n > 0), since the interface documentation specifies that non-nil errors should indicate zero bytes read.I added an integration test to verify that we counted the bytes correctly for small files, and a unit test to prevent further regressions.
/cc @git-lfs/core