Skip to content

Enhanced transfers: part 3 (resumable downloads) - #1297

Merged
sinbad merged 15 commits into
masterfrom
experimental/transfer-features-p3
Jun 10, 2016
Merged

sinbad merged 15 commits into
masterfrom
experimental/transfer-features-p3

Conversation

@sinbad

@sinbad sinbad commented Jun 8, 2016

Copy link
Copy Markdown
Contributor

This PR implements resumable downloads for all storage hosts which support HTTP Range/Content-Range exchanges. Since it seamlessly falls back to re-downloading the entire file if the server doesn't support a range request, it doesn't hurt to always ask to resume and to proceed depending on what the server responds with (often these servers will just return 200 instead of 206 and send the entire file, which this caters for).

@sinbad sinbad added the review label Jun 8, 2016
Comment thread transfer/basic_download.go Outdated
regex := regexp.MustCompile(`bytes (\d+)\-.*`)
match := regex.FindStringSubmatch(rangeHdr)
if match != nil && len(match) > 1 {
contentStart, _ := strconv.ParseInt(match[1], 10, 32)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

I suspect this should be ParseInt(match[1], 10, 64) otherwise you can't resume when more than 2^31 bytes have already been downloaded

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.

Oops, good catch!

Comment thread transfer/basic_download.go Outdated
return fmt.Errorf("Cannot restart %v from %d without a file & hash", t.Object.Oid, fromByte)
}
// We could just use a start byte, but since we know the length be specific
req.Header.Set("Range", fmt.Sprintf("bytes=%d-%d", fromByte, t.Object.Size))

@benhumphreys benhumphreys Jun 10, 2016 •

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Should this be t.Object.Size - 1 ?
RFC 7233 seems to suggest the range is zero based inclusive. See RFC 7233 section 2.1. Though this value should work, as the RFC says:

If the last-byte-pos value is absent, or if the value is greater than or equal to the current length of the representation data, the byte range is interpreted as the remainder of the representation (i.e., the server replaces the value of last-byte-pos with a value that is one less than the current length of the selected representation).

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.

You're right, will fix. Although by fluke the spec also says it will be corrected even though I got it wrong ;D

@sinbad
sinbad merged commit 93c6d40 into master Jun 10, 2016
@sinbad
sinbad deleted the experimental/transfer-features-p3 branch June 10, 2016 08:16
chrisd8088 added a commit to chrisd8088/git-lfs that referenced this pull request Feb 26, 2026
In our "lfstest-gitserver" test utility program, the storageHandler()
function is used to generate responses to HTTP requests which upload
or download individual Git LFS objects.  For both types of requests,
if the content of an object matches a specific "magic" string value
then the function simulates a set of conditions relevant for the
current shell test by returning a particular status code or checking
for a particular request header.

When handling a PUT object upload request, the object's SHA-256 hash
value as provided by the Git LFS client in the URL path is used to
determine whether the object content matches a given special or "magic"
key value.  For these types of requests, the storageHandler() function
first looks for an entry in the global oidHandlers map using the provided
OID as the key.  If an entry exists, its value is then checked against a
series of keys, and if a match is found, the PUT upload handler simulates
the relevant test conditions.

By contrast, when handling a GET object download request, the same
storageHandler() function does not refer to the oidHandlers map.
Instead, if it finds a stored object matching the OID provided by the
client, it then tries to match the object's content against each of a
series of special key strings.  Each match check is made by comparing
the length of the string and the length of the byte array containing
the object content, and then if those are equal, comparing the contents
of the string and the byte array.

This technique was first introduced to the object download handling
logic in commit c9c457c of PR git-lfs#1297
and then copied every time another special object content key was added
in later commits and PRs.

While functional, the discrepancy between the object upload and download
handling portions of the storageHandler() function somewhat obscures the
similarities between the two code segments, and has also led to some
confusion as to which special object content keys need to be defined in
the global contentHandlers array, from which the oidHandlers map is
populated.

For instance, in commit 864beb8 of
PR git-lfs#1585 the "storage-download-retry" string was added to the
contentHandlers array, but the corresponding entry in the oidHandlers
array is never utilized.

As we expect to update and correct some of the specialized handling
of object download test conditions in subsequent commits in this PR,
we first revise the storageHandler() function so that it mirrors the
design of the object upload handling section.  When responding to a
GET request, the function now checks for an entry in the oidHandlers
map, and if an entry is found, then uses its value in a "switch"
statement to search for a match against a specific string.

