Skip to content

Limit CopyCallbackFile to print every 200 ms - #5504

Merged
chrisd8088 merged 3 commits into
git-lfs:mainfrom
cmaves:main
Sep 27, 2023
Merged

chrisd8088 merged 3 commits into
git-lfs:mainfrom
cmaves:main

Conversation

@cmaves

@cmaves cmaves commented Sep 12, 2023

Copy link
Copy Markdown
Contributor

Prior to this commit, the callback would print every single iteration of io.Copy. This results in a print every 32kb and
can slow clean operations by several orders of magnitude when GIT_LFS_PROGRESS is set.
By limiting the print to every quarter second in this PR, most of negative impact is mitigated.

@cmaves
cmaves requested a review from a team as a code owner September 12, 2023 22:02

@chrisd8088 chrisd8088 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks for the patch, and welcome to the Git LFS project!

This looks reasonable to me, on the whole! I had a couple of suggestions to make, but nothing too dramatic, I hope.

That said, adding some tests may be a bit involved, so let us know if we can help with that.

Thanks again very much for the contribution to the project!

Comment thread lfs/util.go Outdated

var prevWritten int64

deadline := time.Now().Add(time.Millisecond * 250)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

It seems to me this is similar in purpose to the DefaultLoggingThrottle value that's defined in the tasklog package, which limits logging to once per every 200 milliseconds.

Rather than using a slightly different value here, I would suggest at least using that tasklog.DefaultLoggingThrottle constant both here and below.

