Skip to content

Retry on oversize file - #3518

Merged
bk2204 merged 1 commit into
git-lfs:masterfrom
bk2204:retry-on-oversize-file
Feb 22, 2019
Merged

bk2204 merged 1 commit into
git-lfs:masterfrom
bk2204:retry-on-oversize-file

Conversation

@bk2204

@bk2204 bk2204 commented Feb 12, 2019

Copy link
Copy Markdown
Member

If we're attempting to resume a download but the file we have on disk is the expected size or too large, we'll send an invalid Range header where the lower bound is greater than the upper bound. If this situation occurs, simply retry the download from the beginning, since we clearly don't have the expected data (or we wouldn't be resuming) and it isn't clear how else to recover.

This fixes #3494.

@bk2204
bk2204 requested a review from a team February 12, 2019 20:18
Comment thread t/t-resume-http-range.sh
printf "%s" "${contents/st/aa}" >".git/lfs/incomplete/$contents_oid.tmp"

GIT_TRACE=1 git lfs fetch 2>&1 | tee fetchresume.log
GIT_TRACE=1 git lfs fetch 2>&1 | tee fetchresume.log

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Is the expected behavior here that the first fetch will delete the unexpectedly large on-disk file after the first fetch so the second fetch can do the transfer from scratch? If so, should we verify that the file has been deleted?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Yeah, that's the idea. I probably need to add a comment to that effect here.

I'll add some assertions that we've deleted the problematic file and ensure that the new file has the correct hash.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I'm not sure that that behavior is worth testing explicitly. I think that what we care about at the integration level is that the fetch was executed successfully (by however that fetch actually works).

Testing an implementation like deleting the file in question makes such a test brittle in my mind.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I've decided to leave it as it is and add some comments to help make the situation a bit easier to understand.