To make this change successful, we also need to register several
additional special object content keys in the contentHandlers array,
specifically the "status-batch-retry", "storage-download-retry-later",
and "storage-download-retry-later-no-header" strings.
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 commits c9c457c and
c8817ff of PR git-lfs#1297 we implemented
a pair of tests to verify that the Git LFS client attempts to resume
interrupted object download transfer requests by retrying the request
and adding 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.

(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.)

To simulate an HTTP response being interrupted, the same commits in
PR git-lfs#1297 updated the storageHandler() function in our "lfstest-gitserver"
test utility program to truncate the data it returned when an object's
content matched specific "magic" strings.  The tests added in the PR
create objects with those special keys as their content, and expect
that downloads of those objects may be interrupted, given certain other
conditions.

When these conditions apply, the storageHandler() function truncates the
data it sends in its responses to GET requests by returning only a slice
from the byte array that contains the object data.

However, the function relies on the "net/http" package from the Go
standard library to generate a Content-Length response header, which
it does based on the length of the data it receives.  As a result,
the Content-Length header values returned to the client with truncated
object data reflect the length of that shortened data and not the
actual Git LFS object size, as they would if a normal object download
request experienced a connection failure.

When the client receives the response with the truncated object data,
the "net/http" package uses the value from the Content-Length header
to determine whether or not the response body has been fully received.
Since the length of the data matches the header's value, the "net/http"
packages terminates the data with a regular EOF marker, and so the
Git LFS client assumes the object has been fully downloaded.  It then
checks the SHA-256 hash value it has computed from the data against the
object's ID, and when these do not match, reports an error and does
not retry the object download request.  The incomplete object data is
retained, however, in a file in the ".git/lfs/incomplete" directory.

If another Git LFS command is run which also attempts to download
the same object, it will locate the temporary file left by the previous
attempt and then make another download request, this time using a
Range header to request the missing object data.

Both of the tests added in PR git-lfs#1297 therefore execute an initial
"git lfs fetch" command, which stops after downloading the first
portion of a test object, and then execute a second "git lfs fetch"
command and check its trace log output to confirm that an HTTP request
with a Range header was sent.  As a consequence, these tests do not
actually verify whether a single Git LFS command will attempt to
resume an interrupted object download request.

We now revise the "lfstest-gitserver" utility to correctly simulate
an interrupted HTTP response when responding to the initial requests
from these tests, which in turn allows us to simplify the tests because
they each need to run just a single "git lfs fetch" command.

The only change we need to make to the "lfstest-gitserver" utility is
in the section of the storageHandler() function which responds to GET
requests.  When one of the two tests' special object content keys is
detected and the other relevant conditions are met, the function
indicates that the response data should be truncated by setting the
"byteLimit" variable to a positive integer.  We therefore insert a
conditional block prior to the output of the response data which checks
for a "byteLimit" value greater than zero, and in that case explicitly
sets the Content-Length header to the full object's size.

We then alter both the "batch storage HTTP download retries with Range
header" and "batch storage HTTP download retries after Range header
rejected" tests in the "t/t-batch-storage-retries.sh" script so they
each only perform a single "git lfs fetch" command and then check
that the command has successfully downloaded a Git LFS object.

In addition to the existing calls to the assert_local_object() test
assertion function we add several grep(1) commands to check for a
number of trace log messages and HTTP headers that we expect the
"git lfs fetch" commands to output.  To capture the HTTP headers we
set the GIT_CURL_VERBOSE environment variable to a value considered
"true" before executing the "git lfs fetch" commands.

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 the first command in each test would return a non-zero
exit code, but this is no longer the case.  (Note that 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.)

As we are simplifying the operation of the tests, we also rewrite their
internal code comments to more accurately explain how the tests operate.
In particular, we correct the comments which stated that the initial
"git lfs fetch" commands reported an error because their object download
requests were interrupted, since that is not technically accurate.
In fact, the commands formerly reported errors because the hashes of
the object data they downloaded did not match the corresponding OIDs,
while the responses to all their HTTP requests were considered complete
and successful due to the values provided in the Content-Length headers.

We also take the opportunity to adjust the log files captured by the
tests 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
redirections and pipelines which generated a log file when running
"git commit" commands, as we expect those commands to succeed and the
tests did not check the "commit.log" files at all.
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 Feb 26, 2026
In PR git-lfs#1303 we updated the Git LFS client to support version 1.0 of
the "tus" protocol, which allows for HTTP upload requests to be resumed
if they are interrupted:

  https://tus.io/protocols/resumable-upload/1-0-x