(If we wanted to get fancy we could make this a configurable value, which is then used in both places, but for now I think it's OK to just use the same constant, as I believe we never set the tasklog.Logger with any other value than the default.)

One other thing we might consider is adding a test or two, akin to the throttling tests in the tasklog package. That's a bit of a heavier lift, though. I'm not sure we have any Go tests of the lfs.CopyCallbackFile() function at all, so let us know if we can help introduce some.

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.

I've updated the value to use the DefaultLoggingThrottle as the sleep duration. I've added a test for the throttle portion of this. It is not the cleanest test with a fair bit of setup and a sleep in it so if there is a better way to test it I'm all ears.

Comment thread lfs/util.go Resolved
Prior to this commit, the callback would print every single iteration of io.Copy.
This results in a print every 32kb and
can slow clean operations by several orders of magnitude
when GIT_LFS_PROGRESS is set.
By limiting the print to every quarter second in this PR,
most of negative impact is mitigated.

@chrisd8088 chrisd8088 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This looks great, thanks! I'll kick off CI, and see if we can get this merged. Thanks again!

@chrisd8088 chrisd8088 changed the title Limit CopyCallbackFile to print every 250 ms Limit CopyCallbackFile to print every 200 ms Sep 27, 2023
@chrisd8088
chrisd8088 merged commit cb27449 into git-lfs:main Sep 27, 2023
chrisd8088 added a commit to chrisd8088/git-lfs that referenced this pull request Sep 28, 2023
In commit 00f1e95 of PR git-lfs#5504 we added
the TestCopyCallbackFileThrottle() test function to confirm that the
CopyCallbackFile() method of GitFilter structure in the "lfs" package
now throttled its output of progress messages into a log file so that,
in general, no more than one message was written during the interval
defined by the tasklog.DefaultLoggingThrottle value.  This new throttling
behaviour was added in commit 5bfa900
in the same PR.

The TestCopyCallbackFileThrottle() function attempts to validate
the throttling action by reading a test buffer in segments and waiting
or not waiting between each read, expecting the log messages to be
written or not written, as appropriate.  However, because our CI test
suite may experience slow execution times as a result of running in
virtualized environments, reads may occur at times more than the
tasklog.DefaultLoggingThrottle interval apart even without the artifical
waits injected by the test function, causing the test to fail when it
sees more log messages than it expects.

We can avoid this problem by making the wait intervals in the test
deterministic using a mocked value for the current time.  We use
the github.com/jmhodges/clock package for this purpose, adding an
object of its Clock interface type to the GitFilter structure.  In
all non-test code this object is simply a wrapper for the "time"
package's functions, but in our test we can use a variant which allows
us to control the time values returned to the CopyCallbackFile() method,
either stepping forward or not, as we desire.
chrisd8088 added a commit to chrisd8088/git-lfs that referenced this pull request Sep 28, 2023
In commit 00f1e95 of PR git-lfs#5504 we added
the TestCopyCallbackFileThrottle() test function to confirm that the
CopyCallbackFile() method of GitFilter structure in the "lfs" package
now throttled its output of progress messages into a log file so that,
in general, no more than one message was written during the interval
defined by the tasklog.DefaultLoggingThrottle value.  This new throttling
behaviour was added in commit 5bfa900
in the same PR.

The TestCopyCallbackFileThrottle() function attempts to validate
the throttling action by reading a test buffer in segments and waiting
or not waiting between each read, expecting the log messages to be
written or not written, as appropriate.  However, because our CI test
suite may experience slow execution times as a result of running in
virtualized environments, reads may occur at times more than the
tasklog.DefaultLoggingThrottle interval apart even without the artifical
waits injected by the test function, causing the test to fail when it
sees more log messages than it expects.

We can avoid this problem by making the wait intervals in the test
deterministic using a mocked value for the current time.  We use
the github.com/jmhodges/clock package for this purpose, adding an
object of its Clock interface type to the GitFilter structure.  In
all non-test code this object is simply a wrapper for the "time"
package's functions, but in our test we can use a variant which allows
us to control the time values returned to the CopyCallbackFile() method,
either stepping forward or not, as we desire.
chrisd8088 added a commit that referenced this pull request Aug 5, 2026
In PR #5504 we modified the CopyCallbackFile() method of the
GitFilter structure in our "lfs" package so that the callback
function it creates only outputs a progress log message when
at least 200 milliseconds have elapsed since the last message
was written, unless the number of bytes read has now met or
exceeded the total size of the given Git LFS object.  (The
minimum time allowed between messages is specified by the
DefaultLoggingThrottle constant from our "tasklog" package.)

In commit 00f1e95 of PR #5504 we then
also introduced the TestCopyCallbackFileThrottle() test function to
verify that the callback function created by the CopyCallbackFile()
method obeyed the new limits on how often it should write to the
progress log.

At present, the callback function checks whether the number of
bytes read has met or exceeded the total size of the given object,
and if it has, unconditionally writes a message to the progress
log, on the assumption that no further callbacks will occur because
the final bytes of the object have now been read.

However, this assumes the total size of the object is known and
correct when the callback is utilized by the CopyWithCallback()
function of our "tools" package.

While this remains true at present for most of the situations in
which we pass a callback function to the CopyWithCallback() function,
these will be more limited in number after we make the change to our
"clean" filter commands proposed in PR #6011.

PR #6011 resolves a bug in our "git lfs filter-progress" and
"git lfs clean" commands whereby they can pass an incorrect
"totalSize" parameter to the CopyWithCallback() function.  The only
solution for this problem is for the commands to simply not attempt
to determine the total size of an object.

Absent other changes, the result of this bug fix would be that
even if a progress log file is specified by the GIT_LFS_PROGRESS
environment variable, no messages would be written to the log file
during "clean" filter operations.  This would occur because the
copyToTemp() method of the GitFilter structure in our "lfs" package
would find a negative value in its "fileSize" parameter and then
set its "cb" callback function parameter to nil.

We intend to address this issue in PR #6011, but before we do so,
in subsequent commits in this PR we will revise the callback
function created by the CopyCallbackFile() method to handle the
case where it is passed a negative value in its "total" parameter,
which indicates that the total size of the object is unknown.

Before we make those revisions to the CopyCallbackFile() method,
though, we first expand our test suite to exercise the callback
function created by the method under a wider range of conditions.

Specifically, we now refactor the TestCopyCallbackFileThrottle()
function so we will be able to easily expand it to simulate a
number of different test cases.  We define these test cases using
a new copyCallbackFileThrottleTestCase structure and an associated
copyCallbackFileThrottleTestMode enumerated value.

At present, the only test case we specify is one where the initial
total size of an object is both known and correct, but in future
commits in this PR we will add other test cases, including one
where the total size of an object is unknown and is therefore
represented by a negative value.

We alter the TestCopyCallbackFileThrottle() function itself to
iterate over one or more test cases and check each one using both
the EagerEOFByteReader and the DeferredEOFByteReader structures from
our "tools" package.  We use these structures' Read() methods to
simulate conditions where the method returns either an EOF error or
a nil error, respectively, at the same time as the final byte of an
input source is read.

For each test case and underlying implementation of the Reader
interface from the Go standard library's "io" package, the
TestCopyCallbackFileThrottle() function now calls the setup()
method of the copyCallbackFileThrottleTestCase structure, which
initializes the progress log file, mock time clock, callback
function, and input data source for a CallbackReader structure
from our "tools" package.

The test function then runs the Assert() method of the
copyCallbackFileThrottleTestCase structure to perform a series of
calls to the Read() method of the CallbackReader structure after
simulated periods of time have elapsed, and checks that the final
contents of the progress log file match the expected results for
the given test case.

In later commits in this PR we will expand the set of test cases run
by the TestCopyCallbackFileThrottle() function as we revise the
callback function created by the CopyCallbackFile() method and the
Read() methods of the BodyWithCallback and CallbackReader structures
in our "tools" package.
chrisd8088 added a commit that referenced this pull request Aug 5, 2026
In subsequent commits in this PR we expect to make several revisions
to the anonymous callback function created by the CopyCallbackFile()
method of the GitFilter structure in our "lfs" package.  In
particular, we intend to alter the function to more appropriately
handle the case where its "total" parameter is negative, which
indicates that the total size of the current object is unknown.

At present, when the callback function is passed a negative value in
its "total" parameter, it simply writes that value to the progress
log as the denominator of the fraction representing the amount of
the object's data processed so far.

As well, if the callback function is passed a negative value in
its "total" parameter, it will always write to the progress log
regardless of how much time has elapsed since the last message was
written.

In PR #5504 we modified the CopyCallbackFile() method of the
GitFilter structure in our "lfs" package so that the callback
function it creates only outputs a progress log message when
at least 200 milliseconds have elapsed since the last message
was written, unless the number of bytes read has now met or
exceeded the total size of the given Git LFS object.  (The
minimum time allowed between messages is specified by the
DefaultLoggingThrottle constant from our "tasklog" package.)

The callback function checks whether the number of bytes read has
met or exceeded the total size of the given object, and if it has,
unconditionally writes a message to the progress log, on the
assumption that no further callbacks will occur because the final
bytes of the object have now been read.

However, this assumes the total size of the object is known and
correct when the callback is utilized by the CopyWithCallback()
function of our "tools" package.  When the "total" parameter is
negative, though, the number of bytes read so far is always larger
than the total size, so a message is always written.

While it is currently rare for the value in the callback function's
"total" parameter to be negative, we expect it to become more common
in the future as a result of the bug fix to our "clean" filter
commands implemented by PR #6011.

Before we address this issue, we first refactor the callback function
created by the CopyCallbackFile() method to make the logic in the
function easier to modify and understand.  In particular, we separate
the multiple parts of the function's single conditional expression
into distinct steps.  We also revise the function so that it returns
immediately when the condition checked by one of these individual
steps is indicates that no log message should be written.
chrisd8088 added a commit that referenced this pull request Aug 5, 2026
In PR #5504 we modified the CopyCallbackFile() method of the
GitFilter structure in our "lfs" package so that the callback
function it creates only outputs a progress log message when
at least 200 milliseconds have elapsed since the last message
was written, unless the number of bytes read has now met or
exceeded the total size of the given Git LFS object.  (The
minimum time allowed between messages is specified by the
DefaultLoggingThrottle constant from our "tasklog" package.)

The callback function checks whether the number of bytes read has
met or exceeded the total size of the given object, and if it has,
unconditionally writes a message to the progress log, on the
assumption that no further callbacks will occur because the final
bytes of the object have now been read.

However, this assumes the total size of the object is known and
correct when the callback is utilized by the CopyWithCallback()
function of our "tools" package.  When the "total" parameter is
negative, though, the number of bytes read so far is always larger
than the total size, so a message is always written.

While it is currently rare for the value in the callback function's
"total" parameter to be negative, we expect it to become more common
in the future as a result of the bug fix to our "clean" filter
commands implemented by PR #6011.

PR #6011 resolves a bug in our "git lfs filter-progress" and
"git lfs clean" commands whereby they can pass an incorrect
"totalSize" parameter to the CopyWithCallback() function.  The only
solution for this problem is for the commands to simply not attempt
to determine the total size of an object.

Absent other changes, the result of this bug fix would be that
even if a progress log file is specified by the GIT_LFS_PROGRESS
environment variable, no messages would be written to the log file
during "clean" filter operations.  This would occur because the
copyToTemp() method of the GitFilter structure in our "lfs" package
would find a negative value in its "fileSize" parameter and then
set its "cb" callback function parameter to nil.

We intend to address this issue in PR #6011, but the consequence
when we do so will be that the callback function created by the
CopyCallbackFile() method will receive a negative value in its
"total" parameter during "clean" filter operations if a progress log
is configured with the GIT_LFS_PROGRESS environment variable.

At present, the callback function may already receive a negative
value in its "total" parameter, but only under a relatively unusual
combination of circumstances.  First, of course, a progress log must
be enabled with the GIT_LFS_PROGRESS environment variable.  Second,
the legacy "git lfs smudge" command must be used during a "smudge"
filter operation rather than the "git lfs filter-process" command;
this may occur if a version of Git older than v2.11.0 is in use.
Third, the "git lfs smudge" command must download an object that is
not available locally from a server which compresses the content
for transit and therefore sends a Content-Encoding header but no
Content-Length header.

Under these conditions, the lack of a Content-Length header means
the size of the HTTP response body will be represented with a value
of -1.  When our HTTP download adapter passes this negative value to
the CopyWithCallback() function, it will be used to initialize a new
CallbackReader structure from our "tools" package, and ultimately
passed to the callback function created by the CopyCallbackFile()
method.  Note that the callback function itself is passed from the
Smudge() method of the GitFilter structure to the same structure's
downloadFile() method, which creates a new transfer queue using
the WithProgressCallback() option function.

By contrast, when the "git lfs filter-process" command is in use,
it creates a transfer queue without a defined callback function.
As a result, objects that not are available locally are downloaded
and written to local storage without generating any progress log
messages.  When the Smudge() method is later invoked (because Git
re-sends a "smudge" command to our long-running filter process),
it runs the GitFilter structure's readLocalFile() method instead of
the downloadFile() method.  While the readLocalFile() method does
invoke the CopyWithCallback() function and passes a callback
function to it, at this point the size of the object can be
determined from the file in local storage, so the callback function
receives a non-negative value in its "total" parameter.

However, as described above, when the "git lfs smudge" command is
in use and a content-encoded download occurs, the callback function
created by the CopyCallbackFile() method will receive a negative
value in its "total" parameter, which means the function will write
to the progress log regardless of the amount of time that has
elapsed since the previous message was written.

Further, we expect the changes in PR #6011 to result in a wider
range of conditions under which the callback function will receive
a negative value in its "total" parameter.

We therefore now revise the callback function created by the
CopyCallbackFile() method so that it checks whether the value of
the "total" parameter is negative, and if so will not write a
progress log message unless a sufficient period of time has
elapsed since the previous log message was written.

To verify that our change is effective, we define a second test
case for the TestCopyCallbackFileThrottle() test function that
we refactored in a prior commit in this PR.  Our new test case
is named "initial total unknown" and simulates multiple invocations
of the callback function where the "total" parameter has a negative
value each time.  In this test case, we expect that the modifications
from this commit should ensure that the callback function only writes
one log message, after the test advances the mock time clock by the
full duration specified in the DefaultLoggingThrottle constant from
our "tasklog" package.

Note that the log message expected by our new "initial total unknown"
test case contains a "-1" in the denominator of the fraction
representing the amount of source data read so far.  We will refine
this field in a subsequent commit in this PR so that negative
"total" values are no longer reported in a literal manner.
chrisd8088 added a commit that referenced this pull request Aug 5, 2026
In PR #5504 we modified the CopyCallbackFile() method of the
GitFilter structure in our "lfs" package so that the callback
function it created only output a progress log message when
at least 200 milliseconds had elapsed since the last message
was written, unless the number of bytes read had met or exceeded
the total size of the given Git LFS object.  (The minimum time
allowed between messages was specified by the DefaultLoggingThrottle
constant from our "tasklog" package.)

This design depended on the assumption that the total size of the
given object was known and correct when the callback was utilized
by the CopyWithCallback() function of our "tools" package.  When
the "total" parameter was negative, though, a message would always
be written because the number of bytes read so far would always be
larger than the total size.

In a previous commit in this PR we adjusted the callback function
so that it now checks whether the value of the "total" parameter
is negative, and if so will not write a progress log message unless
a sufficient period of time has elapsed since the previous log
message was written.

However, one consequence of this adjustment is that when the "total"
parameter is negative, it is not guaranteed that a log message will
be written when the last callback is made after the final bytes of
the given object's data are read.  Further, if an object's data is
fully processed in less than 200 milliseconds, no progress log
message will be written for the object at all.

While it is currently rare for the value in the callback function's
"total" parameter to be negative, we expect it to become more common
in the future as a result of the bug fix to our "clean" filter
commands implemented by PR #6011.

PR #6011 resolves a bug in our "git lfs filter-progress" and
"git lfs clean" commands whereby they can pass an incorrect
"totalSize" parameter to the CopyWithCallback() function.  The only
solution for this problem is for the commands to simply not attempt
to determine the total size of an object.

Absent other changes, the result of this bug fix would be that
even if a progress log file is specified by the GIT_LFS_PROGRESS
environment variable, no messages would be written to the log file
during "clean" filter operations.  This would occur because the
copyToTemp() method of the GitFilter structure in our "lfs" package
would find a negative value in its "fileSize" parameter and then
set its "cb" callback function parameter to nil.

We intend to address this issue in PR #6011, but the consequence
when we do so will be that the callback function created by the
CopyCallbackFile() method will receive a negative value in its
"total" parameter during "clean" filter operations if a progress log
is configured with the GIT_LFS_PROGRESS environment variable.

At present, the callback function may already receive a negative
value in its "total" parameter, but only under a relatively unusual
combination of circumstances.  First, of course, a progress log must
be enabled with the GIT_LFS_PROGRESS environment variable.  Second,
the legacy "git lfs smudge" command must be used during a "smudge"
filter operation rather than the "git lfs filter-process" command;
this may occur if a version of Git older than v2.11.0 is in use.
Third, the "git lfs smudge" command must download an object that is
not available locally from a server which compresses the content
for transit and therefore sends a Content-Encoding header but no
Content-Length header.

Under these conditions, the lack of a Content-Length header means
the size of the HTTP response body will be represented with a value
of -1.  When our HTTP download adapter passes this negative value to
the CopyWithCallback() function, it will be used to initialize a new
CallbackReader structure from our "tools" package, and ultimately
passed to the callback function created by the CopyCallbackFile()
method.  Note that the callback function itself is passed from the
Smudge() method of the GitFilter structure to the same structure's
downloadFile() method, which creates a new transfer queue using
the WithProgressCallback() option function.

By contrast, when the "git lfs filter-process" command is in use,
it creates a transfer queue without a defined callback function.
As a result, objects that are not available locally are downloaded
and written to local storage without generating any progress log
messages.  When the Smudge() method is later invoked (because Git
re-sends a "smudge" command to our long-running filter process),
it runs the GitFilter structure's readLocalFile() method instead of
the downloadFile() method.  While the readLocalFile() method does
invoke the CopyWithCallback() function and passes a callback
function to it, at this point the size of the object can be
determined from the file in local storage, so the callback function
receives a non-negative value in its "total" parameter.

However, as described above, when the "git lfs smudge" command is
in use and a content-encoded download occurs, the callback function
created by the CopyCallbackFile() method will receive a negative
value in its "total" parameter, which means the function will write
to the progress log regardless of the amount of time that has
elapsed since the previous message was written.

Further, we expect the changes in PR #6011 to result in a wider
range of conditions under which the callback function will receive
a negative value in its "total" parameter.

We therefore now enhance both the callback function and the internal
implementation of the Read() method of the BodyWithCallback and
CallbackReader structures in our "tools" package so that a progress
log message will always be written when the final bytes of a given
object are read.

First, we update the doCallbackIfRequired() method of the
"callbackState" structure in our "tools" package so that it checks
if an EOF condition has been encountered, and if so, checks whether
the amount of data read so far exceeds the total size that was
provided initially for the given object.  Should both of these
conditions apply, the method resets the "totalSize" field of the
"callbackState" structure to the final value of the "readSize"
field, and then runs the doCallback() method to invoke the callback
function.

As a result of this change, a callback will be performed the first
time the EOF condition is encountered while processing an object
whose total size was initially unknown, and when this callback is
made, the callback function's "total" parameter will not have a
negative value but will have the same value as the function's
"written" parameter.

Next, we update the CopyCallbackFile() method of the GitFilter
structure in our "lfs" package so that the callback function the
method creates now checks whether the "total" parameter passed to
the function has a different value than it did the previous time
the function was invoked.  If this is the case, and if the current
value of the "total" is also equal to the non-negative value of the
"written" parameter, then the function writes a message to the
progress log regardless of the amount of time that has elapsed since
the previous message was written.

This change has the key consequence that if the callback function
is invoked one or more times with a negative value for its "total"
parameter, and is invoked a final time with matching non-negative
values for its "written" and "total" parameters, it will always
write a progress log message at that time.

When this change is combined with the revisions we have made to the
"callbackState" structure's doCallbackIfRequired() method in this
commit, the result is that if an object's total size is initially
unknown, when an EOF condition is encountered while reading the
object's data, then a final progress log message is guaranteed to
be written with the total size of the object represented as equal
to the actual number of bytes that were read.

To verify this new behaviour, we add or update a number of our tests,
starting with the TestCopyCallbackFileThrottle() test function in
our "lfs" package.  Most significantly, we update this function's
"initial total unknown" test case so that it now expects a final
log message in which the total size of the object is not negative
but matches the total number of bytes read.

Note that the first log message expected by our new "initial total
unknown" test case still contains a "-1" in the denominator of the
fraction representing the amount of source data read so far.  We will
refine this field in a subsequent commit in this PR so that negative
"total" parameter values are no longer reported in a literal manner.

We also add two new test cases for the TestCopyCallbackFileThrottle()
function, in order to simulate situations where the total size of
an object is initially either overestimated or underestimated.  In
both cases, a final callback should be made in which the "total"
parameter value has been adjusted to match the actual number of
bytes that have been read when the EOF condition is reached.

To simulate these additional test cases, we expand the setup() and
Assert() methods of the "copyCallbackFileThrottleTestCase" structure
we introduced to our "lfs/util_test.go" source file in a previous
commit in this PR.

For the particular test case where the total size of an object is
underestimated, the Assert() method now performs three extra calls to
the Read() method of the CallbackReader structure, with a simulated
delay between the first two of these calls.  The first of these calls
should never result in a log message being written, while the second
should produce a log message in which the number of bytes read so far
exceeds the total size of the object.  For both new test cases, when
the EOF condition is reached, a callback should be made which produces
a log message with the object's total size adjusted in the final log
message to match the actual number of bytes read.

To independently verify the new behaviour of the "callbackState"
structure's doCallbackIfRequired() method, we add a test function to
our "tools" package which also simulates situations in which the
total size of the object is either unknown, overestimated, or
underestimated.

The TestBothCallbackReadersInvokeCallbackOnEOFWhenTotalSizeIncorrect()
function confirms that the Read() methods of both the BodyWithCallback
and CallbackReader structures perform a final callback when the EOF
condition is reached and pass the callback function an updated value
for the total size of the object which matches the actual number of
bytes read.  Note that in a prior commit in this PR we refactored the
Read() methods of both of the BodyWithCallback and CallbackReader
structures so that these methods now just invoke the read() method of
a "callbackState" structure, which in turn executes the
doCallbackIfRequired() method, so we do not need to provide a test
of the doCallbackIfRequired() method itself.

In addition to these changed or new Go test functions, we also expand
our "t/t-progress.sh" shell script with two new tests, one of which
exercises the "git lfs smudge" command under the specific set of
conditions we described above.

Our new "progress log: unknown initial size (smudge)" shell test
forces the use of the "git lfs smudge" command by a "git clone"
command and specified a progress log file using the GIT_LFS_PROGRESS
environment variable.  As well, in the repository cloned by the
"git clone" command, the test establishes a Git LFS object with
the special key string "storage-download-encoding-gzip" as its
contents, which causes our "lfstest-gitserver" test utility program
to return the object's contents in an HTTP response with a
gzip-encoded body and without a Content-Length header.

The test then checks that the progress log file contains an entry
with the full size of the object reported in both the numerator and
denominator of the field which represents the amount of data
downloaded for the object.  Previously, all progress log entries
generated by the "git lfs smudge" command while downloading a
content-encoded object would have a "-1" in the field's denominator.

Note that any intermediate entries preceding the final log entry
will still have a negative value in this field, but we will address
that issue in a subsequent commit in this PR.

(Note also that we use the same "storage-download-encoding-gzip"
special object content key for a more complete test of the Git LFS
client's handling of gzip-encoded responses in the "batch storage
HTTP download with gzip encoding" test in our
"t/t-batch-storage-encoding.sh" shell script.)

The other new shell test in our "t/t-progress.sh" script is named
"progress log: unknown initial size (fetch/pull)", and operates in
a similar manner to the "progress log: unknown initial size (smudge)"
test except that it exercises the "git lfs fetch" and "git lfs pull"
commands rather than the "git lfs smudge" command.

As noted in the following comment on PR #6011, our "git lfs fetch"
and "git lfs pull" commands may write progress log entries with a "-1"
value in the denominator of the field which represents the amount of
data downloaded for the object, if the HTTP response to an object
download request is content-encoded and lacks a Content-Length header.

  #6011 (comment)

In these commands, a transfer queue is created using the
WithProgress() option function rather than the WithProgressCallback()
option function, so the "cb" field of the TransferQueue structure
from our "tq" package is nil.  However, both commands pass a Meter
structure from our "tq" package to the WithProgress() function,
so the TransferQueue structure's "m" field is non-nil.

As a result, when the TransferQueue structure's ensureAdapterBegun()
method is executed, the callback function it creates will execute
only the TransferBytes() method of the Meter structure, which in
turn does not immediately return without taking any action because
its "m" receiver parameter is non-nil.  By comparison, when the
WithProgressCallback() option function is used instead of the
WithProgress() option function, this same callback function runs
the TransferBytes() method, which returns immediately, and then
runs the callback function specified by the non-nil "cb" field.

The callback function created by the ensureAdapterBegun() method
is then passed to the Begin() method of the transfer adapter, and
ultimately passed to the CopyWithCallback() function of our "tools"
package by the "basicDownloadAdapter" structure's download() method.
(Note that this is also true for our "git lfs filter-process" and
"git lfs smudge" commands, not just for the "git lfs fetch"
and "git lfs pull" commands.)

As described above regarding the "git lfs smudge" command, the
CopyWithCallback() function initializes a new CallbackReader structure
and so the callback function passed to the CopyWithCallback() function
will ultimately be invoked as object data is read from the HTTP
response body.  With the "git lfs fetch" and "git lfs pull" commands,
though, this callback function only runs the TransferBytes() method
of the Meter structure, and that method does not return immediately
but runs the logBytes() method of the same structure.

For historical reasons which date back to the introduction of the
logBytes() method in commit 710862b
of PR #476, this method writes a line to the progress log file with
the identical format as the lines generated by the callback function
created by the CopyCallbackFile() method of the GitFilter structure.

However, unlike that anonymous callback function, the logBytes()
method does not limit its output based on the amount of time that
has elapsed since it was previously invoked.  When we revised the
CopyCallbackFile() method in PR #5504 to implement a mechanism
which throttled the frequency of its output to the progress log,
we did not also update the logBytes() method to do the same.

While we likely should refactor the logBytes() method and the
CopyCallbackFile() method's callback function to share a common
implementation, that change falls outside of the scope of this PR,
so we defer it to a future PR.

What this PR does accomplish, though, is to ensure that regardless
of their source, when progress log messages are written for an object
whose total size is initially unknown, a final callback will be made
after the EOF condition is encountered, and this will generate a
progress log message in which the total size has been adjusted to
match the actual number of bytes read.

For this reason, our "progress log: unknown initial size (fetch/pull)"
shell test exercises the "git lfs fetch" and "git lfs pull" commands
with a progress log file specified in the GIT_LFS_PROGRESS environment
variable and an HTTP object download response that lacks a
Content-Length header.  Like our other new shell test, this test
also uses the "storage-download-encoding-gzip" special object
content key to cause the "lfstest-gitserver" utility to gzip-encode
its response to an object download request.

The test then confirms that the progress logs generated by the
"git lfs fetch" and "git lfs pull" commands contain an entry with
the full size of the object reported in both the numerator and
denominator of the field which represents the amount of data
downloaded for the object.  Previously, all progress log entries
generated by these commands while downloading a content-encoded
object would have a "-1" in the field's denominator.

Note that any intermediate entries preceding the final log entry
will still have a negative value in this field, but we will address
that issue in a subsequent commit in this PR.
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.
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.

2 participants