Repository navigation
Avoid hang when using git hash-object --stdin --path - #3902
Merged
Merged
Conversation
When we use git hash-object --stdin with the --path option, Git applies filters to the object, so Git LFS is invoked. However, if the object provided is less than 1024 bytes in size, we would hang. This occurred because of our packet reader didn't quite implement the io.Reader interface completely: if it returned a non-zero value and io.EOF, the next call to Read would not return 0 and io.EOF. Instead, it would try to read from stdin, which would not be sending us more data until we provided a response, so we would hang. To solve this, keep track of the EOF and always return it on subsequent Read calls. In addition, don't process the callback to write the file in this case, since we don't actually want to write into the working tree.
bk2204
marked this pull request as ready for review
November 4, 2019 20:11
bk2204
added a commit
to bk2204/pktline
that referenced
this pull request
Mar 29, 2021
The io.Reader interface specifies that once we reach an EOF indicator, the next read from the stream should return 0, io.EOF. However, our reader didn't work that way, causing us to attempt to read the next packet from the underlying reader. In some cases, such a packet won't exist until we've responded to the one we already got, causing us to hang. This was fixed in git-lfs/git-lfs#3902, but never ported over to here. Let's port that change over here, and add a Reset method to allow users to continue with the next packet if there's more data to read from the underlying stream.
bk2204
added a commit
to bk2204/pktline
that referenced
this pull request
Mar 30, 2021
The io.Reader interface specifies that once we reach an EOF indicator, the next read from the stream should return 0, io.EOF. However, our reader didn't work that way, causing us to attempt to read the next packet from the underlying reader. In some cases, such a packet won't exist until we've responded to the one we already got, causing us to hang. This was fixed in git-lfs/git-lfs#3902, but never ported over to here. Let's port that change over here, and add a Reset method to allow users to continue with the next packet if there's more data to read from the underlying stream.
chrisd8088
added a commit
to cynix/git-lfs
that referenced
this pull request
Aug 5, 2026
In a prior commit in this PR we revised the clean() function in our "commands/command_clean.go" source file so that when it is passed a non-empty "fileName" parameter, the function no longer tries to determine the size of the file at the location given by that parameter. Previously, the function would invoke the Stat() function from the Go standard library's "os" package to attempt to read the size of the given file. If that succeeded, and if the clean() function's "fileSize" parameter was negative, the file size returned by the Stat() function would be used in the subsequent call to the Clean() method of the GitFilter structure in our "lfs" package. Instead, the clean() function now just passes its "fileSize" parameter directly to the Clean() method, without modification. This change ensures that we do not pass an incorrect size to the Clean() method, as might previously occur under the circumstances reported in the secondary issue from git-lfs#4974. Specifically, as described in the following comment, when Git streamed a file's data to our "git lfs filter-process" command, if the corresponding file on disk was altered before the command checked its size, an error would be reported: git-lfs#4974 (comment) Both the "git lfs filter-process" command and the "git lfs clean" command pass negative values for the "fileSize" parameter when they invoke the clean() function, since they do not receive file size information from Git during "clean" filter operations. Hence the Clean() method now always receives a negative value in its "fileSize" parameter during "clean" filter operations. In the normal case, when no Git LFS pointer extension programs are configured, the Clean() method invokes the GitFilter structure's copyToTemp() method to read the object data piped from Git, check if it already contains a valid Git LFS pointer, and otherwise calculate the data's SHA-256 hash while spooling the data to a temporary file. To write the object data to the file, the copyToTemp() method uses the CopyWithCallback() function from our "tools" package. The purpose of our CopyWithCallback() function is simply to copy data from a source to a destination, while periodically invoking a callback function to write progress messages to a log file, if one is defined with the GIT_LFS_PROGRESS environment variable. These progress log entries include a field which represents the amount of an object's data that has been transferred so far as a fraction whose denominator is the expected total size of the object. To populate the denominator of this field in the progress log entries, the CallbackReader structure in our "tools" package is initialized with a "totalSize" parameter, which will then be passed to the provided callback function each time it is invoked. Since the CopyWithCallback() function creates a CallbackReader structure, it therefore requires both its own "totalSize" parameter and a pointer to a callback function. During a "clean" filter operation, these parameters are passed down from the clean() function to the GitFilter structure's Clean() method and then on to the copyToTemp() method, which passes them to the CopyWithCallback() function. Note that the GitFilter structure's methods use the name "fileSize" for the parameter which is ultimately used for the CopyWithCallback() function's "totalSize" parameter. In commit b0d669c of PR git-lfs#3902 we added a check to the copyToTemp() method which sets the callback function pointer to nil if the "fileSize" parameter is negative. As noted above, we revised the clean() function in a prior commit in this PR so that it no longer attempts to read the size of the file identified by its "fileName" parameter. Since both the "git lfs clean" and "git lfs filter-process" commands pass a negative value to the clean() function, that is the value which all "clean" filter operations now pass to the copyToTemp() method. As a result of our changes, the check for a negative "fileSize" parameter value added to the copyToTemp() method in PR git-lfs#3902 will always succeed during "clean" filter operations. This effectively disables all progress log output from these operations even though the clean() function explicitly creates a callback function for this purpose using the CopyCallbackFile() method of the GitFilter structure. The "progress log: file counts" test that we renamed and updated in the recent PR git-lfs#6288 provides a demonstration of this change in the behaviour of the Git LFS client. In commit 4a4e524 of that PR we added to the test a check of the number of entries with the operation name "clean" that appear in a progress log after a "git add" command. Since our change to the clean() function in the prior commit in this PR, that check now finds no matching entries in the progress log, and so the test currently fails. To resolve this problem, we now simply remove the conditional block in the copyToTemp() method which sets the callback function pointer to nil if the "fileSize" parameter is negative. Note, though, that when we introduced that conditional block in PR git-lfs#3902, we did so with the intention that it would act as a fallback protection against the problem reported in git-lfs#3884. Prior to the changes from PR git-lfs#3902, the "git hash-object" command would fail when its --path option's argument matched a Git LFS filter entry from a "gitattributes" file and the command's input was smaller than the size of the read buffer the Git LFS client uses when decoding pointers. In commit b0d669c of PR git-lfs#3902 we resolved this problem by ensuring that the Read() method of what was then the "pktlineReader" structure in our "git" package properly handled subsequent calls after reading a non-zero number of bytes and also encountering the EOF condition. (Note that we later moved this structure into our separate "git-lfs/pktline" repository.) In the same commit we also attempted to protect against the issue from git-lfs#3884 with a separate change to the copyToTemp() method. Specifically, we added the conditional block which sets the callback function pointer to nil if the "fileSize" parameter is negative, with the understanding that this would prevent the method from writing a file to the working tree when the Git LFS client was invoked by a "git hash-object" command. As we explained in the commit description, this change was meant to ensure that we "don't process the callback to write the file in this case, since we don't actually want to write into the working tree". However, we apparently made this change under a misapprehension. The callback function provided to the copyToTemp() method is not responsible for writing to a file in the working tree. In fact, during a "clean" filter operation, no file will be written to the working tree at all, including by Git. (This stands in contrast to a "smudge" filter operation, when Git does write a file to the working tree.) What does occur during a "clean" filter operation, though, is that the copyToTemp() method creates a temporary file in the local Git LFS storage directories and writes to the file using the CopyWithCallback() function from our "tools" package. This occurs regardless of whether a callback function is defined or not. The CopyWithCallback() function uses the Copy() function from the Go standard library's "io" package to perform the actual transfer of data. The clean() function in our "commands" package then moves the temporary file into a final location in the object storage directory hierarchy within the local Git LFS storage directory. All of these steps are performed even if the "clean" filter operation was invoked via a "git hash-object" command and not a "git add" command. As described further above, the only purpose of the callback function that the CopyCallbackFile() method of the GitFilter structure creates is to write messages to a progress log file, if one is specified with the GIT_LFS_PROGRESS environment variable. So the conditional block that we added to the copyToTemp() method in commit b0d669c only causes the progress log to be disabled under some conditions, not to prevent a Git LFS object file from being created. Thus we can remove this conditional block entirely without concern that we might introduce a regression and a re-introduction of the problem reported in git-lfs#3884. However, absent some of our other changes from PR git-lfs#6288, we would cause a regression of another form, by re-introducing the issue that was reported and resolved in PR git-lfs#5504. That PR revised the callback function created by the CopyCallbackFile() method so that it would only write a message to the progress log if at least 200 milliseconds had elapsed since the previous message was written, or if all the data for an object had been read and so the current message would be the final entry for a given object. We made this change specifically to avoid excessive numbers of writes to the progress log during "clean" filter operations. In order to ensure that a final progress log message was written for an object when its data had been fully read, the callback function would check whether its "written" parameter was equal to or larger than its "total" parameter, and if so, unconditionally write a message even if less than 200 milliseconds had elapsed since the previous message was written. This logic depends on the "total" parameter's value never being negative, though, because if it is then the "written" parameter will always be larger and so every message will be written, regardless of the amount of time that has elapsed since the previous message was written. On their own, our changes to the clean() function in our prior commit in this PR would result in the callback function writing every message to the progress log during "clean" filter operations, which would re-introduce the problem that we tried to resolve in PR git-lfs#5504. Fortunately, though, we have recently addressed this issue in PR git-lfs#6288 by refactoring both the callback function created by the CopyCallbackFile() method and the internal implementation of the Read() method of the CallbackReader structure. The callback function now avoids writing messages if insufficient time has elapsed even when the "total" parameter's value is negative. As well, when the callback function receives a negative value in its "total" parameter, it represents the unknown total as a series of question mark characters in the progress log entry, rather than as a literal negative integer. Further, the internal implementation of the CallbackReader structure's Read() method will now change the value of the "total" parameter to equal that of the "written" parameter when it invokes the callback function for the final time for a given object. This ensures that the final entry for an object whose size was initially unknown will not contain question mark characters (or a negative integer) but will instead have equal values in the numerator and denominator of the field which represents the amount of the object's data that has been processed. To verify that these changes in PR git-lfs#6288 work in combination with our changes from this PR to generate an entry in the progress log during a "clean" filter operation, we now add a "progress log: unknown initial size (clean)" test to our "t/t-progress.sh" shell script. We model this test on the "progress log: unknown initial size (smudge)" and "progress log: unknown initial size (fetch/pull)" tests that we added in commit 47d1e25 of PR git-lfs#6288. Our new test performs a "git add" command and confirms that although the "git lfs filter-process" command no longer attempts to determine the size of an object by calling the Stat() function on the corresponding file in the working tree, a final progress log entry will be written where the total size of the object is correctly reported.
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.
When we use git hash-object --stdin with the --path option, Git applies filters to the object, so Git LFS is invoked. However, if the object provided is less than 1024 bytes in size, we would hang. This occurred because of our packet reader didn't quite implement the io.Reader interface completely: if it returned a non-zero value and io.EOF, the next call to Read would not return 0 and io.EOF. Instead, it would try to read from stdin, which would not be sending us more data until we provided a response, so we would hang.
To solve this, keep track of the EOF and always return it on subsequent Read calls. In addition, don't process the callback to write the file in this case, since we don't actually want to write into the working tree.
Fixes #3884.
/cc @rivuonna as reporter