In the same PR, we also added tests to our shell test suite to verify
that the when the "lfs.tusTransfers" configuration option is set to
a value considered "true", the Git LFS client will use the "tus"
protocol when uploading individual objects over HTTP, and that the
client will retry an interrupted request and send the additional data
necessary to fully upload an object.

These tests are located in the "t/t-resume-tus.sh" script, whose name
follows the pattern established in PR git-lfs#1297 when we created the
"t/t-resume-http-range.sh" shell script.  However, only one of the
two tests in the "t/t-resume-tus.sh" script actually checks whether
an interrupted object upload request can be resumed with the "tus"
protocol.

In a previous commit in this PR we moved the tests from the
"t/t-resume-http-range.sh" script to a new shell script named
"t/t-batch-storage-retries.sh", in order to 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 "tus" protocol.

To keep the names of our test scripts as consistent as possible
we now rename the "t/t-resume-tus.sh" script to
"t/t-batch-storage-upload-tus.sh", which also better reflects the
fact that both tests in the "t/t-resume-tus.sh" script validate the
client's use of the "tus" protocol for object uploads but only one
actually causes a request to be resumed.
chrisd8088 added a commit to chrisd8088/git-lfs that referenced this pull request Mar 1, 2026
In our "lfstest-gitserver" test utility program, the storageHandler()
function is used to generate responses to HTTP requests which upload
or download individual Git LFS objects.  For both types of requests,
if the content of an object matches a specific "magic" string value
then the function simulates a set of conditions relevant for the
current shell test by returning a particular status code or checking
for a particular request header.

When handling a PUT object upload request, the object's SHA-256 hash
value as provided by the Git LFS client in the URL path is used to
determine whether the object content matches a given special or "magic"
key value.  For these types of requests, the storageHandler() function
first looks for an entry in the global oidHandlers map using the provided
OID as the key.  If an entry exists, its value is then checked against a
series of keys, and if a match is found, the PUT upload handler simulates
the relevant test conditions.

By contrast, when handling a GET object download request, the same
storageHandler() function does not refer to the oidHandlers map.
Instead, if it finds a stored object matching the OID provided by the
client, it then tries to match the object's content against each of a
series of special key strings.  Each match check is made by comparing
the length of the string and the length of the byte array containing
the object content, and then if those are equal, comparing the contents
of the string and the byte array.

This technique was first introduced to the object download handling
logic in commit c9c457c of PR git-lfs#1297
and then copied every time another special object content key was added
in later commits and PRs.

While functional, the discrepancy between the object upload and download
handling portions of the storageHandler() function somewhat obscures the
similarities between the two code segments, and has also led to some
confusion as to which special object content keys need to be defined in
the global contentHandlers array, from which the oidHandlers map is
populated.

For instance, in commit 864beb8 of
PR git-lfs#1585 the "storage-download-retry" string was added to the
contentHandlers array, but the corresponding entry in the oidHandlers
array is never utilized.

As we expect to update and correct some of the specialized handling
of object download test conditions in subsequent commits in this PR,
we first revise the storageHandler() function so that it mirrors the
design of the object upload handling section.  When responding to a
GET request, the function now checks for an entry in the oidHandlers
map, and if an entry is found, then uses its value in a "switch"
statement to search for a match against a specific string.

To make this change successful, we also need to register several
additional special object content keys in the contentHandlers array,
specifically the "status-batch-retry", "storage-download-retry-later",
and "storage-download-retry-later-no-header" strings.
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 commits c9c457c and
c8817ff of PR git-lfs#1297 we implemented
a pair of tests to verify that the Git LFS client attempts to resume
interrupted object download transfer requests by retrying the request
and adding 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.

(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.)

To simulate an HTTP response being interrupted, the same commits in
PR git-lfs#1297 updated the storageHandler() function in our "lfstest-gitserver"
test utility program to truncate the data it returned when an object's
content matched specific "magic" strings.  The tests added in the PR
create objects with those special keys as their content, and expect
that downloads of those objects may be interrupted, given certain other
conditions.

When these conditions apply, the storageHandler() function truncates the
data it sends in its responses to GET requests by returning only a slice
from the byte array that contains the object data.