Comment thread t/cmd/lfstest-gitserver.go Outdated
byteLimit = 8
batchResumeFailFallbackStorageAttempts++
}
} else if len(by) == len("status-batch-retry") && string(by) == "status-batch-retry" {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

We may be copying what's already here, but is there a need to check the string both for string and length equality? Shouldn't the later follow from the former?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I think this was an optimization (perform the cheap check first), but I'm happy to just remove it and switch to a string comparison. I expect Go does the same comparison internally anyway.

if match != nil && len(match) > 2 && len(match[2]) != 0 {
first, _ := strconv.ParseInt(match[1], 10, 32)
second, _ := strconv.ParseInt(match[2], 10, 32)
if second < first {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Do you think it would be worth adding a comment indicating what it means to be in this conditional?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Sure, I can add comments for these conditionals.

return
}
}
if match != nil && len(match) > 1 {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Likewise in this conditional?

Comment thread t/t-resume-http-range.sh
printf "%s" "${contents/st/aa}" >".git/lfs/incomplete/$contents_oid.tmp"

GIT_TRACE=1 git lfs fetch 2>&1 | tee fetchresume.log
GIT_TRACE=1 git lfs fetch 2>&1 | tee fetchresume.log

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I'm not sure that that behavior is worth testing explicitly. I think that what we care about at the integration level is that the fetch was executed successfully (by however that fetch actually works).

Testing an implementation like deleting the file in question makes such a test brittle in my mind.

Comment thread t/t-resume-http-range.sh

# this string announces to server that we want a test that
# interrupts the transfer when started from 0 to cause resume
contents="status-batch-retry"

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks for documenting this. When I first explored these tests, I was often confused by why these magic strings caused certain things to happen; this is really helpful for similar newcomers.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I actually need to revise this, since this comment is pulled in from another test assertion. But I will replace it with a hopefully equally helpful comment.

@bk2204
bk2204 force-pushed the retry-on-oversize-file branch from 5b0ed34 to 85d72e1 Compare February 22, 2019 14:43
If we're attempting to resume a download but the file we have on disk is
the expected size or too large, we'll send an invalid Range header where
the lower bound is greater than the upper bound. If this situation
occurs, simply retry the download from the beginning, since we clearly
don't have the expected data (or we wouldn't be resuming) and it isn't
clear how else to recover.
@bk2204
bk2204 force-pushed the retry-on-oversize-file branch from 85d72e1 to 73e8a71 Compare February 22, 2019 14:46
@bk2204

bk2204 commented Feb 22, 2019

Copy link
Copy Markdown
Member Author

I think I've addressed all of the concerns here. There are now copious correct comments, and I've removed the “optimization” which Go likely already performs.

@ttaylorr ttaylorr left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks, this looks great.

@bk2204
bk2204 merged commit f34fd84 into git-lfs:master Feb 22, 2019
@bk2204
bk2204 deleted the retry-on-oversize-file branch February 22, 2019 19:52
chrisd8088 added a commit to chrisd8088/git-lfs that referenced this pull request Feb 26, 2026
In PR git-lfs#1297 we updated the Git LFS client so it attempts to resume
interrupted object download transfer requests by retrying the request
and adding a Range header.  The original version of this PR implemented
support for the resumption of HTTP object download requests in a
dedicated transfer adapter named "http-range".  This transfer adapter
was not included in the final version of the PR, though, because we
simply enhanced our basic transfer adapter so it will retry an
interrupted download request using a Range header to retrieve the
missing portion of the object.

References to the prototype "http-range" transfer adapter still remain,
however, in the name of the "t/t-resume-http-range.sh" shell script
added PR git-lfs#1297, and in the names of the tests it contains.  The first
two tests in this script were introduced in that PR, and they verify
that after an interrupted object download request, the client makes
another request with a Range header.  The first confirms that the client
correctly handles a successful response with a 216 Partial Content status
code, while the second test ensures that if the server replies with a
416 Range Not Satisfiable status code, the client then retries the full
original request without a Range header.

We further updated our basic transfer adapter in PR git-lfs#3518 so that it
would avoid making requests with a Range header unless the bytes indexes
it would provide in the header had sensible values.  In particular,
if the client has previously downloaded sufficient data to meet or
exceed the expected size of the object, but another attempt has been
enqueued for the same object due to an HTTP error, the next request
will simply retry the full original request.

To verify this updated behaviour, PR git-lfs#3518 added a third test named
"resume-http-range-retry" to the "t/t-resume-http-range.sh" script.
This test is intended to check that the Git LFS client retries an
object download request without using a Range header if the object
data already downloaded has the same length as the object.

At present, all of these tests succeed, but they also all fail to
properly simulate the conditions they intend to test, and in the case
of the "resume-http-range-retry" test the consequence is that it now
just passes trivially regardless of the test conditions.

We will address these issues in other commits in this PR, but first
we move the tests from the "t/t-resume-http-range.sh" script into the
"t/t-batch-storage-retries.sh" script (which we renamed from the
"t/t-batch-retries.sh" script in a prior commit in this PR).

By doing so, we consolidate all of the tests related to the client's
handling of failed or interrupted object transfer HTTP requests, except
for those which verify our support of the non-standard "tus" protocol.
(Note that in a subsequent commit in this PR we expect to also rename
the "t/t-resume-tus.sh" script that contains our tests of the "tus"
protocol.)
chrisd8088 added a commit to chrisd8088/git-lfs that referenced this pull request Feb 26, 2026
Since commit f082660 of PR git-lfs#1297 our
"lfstest-gitserver" test utility program has responded to certain
HTTP requests with Range headers by returning a 206 Partial Content
status code and the specified portion of a stored Git LFS object in
the body of the HTTP response.  These responses also include a
Content-Range header whose purpose is to identify the exact portion
of the content that was returned in the response body.

However, at present our test utility returns invalid data in these
Content-Range headers.  If the Git LFS client makes a request with a
Range header which specifies the byte range "10-27" of a 28-byte object,
the Content-Range header we generate will appear as "bytes 10-28/-18"
instead of the correct value, which should be "bytes 10-27/28".
For reference regarding the official specification of this header, see:

  https://www.rfc-editor.org/rfc/rfc9110.html#field.content-range

The first error in our Content-Range headers occurs because we use the
size of the object as the end position byte index, but both indexes are
zero-based and so should never exceed one less than the full size of the
object.

The second error occurs because we do not simply use the full size of
the object as the value of the third, denominator-like field, but
instead subtract the size of the object from the start position byte
index, which yields a negative integer.

To resolve this issue, we adjust the two instances in our test utility
where we format a Content-Range header so that both cases we use the
correct values in all three of the header's numeric fields.

Note that we copied the original, incorrect code from PR git-lfs#1297 in commit
73e8a71 of PR git-lfs#3518 when we added
support in the "lfstest-gitserver" utility for what was at the time named
the "resume-http-range-retry" test in the "t/t-resume-http-range.sh"
script.  In prior commits in this PR we have now moved this test to the
"t/t-batch-storage-retries.sh" script and renamed it as the "batch
storage HTTP download retries without invalid Range header" test.
chrisd8088 added a commit to chrisd8088/git-lfs that referenced this pull request Feb 26, 2026
In commit 73e8a71 of PR git-lfs#3518 we
updated the Git LFS client so that when it attempts to download an
object over HTTP after one or more previous attempts have failed,
if the object data accumulated from the earlier HTTP responses meets
or exceeds the object's expected size and yet is not considered valid
based on its SHA-256 hash, the client will make a request for the
full object and not use a Range header to request only a portion of
the object's data.

To verify the client behaves as expected under these conditions, the
same commit also included a new test which creates a file in the
".git/lfs/incomplete" directory before running a "git lfs fetch"
command.  The file the test creates is designed to match the size
and OID of the Git LFS object the test creates, but contain invalid
content, thus forcing the command to request the entire object rather
than only a portion of it.

(Note that in prior commits in this PR we moved this test from its
previous location in the "t/t-resume-http-range.sh" shell script to a
new script file named "t/t-batch-storage-retries.sh", and we also
changed the name of the test from "resume-http-range-retry" to
"batch storage HTTP download retries without invalid Range header".)

At the time the test was first written, it worked as intended.  However,
in commit 1edb976 of PR git-lfs#3826 we
altered the name of the temporary files created by the Git LFS client
in the ".git/lfs/incomplete" directory, as part of a set of revisions
to ensure multiple worker routines do not generate conflicting
operations on these files.

Since PR git-lfs#3826 the files the client stores in the ".git/lfs/incomplete"
directory now have a file extension of ".part", whereas they had the
extension ".tmp" when PR git-lfs#3518 was merged.  Because the test was not
updated in PR git-lfs#3826, it still uses the ".tmp" extension on the file
it creates in the ".git/lfs/incomplete" directory.

The consequence is that when the test added by PR git-lfs#3518 runs, the
client just ignores the file with the ".tmp" extension and so the
"git lfs fetch" command executed by the test downloads the given Git LFS
object as if no previous downloads had been attempted.  The test performs
no additional checks other than confirming that the object has been
downloaded, and therefore passes trivially even though it fails to
simulate the conditions it was designed to assess.

We resolve this problem simply by updating the test to use the ".part"
file extension when creating files in the ".git/lfs/incomplete"
directory.

However, we would like to guard against future regressions which might
cause this test to again become ineffective.  To do this, we introduce
an initial check which also creates a file in the ".git/lfs/incomplete"
directory and then runs a "git lfs fetch" command.  In this instance,
though, the file we create contains only a valid but truncated portion
of the Git LFS object's data.  We then verify after the "git lfs fetch"
command is executed that it detected the presence of the incomplete
data file and so issues an HTTP request with a Range header to download
just the missing portion of the object's data.

With this additional check in place, the test should now fail if in the
future we alter how the Git LFS client manages its temporary storage of
data during object downloads, but forget to alter this test as well.
The initial "git lfs fetch" command would likely still download the
object successfully, but do so without using a Range header in its
request, and our new check will fail when that header is not found
among the command's trace log output.

Note, though, that this extra check specifically does not suffice to
verify the changes made to the client in PR git-lfs#3518.  As described above,
the key behaviour the test needs to confirm is that the client retries
a full object download when it finds a temporary file left by a previous
download attempt but the file is not smaller than the object to be
retrieved.

At present, however, the test does not actually verify this behaviour on
the part of the client, other than to check that it ultimately succeeds
in downloading the full object.  To make sure the test validates that
the object is retrieved without using a request with a Range header,
we now capture trace log output from the "git lfs fetch" command and
check that it does not include any indication that a request with a
Range header had been performed.

The original version of the test actually ran the "git lfs fetch" command
twice, although not for the reason that we now run the command an extra
time, since we have added a preliminary instance of the command solely to
confirm that the client will find the temporary object data files the
test creates.

According to the comments in the test, the reason it runs the "git lfs
fetch" command twice is because the first instance of the command may
experience an error.  This behaviour was likely observed when the test
was first written because the test was designed to replicate the problem
reported in git-lfs#3494, before PR git-lfs#3518 was developed to resolve that issue.

Without the changes introduced in PR git-lfs#3518 (or PR git-lfs#3826), the first time
the "git lfs fetch" command was run it would have made a request with
a Range header whose contents were invalid, such as "bytes=18-17" for
an 18-byte object.  The client would then receive a response from the
server with a 400 Bad Request status code, remove the invalid temporary
object data file, and report an error.  When the command was run a
second time, though, no temporary file would be found and the client
would make a request for the entire object, which would succeed.

Since the modifications from PR git-lfs#3518 were merged, though, the test has
likely never required the second instance of the "git lfs fetch" command,
even before the test was inadvertently made ineffective by the changes
from PR git-lfs#3826.  We can therefore just remove the extra invocation of the
"git lfs fetch" command (but note that as described above, we have added
one initial invocation of the command which runs under different
conditions, in order to guard regressions in the test's design).

In addition to the existing call to the assert_local_object() assertion
function at the end of the test, we add several grep(1) commands to
check for a number of trace log messages and HTTP headers that we expect
the final "git lfs fetch" command to output.  As with our new, initial
invocation of that command, we set the GIT_CURL_VERBOSE environment
variable to a value considered "true" so we can capture all the headers
from the HTTP requests made by the client.

After each "git lfs fetch" command runs, we now also verify that the
command succeeded by checking that it returned a zero exit code.
Following an idiom we use in many of our shell tests, we check the first
element of the PIPESTATUS array variable to determine whether the
"git lfs fetch" command succeeded.

Previously at least the first command run by the test would return a
non-zero exit code, but this did not cause the tests to fail because the
shell's "errexit" attribute ignores the exit codes of all commands in
a pipeline other than the final one (which is why we need to check the
PIPESTATUS array variable).

We also adjust the log files captured by the test to match our standard
practice in other similar tests.  We now use the simple file name
"fetch.log" for the log files in which we capture the "git lfs fetch"
commands' output.  We also remove the redirection and pipeline which
generated a log file when running the "git commit" command, as we expect
this command to succeed and the test did not check the "commit.log"
file at all.

Finally, to help clarify the purpose of the test's various steps, we
rewrite and expand its internal code comments to more accurately explain
how it operates.  As well, we add a comment to the portion of the
"lfstest-gitserver" test utility's storageHandler() function that is
exercised by this test to indicate that the program should never
receive a Range header from the client with an invalid set of start
and end position byte indexes.  The utility's check that the provided
byte range is sensible was added while investigating and reproducing
the issue reported in git-lfs#3494, but this condition should never occur
due to the changes made to the client in PR git-lfs#3518.
chrisd8088 added a commit to chrisd8088/git-lfs that referenced this pull request Feb 26, 2026
In both PR git-lfs#1297 and PR git-lfs#3518 we introduced tests which exercise
the Git LFS client in conditions where it attempts to resume an
interrupted object download transfer request by retrying the request
and, in some cases, adding a Range header.

(Note that in prior commits in this PR we moved these tests from their
previous location in the "t/t-resume-http-range.sh" shell script to a
new script file named "t/t-batch-storage-retries.sh", and we also
renamed the tests to better reflect their purpose.)

These tests require our "lfstest-gitserver" test utility program to
parse any Range headers sent by the Git LFS client, and so both PRs
added logic to the utility for this purpose, in two separate blocks.
In both blocks, the program first checks if a Range header was provided
in the current HTTP request to download an object, and if a Range
header is found, then the program applies a regular expression to try
to extract start and end position byte indexes from the header's value.

As we may in the future want to add other tests that require the
"lfstest-gitserver" utility to parse Range headers, we refactor the
program now to consolidate its parsing logic for these headers into
a single common function named handleRangeRequest().

We also take the opportunity to make the utility's parsing of Range
header values more strict.  For instance, the program will now return
an HTTP response with a 400 Bad Request status code if the Range
header's value does not match against the regular expression we use
parse it.

Likewise, the program now returns a response with a 400 Bad Request
status code if the client sends both a Range header and an
Accept-Encoding header.  While the HTTP specification allows both
both headers to appear in a request, if the content is encoded by
the server then the start and end position indexes in the Range header
should then be interpreted as offsets into the encoded content:

  https://www.rfc-editor.org/rfc/rfc9110.html#section-14.1.2-3

Since the Git LFS client calculates byte range indexes it sends with
Range headers as offsets into the unencoded object data, the client
should never sent an Accept-Encoding header with a Range header as
the server might then choose to encode the object data and then apply
the requested byte range to the encoded data.  For the same reason,
the "net/http" package in the Go standard library does not automatically
insert an "Accept-Encoding: gzip" header into a request unless no Range
header is present:

  https://github.com/golang/go/blob/go1.25.7/src/net/http/transport.go#L2768
  https://github.com/golang/go/blob/go1.25.7/src/net/http/transport.go#L2779-L2781

We slightly refine the regular expression used to parse the value of
a Range header, and add a comment which notes that the expression would
not match a valid header value that followed the "suffix-range" format
(e.g., "bytes=-100") but that we expect the Git LFS client to never
send a header with such a value.

If an end position byte index is provided, we now always check that it
is at least equal to the start position index, and if it is not, we
return a 400 Bad Request status code.  For greater clarity, we now also
use the aliases from the "net/http" package in place of literal HTTP
status codes like 400.

Finally, our changes allow us to remove the "resumeAt" variable from the
storageHandlers() function, so we do that as well.
chrisd8088 added a commit to chrisd8088/git-lfs that referenced this pull request Mar 1, 2026
In PR git-lfs#1297 we updated the Git LFS client so it attempts to resume
interrupted object download transfer requests by retrying the request
and adding a Range header.  The original version of this PR implemented
support for the resumption of HTTP object download requests in a
dedicated transfer adapter named "http-range".  This transfer adapter
was not included in the final version of the PR, though, because we
simply enhanced our basic transfer adapter so it will retry an
interrupted download request using a Range header to retrieve the
missing portion of the object.

References to the prototype "http-range" transfer adapter still remain,
however, in the name of the "t/t-resume-http-range.sh" shell script
added PR git-lfs#1297, and in the names of the tests it contains.  The first
two tests in this script were introduced in that PR, and they verify
that after an interrupted object download request, the client makes
another request with a Range header.  The first confirms that the client
correctly handles a successful response with a 216 Partial Content status
code, while the second test ensures that if the server replies with a
416 Range Not Satisfiable status code, the client then retries the full
original request without a Range header.

We further updated our basic transfer adapter in PR git-lfs#3518 so that it
would avoid making requests with a Range header unless the bytes indexes
it would provide in the header had sensible values.  In particular,
if the client has previously downloaded sufficient data to meet or
exceed the expected size of the object, but another attempt has been
enqueued for the same object due to an HTTP error, the next request
will simply retry the full original request.

To verify this updated behaviour, PR git-lfs#3518 added a third test named
"resume-http-range-retry" to the "t/t-resume-http-range.sh" script.
This test is intended to check that the Git LFS client retries an
object download request without using a Range header if the object
data already downloaded has the same length as the object.

At present, all of these tests succeed, but they also all fail to
properly simulate the conditions they intend to test, and in the case
of the "resume-http-range-retry" test the consequence is that it now
just passes trivially regardless of the test conditions.

We will address these issues in other commits in this PR, but first
we move the tests from the "t/t-resume-http-range.sh" script into the
"t/t-batch-storage-retries.sh" script (which we renamed from the
"t/t-batch-retries.sh" script in a prior commit in this PR).

By doing so, we consolidate all of the tests related to the client's
handling of failed or interrupted object transfer HTTP requests, except
for those which verify our support of the non-standard "tus" protocol.
(Note that in a subsequent commit in this PR we expect to also rename
the "t/t-resume-tus.sh" script that contains our tests of the "tus"
protocol.)
chrisd8088 added a commit to chrisd8088/git-lfs that referenced this pull request Mar 1, 2026
Since commit f082660 of PR git-lfs#1297 our
"lfstest-gitserver" test utility program has responded to certain
HTTP requests with Range headers by returning a 206 Partial Content
status code and the specified portion of a stored Git LFS object in
the body of the HTTP response.  These responses also include a
Content-Range header whose purpose is to identify the exact portion
of the content that was returned in the response body.

However, at present our test utility returns invalid data in these
Content-Range headers.  If the Git LFS client makes a request with a
Range header which specifies the byte range "10-27" of a 28-byte object,
the Content-Range header we generate will appear as "bytes 10-28/-18"
instead of the correct value, which should be "bytes 10-27/28".
For reference regarding the official specification of this header, see:

  https://www.rfc-editor.org/rfc/rfc9110.html#field.content-range

The first error in our Content-Range headers occurs because we use the
size of the object as the end position byte index, but both indexes are
zero-based and so should never exceed one less than the full size of the
object.

The second error occurs because we do not simply use the full size of
the object as the value of the third, denominator-like field, but
instead subtract the size of the object from the start position byte
index, which yields a negative integer.

To resolve this issue, we adjust the two instances in our test utility
where we format a Content-Range header so that both cases we use the
correct values in all three of the header's numeric fields.

Note that we copied the original, incorrect code from PR git-lfs#1297 in commit
73e8a71 of PR git-lfs#3518 when we added
support in the "lfstest-gitserver" utility for what was at the time named
the "resume-http-range-retry" test in the "t/t-resume-http-range.sh"
script.  In prior commits in this PR we have now moved this test to the
"t/t-batch-storage-retries.sh" script and renamed it as the "batch
storage HTTP download retries without invalid Range header" test.
chrisd8088 added a commit to chrisd8088/git-lfs that referenced this pull request Mar 1, 2026
In commit 73e8a71 of PR git-lfs#3518 we
updated the Git LFS client so that when it attempts to download an
object over HTTP after one or more previous attempts have failed,
if the object data accumulated from the earlier HTTP responses meets
or exceeds the object's expected size and yet is not considered valid
based on its SHA-256 hash, the client will make a request for the
full object and not use a Range header to request only a portion of
the object's data.

To verify the client behaves as expected under these conditions, the
same commit also included a new test which creates a file in the
".git/lfs/incomplete" directory before running a "git lfs fetch"
command.  The file the test creates is designed to match the size
and OID of the Git LFS object the test creates, but contain invalid
content, thus forcing the command to request the entire object rather
than only a portion of it.

(Note that in prior commits in this PR we moved this test from its
previous location in the "t/t-resume-http-range.sh" shell script to a
new script file named "t/t-batch-storage-retries.sh", and we also
changed the name of the test from "resume-http-range-retry" to
"batch storage HTTP download retries without invalid Range header".)

At the time the test was first written, it worked as intended.  However,
in commit 1edb976 of PR git-lfs#3826 we
altered the name of the temporary files created by the Git LFS client
in the ".git/lfs/incomplete" directory, as part of a set of revisions
to ensure multiple worker routines do not generate conflicting
operations on these files.

Since PR git-lfs#3826 the files the client stores in the ".git/lfs/incomplete"
directory now have a file extension of ".part", whereas they had the
extension ".tmp" when PR git-lfs#3518 was merged.  Because the test was not
updated in PR git-lfs#3826, it still uses the ".tmp" extension on the file
it creates in the ".git/lfs/incomplete" directory.

The consequence is that when the test added by PR git-lfs#3518 runs, the
client just ignores the file with the ".tmp" extension and so the
"git lfs fetch" command executed by the test downloads the given Git LFS
object as if no previous downloads had been attempted.  The test performs
no additional checks other than confirming that the object has been
downloaded, and therefore passes trivially even though it fails to
simulate the conditions it was designed to assess.

We resolve this problem simply by updating the test to use the ".part"
file extension when creating files in the ".git/lfs/incomplete"
directory.

However, we would like to guard against future regressions which might
cause this test to again become ineffective.  To do this, we introduce
an initial check which also creates a file in the ".git/lfs/incomplete"
directory and then runs a "git lfs fetch" command.  In this instance,
though, the file we create contains only a valid but truncated portion
of the Git LFS object's data.  We then verify after the "git lfs fetch"
command is executed that it detected the presence of the incomplete
data file and so issues an HTTP request with a Range header to download
just the missing portion of the object's data.

With this additional check in place, the test should now fail if in the
future we alter how the Git LFS client manages its temporary storage of
data during object downloads, but forget to alter this test as well.
The initial "git lfs fetch" command would likely still download the
object successfully, but do so without using a Range header in its
request, and our new check will fail when that header is not found
among the command's trace log output.

Note, though, that this extra check specifically does not suffice to
verify the changes made to the client in PR git-lfs#3518.  As described above,
the key behaviour the test needs to confirm is that the client retries
a full object download when it finds a temporary file left by a previous
download attempt but the file is not smaller than the object to be
retrieved.

At present, however, the test does not actually verify this behaviour on
the part of the client, other than to check that it ultimately succeeds
in downloading the full object.  To make sure the test validates that
the object is retrieved without using a request with a Range header,
we now capture trace log output from the "git lfs fetch" command and
check that it does not include any indication that a request with a
Range header had been performed.

The original version of the test actually ran the "git lfs fetch" command
twice, although not for the reason that we now run the command an extra
time, since we have added a preliminary instance of the command solely to
confirm that the client will find the temporary object data files the
test creates.

According to the comments in the test, the reason it runs the "git lfs
fetch" command twice is because the first instance of the command may
experience an error.  This behaviour was likely observed when the test
was first written because the test was designed to replicate the problem
reported in git-lfs#3494, before PR git-lfs#3518 was developed to resolve that issue.

Without the changes introduced in PR git-lfs#3518 (or PR git-lfs#3826), the first time
the "git lfs fetch" command was run it would have made a request with
a Range header whose contents were invalid, such as "bytes=18-17" for
an 18-byte object.  The client would then receive a response from the
server with a 400 Bad Request status code, remove the invalid temporary
object data file, and report an error.  When the command was run a
second time, though, no temporary file would be found and the client
would make a request for the entire object, which would succeed.

Since the modifications from PR git-lfs#3518 were merged, though, the test has
likely never required the second instance of the "git lfs fetch" command,
even before the test was inadvertently made ineffective by the changes
from PR git-lfs#3826.  We can therefore just remove the extra invocation of the
"git lfs fetch" command (but note that as described above, we have added
one initial invocation of the command which runs under different
conditions, in order to guard regressions in the test's design).

In addition to the existing call to the assert_local_object() assertion
function at the end of the test, we add several grep(1) commands to
check for a number of trace log messages and HTTP headers that we expect
the final "git lfs fetch" command to output.  As with our new, initial
invocation of that command, we set the GIT_CURL_VERBOSE environment
variable to a value considered "true" so we can capture all the headers
from the HTTP requests made by the client.

After each "git lfs fetch" command runs, we now also verify that the
command succeeded by checking that it returned a zero exit code.
Following an idiom we use in many of our shell tests, we check the first
element of the PIPESTATUS array variable to determine whether the
"git lfs fetch" command succeeded.

Previously at least the first command run by the test would return a
non-zero exit code, but this did not cause the tests to fail because the
shell's "errexit" attribute ignores the exit codes of all commands in
a pipeline other than the final one (which is why we need to check the
PIPESTATUS array variable).

We also adjust the log files captured by the test to match our standard
practice in other similar tests.  We now use the simple file name
"fetch.log" for the log files in which we capture the "git lfs fetch"
commands' output.  We also remove the redirection and pipeline which
generated a log file when running the "git commit" command, as we expect
this command to succeed and the test did not check the "commit.log"
file at all.

Finally, to help clarify the purpose of the test's various steps, we
rewrite and expand its internal code comments to more accurately explain
how it operates.  As well, we add a comment to the portion of the
"lfstest-gitserver" test utility's storageHandler() function that is
exercised by this test to indicate that the program should never
receive a Range header from the client with an invalid set of start
and end position byte indexes.  The utility's check that the provided
byte range is sensible was added while investigating and reproducing
the issue reported in git-lfs#3494, but this condition should never occur
due to the changes made to the client in PR git-lfs#3518.
chrisd8088 added a commit to chrisd8088/git-lfs that referenced this pull request Mar 1, 2026
In both PR git-lfs#1297 and PR git-lfs#3518 we introduced tests which exercise
the Git LFS client in conditions where it attempts to resume an
interrupted object download transfer request by retrying the request
and, in some cases, adding a Range header.

(Note that in prior commits in this PR we moved these tests from their
previous location in the "t/t-resume-http-range.sh" shell script to a
new script file named "t/t-batch-storage-retries.sh", and we also
renamed the tests to better reflect their purpose.)

These tests require our "lfstest-gitserver" test utility program to
parse any Range headers sent by the Git LFS client, and so PRs git-lfs#1297
and git-lfs#3518 each added logic to the utility for this purpose, in two
separate blocks.  In both blocks, the program first checks if a Range
header was provided in the current HTTP request to download an object,
and if a Range header is found, then the program applies a regular
expression to try to extract start and end position byte indexes from
the header's value.

As we may in the future want to add other tests that require the
"lfstest-gitserver" utility to parse Range headers, we refactor the
program now to consolidate its parsing logic for these headers into
a single common function named handleRangeRequest().

We also take the opportunity to make the utility's parsing of Range
header values more strict.  For instance, the program will now return
an HTTP response with a 400 Bad Request status code if the Range
header's value does not match against the regular expression we use
parse it.

Likewise, the program now returns a response with a 400 Bad Request
status code if the client sends both a Range header and an
Accept-Encoding header.  While the HTTP specification allows both
headers to appear in a request, if the content is encoded by the server
then the start and end position indexes in the Range header should be
interpreted as offsets into the encoded content:

  https://www.rfc-editor.org/rfc/rfc9110.html#section-14.1.2-3

Since the Git LFS client calculates byte range indexes it sends with
Range headers as offsets into the unencoded object data, the client
should never sent an Accept-Encoding header with a Range header as
the server might then choose to encode the object data and then apply
the requested byte range to the encoded data.  For the same reason,
the "net/http" package in the Go standard library does not automatically
insert an "Accept-Encoding: gzip" header into a request unless no Range
header is present:

  https://github.com/golang/go/blob/go1.25.7/src/net/http/transport.go#L2768
  https://github.com/golang/go/blob/go1.25.7/src/net/http/transport.go#L2779-L2781

We slightly refine the regular expression used to parse the value of
a Range header, and add a comment which notes that the expression would
not match a valid header value that followed the "suffix-range" format
(e.g., "bytes=-100") but that we expect the Git LFS client to never
send a header with such a value.

If an end position byte index is provided, we now always check that it
is at least equal to the start position index, and if it is not, we
return a 400 Bad Request status code.  For greater clarity, we now also
use the aliases from the "net/http" package in place of literal HTTP
status codes like 400.

Finally, our changes allow us to remove the "resumeAt" variable from the
storageHandlers() function, so we do that as well.
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.

LFS Client Error (InvalidArgumentError)

3 participants