However, the function relies on the "net/http" package from the Go
standard library to generate a Content-Length response header, which
it does based on the length of the data it receives.  As a result,
the Content-Length header values returned to the client with truncated
object data reflect the length of that shortened data and not the
actual Git LFS object size, as they would if a normal object download
request experienced a connection failure.

When the client receives the response with the truncated object data,
the "net/http" package uses the value from the Content-Length header
to determine whether or not the response body has been fully received.
Since the length of the data matches the header's value, the "net/http"
packages terminates the data with a regular EOF marker, and so the
Git LFS client assumes the object has been fully downloaded.  It then
checks the SHA-256 hash value it has computed from the data against the
object's ID, and when these do not match, reports an error and does
not retry the object download request.  The incomplete object data is
retained, however, in a file in the ".git/lfs/incomplete" directory.

If another Git LFS command is run which also attempts to download
the same object, it will locate the temporary file left by the previous
attempt and then make another download request, this time using a
Range header to request the missing object data.

Both of the tests added in PR git-lfs#1297 therefore execute an initial
"git lfs fetch" command, which stops after downloading the first
portion of a test object, and then execute a second "git lfs fetch"
command and check its trace log output to confirm that an HTTP request
with a Range header was sent.  As a consequence, these tests do not
actually verify whether a single Git LFS command will attempt to
resume an interrupted object download request.

We now revise the "lfstest-gitserver" utility to correctly simulate
an interrupted HTTP response when responding to the initial requests
from these tests, which in turn allows us to simplify the tests because
they each need to run just a single "git lfs fetch" command.

The only change we need to make to the "lfstest-gitserver" utility is
in the section of the storageHandler() function which responds to GET
requests.  When one of the two tests' special object content keys is
detected and the other relevant conditions are met, the function
indicates that the response data should be truncated by setting the
"byteLimit" variable to a positive integer.  We therefore insert a
conditional block prior to the output of the response data which checks
for a "byteLimit" value greater than zero, and in that case explicitly
sets the Content-Length header to the full object's size.

We then alter both the "batch storage HTTP download retries with Range
header" and "batch storage HTTP download retries after Range header
rejected" tests in the "t/t-batch-storage-retries.sh" script so they
each only perform a single "git lfs fetch" command and then check
that the command has successfully downloaded a Git LFS object.

In addition to the existing calls to the assert_local_object() test
assertion function we add several grep(1) commands to check for a
number of trace log messages and HTTP headers that we expect the
"git lfs fetch" commands to output.  To capture the HTTP headers we
set the GIT_CURL_VERBOSE environment variable to a value considered
"true" before executing the "git lfs fetch" commands.

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 the first command in each test would return a non-zero
exit code, but this is no longer the case.  (Note that 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.)

As we are simplifying the operation of the tests, we also rewrite their
internal code comments to more accurately explain how the tests operate.
In particular, we correct the comments which stated that the initial
"git lfs fetch" commands reported an error because their object download
requests were interrupted, since that is not technically accurate.
In fact, the commands formerly reported errors because the hashes of
the object data they downloaded did not match the corresponding OIDs,
while the responses to all their HTTP requests were considered complete
and successful due to the values provided in the Content-Length headers.

We also take the opportunity to adjust the log files captured by the
tests 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
redirections and pipelines which generated a log file when running
"git commit" commands, as we expect those commands to succeed and the
tests did not check the "commit.log" files at all.
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.
chrisd8088 added a commit to chrisd8088/git-lfs that referenced this pull request Mar 1, 2026
In PR git-lfs#1303 we updated the Git LFS client to support version 1.0 of
the "tus" protocol, which allows for HTTP upload requests to be resumed
if they are interrupted:

  https://tus.io/protocols/resumable-upload/1-0-x

In the same PR, we also added tests to our shell test suite to verify
that the when the "lfs.tusTransfers" configuration option is set to
a value considered "true", the Git LFS client will use the "tus"
protocol when uploading individual objects over HTTP, and that the
client will retry an interrupted request and send the additional data
necessary to fully upload an object.

These tests are located in the "t/t-resume-tus.sh" script, whose name
follows the pattern established in PR git-lfs#1297 when we created the
"t/t-resume-http-range.sh" shell script.  However, only one of the
two tests in the "t/t-resume-tus.sh" script actually checks whether
an interrupted object upload request can be resumed with the "tus"
protocol.

In a previous commit in this PR we moved the tests from the
"t/t-resume-http-range.sh" script to a new shell script named
"t/t-batch-storage-retries.sh", in order to 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 "tus" protocol.

To keep the names of our test scripts as consistent as possible
we now rename the "t/t-resume-tus.sh" script to
"t/t-batch-storage-upload-tus.sh", which also better reflects the
fact that both tests in the "t/t-resume-tus.sh" script validate the
client's use of the "tus" protocol for object uploads but only one
actually causes a request to be resumed.
chrisd8088 added a commit to chrisd8088/git-lfs that referenced this pull request Apr 1, 2026
In prior commits in this PR we moved all of the tests from our
"t/t-resume-http-range.sh" shell script into a new script named
"t/t-batch-storage-retries.sh", and also renamed the tests to
follow a consistent pattern and better reflect their purpose.

We likewise renamed the "t/t-resume-tus.sh" shell script as
"t/t-batch-storage-upload-tus.sh" and renamed the tests in that
script as well.

All these tests begin in part by invoking our clone_repo() test helper
function and passing the "reponame" shell variable as both the function's
two parameters.  However, some of the tests quote the "reponame" variable
inconsistently in these calls, a discrepancy which was first introduced
with the original versions of the tests in commits
c9c457c and
c8817ff of PR git-lfs#1297, and in
commit 44cf589 of PR git-lfs#1303.

As suggested by larsxschneider on PR review, we now add double quotation
marks around the "reponame" variable in all of the clone_repo() calls
in these functions.

Note that there are other instances of this specific inconsistency in
other scripts in our test suite, but we leave those to be addressed in
a future PR, in order to keep the scope of this PR somewhat constrained.

We also take the opportunity to align the indentation of one comment
in the "batch storage HTTP download retries with Range header" test to
match our standard formatting.
chrisd8088 added a commit to chrisd8088/git-lfs that referenced this pull request Apr 1, 2026
In commits c9c457c and
c8817ff of PR git-lfs#1297 we implemented
a pair of tests to verify that the Git LFS client attempts to resume
interrupted object download transfer requests by retrying the request
and adding a Range header.  The first confirms that the client
correctly handles a successful response with a 206 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.

(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.)

To simulate an HTTP response being interrupted, the same commits in
PR git-lfs#1297 updated the storageHandler() function in our "lfstest-gitserver"
test utility program to truncate the data it returned when an object's
content matched specific "magic" strings.  The tests added in the PR
create objects with those special keys as their content, and expect
that downloads of those objects may be interrupted, given certain other
conditions.

When these conditions apply, the storageHandler() function truncates the
data it sends in its responses to GET requests by returning only a slice
from the byte array that contains the object data.

At present, the truncation performed by the storageHandler() function
in these cases occurs at fixed offsets.  During the "batch storage HTTP
download retries with Range header" test the function returns only the
first 10 bytes of the object, unless a Range header was provided.  The
test then checks that the Git LFS client makes another request with a
Range header with a start position index of 10 bytes, and that the server
responds with a Content-Range header with the same start position index.

In a similar manner, during the "batch storage HTTP download retries
after Range header rejected" test, the storageHandler() function replies
to the first request made without a Range header by truncating its
response after 8 bytes.  It then rejects any requests made with a Range
header, but returns a complete response to subsequent requests made
without Range headers.  The test checks that the client makes one
request with a Range header with a start position index of 8 bytes,
before making another request without a Range header.

In both cases, though, the specific byte offsets are hard-coded in the
"lfstest-gitserver" utility.  This design dates from the original
versions of the tests from PR git-lfs#1297.  We then updated the tests in a
previous commit in this PR to search for these same hard-coded offsets
in the HTTP headers sent and received by the client.

During review of this PR, larsxschneider suggested documenting the source
of these hard-coded start position indexes using code comments.  However,
we can further improve on the original test design by changing the
"lfstest-gitserver" utility to truncate its responses programmatically,
and by changing the tests to expect these truncation points using the
same logic.  Specifically, we update the utility so that it returns the
first half of the object data (after rounding down to the nearest byte)
rather than at a fixed offset of either 8 or 10 bytes.

Next, we update the two affected tests so they calculate the same
midpoint offsets into their object data and then check for these values
in the start position indexes of the Range and Content-Range headers.

Finally, we adjust the comments in both tests to reflect the fact that
they expect the "lfstest-gitserver" test server to interrupt some of
its responses halfway through the object data.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants