Skip to content

More robust handling of parallel attempts to download the same file - #3826

Merged
bk2204 merged 2 commits into
git-lfs:masterfrom
slonopotamus:parallel-download-v2
Sep 26, 2019
Merged

bk2204 merged 2 commits into
git-lfs:masterfrom
slonopotamus:parallel-download-v2

Conversation

@slonopotamus

Copy link
Copy Markdown
Contributor
  1. git-lfs now only writes to unique temp files created with ioutil.TempFile
    that are open with O_CREATE|O_EXCL
  2. Partially-downloaded file is now atomically borrowed and returned back via os.Rename
  3. .part <-> .tmp and .tmp -> final renames are allowed to fail and are handled appropriately

This is a continuation of #3813
Fixes #2825

There are several error codepaths where we borrow .part file but remove it instead of returning back.
I believe that it is OK and in those erroneous cases it is better to restart download from scratch
instead of attempting to use possibly-corrupt .part file.

Comment thread tq/basic_download.go Resolved
@slonopotamus

Copy link
Copy Markdown
Contributor Author

I believe there's something broken with Windows CI. Reported as #3828.

@bk2204

bk2204 commented Sep 19, 2019

Copy link
Copy Markdown
Member

I appreciate the patch, and I'll take a look at this Monday.

@slonopotamus

slonopotamus commented Sep 19, 2019 •

Copy link
Copy Markdown
Contributor Author

We've started testing this change internally in Mail.Ru today, I'll follow up with updates if we encounter any issues.

Our usecase is Jenkins nodes with shared lfs.storage across 8 git checkouts on each and who run git pull simultaneously in all working copies when new commit is pushed into git repository and Jenkins runs a new massively-parallel build against it, so we hit race issues several times a day.

@slonopotamus

Copy link
Copy Markdown
Contributor Author

Forgot to say: this PR + #3813 is the only Go code I wrote in my entire life as of today. So if you see that I do not follow Go code standards/best practices - feel free to ask for changes.

@bk2204 bk2204 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 like a great start. There are a few things I noticed with respect to permissions and Windows support, but overall I'm excited to see how this ends up.

Comment thread tq/basic_download.go Resolved
Comment thread tq/basic_download.go Resolved
Comment thread tq/basic_download.go Resolved

@bk2204 bk2204 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 good. Thanks again for the change.

I will ask you to rebase this on master once the Windows CI fix gets merged just to ensure that this does indeed pass.

@slonopotamus

Copy link
Copy Markdown
Contributor Author

Okay. Please, @mention me when that happens.

1. git-lfs now only writes to unique temp files created with `ioutil.TempFile`
   that are open with `O_CREATE|O_EXCL`
2. Partially-downloaded file is now atomically borrowed and returned back via `os.Rename`
3. `.part <-> .tmp` and `.tmp -> final` renames are allowed to fail and are handled appropriately

This is a continuation of git-lfs#3813
Fixes git-lfs#2825

There are several error codepaths where we borrow .part file but remove it instead of returning back.
I believe that it is OK and in those erroneous cases it is better to restart download from scratch
instead of attempting to use possibly-corrupt .part file.
@slonopotamus

Copy link
Copy Markdown
Contributor Author

Rebased onto latest master branch to fix Windows CI.

@bk2204
bk2204 merged commit d29e5a8 into git-lfs:master Sep 26, 2019
@slonopotamus
slonopotamus deleted the parallel-download-v2 branch September 26, 2019 20:03
slonopotamus added a commit to slonopotamus/git-lfs that referenced this pull request Oct 23, 2019
On Windows, there is no way to replace file atomically. Instead, MoveFileExA:

1. Calls CreateFileA(access=Delete, shareMode=Delete)
2. Calls SetRenameInformationFile to rename file
3. Calls CloseFile

The problem is that if parallel process attempts to read destination file between
steps 2 and 3, it will try to do that without shareMode=Delete and will hit a SHARING_VIOLATION error.

There are two solutions here:
1. Do not overwrite file if it already exists
2. Retry reading from file if got a SHARING_VIOLATION
3. Do not use Windows

This commit implements option 1.

Fixes git-lfs#2825 (again). Also see git-lfs#3813 and git-lfs#3826.
slonopotamus added a commit to slonopotamus/git-lfs that referenced this pull request Oct 23, 2019
On Windows, there is no way to replace file atomically. Instead, MoveFileExA:

1. Calls CreateFileA(access=Delete, shareMode=Delete)
2. Calls SetRenameInformationFile to rename file
3. Calls CloseFile

The problem is that if parallel process attempts to read destination file between
steps 2 and 3, it will try to do that without shareMode=Delete and will hit a SHARING_VIOLATION error.

There are two solutions here:
1. Do not overwrite file if it already exists
2. Retry reading from file if got a SHARING_VIOLATION
3. Do not use Windows

This commit implements option 1.

Fixes git-lfs#2825 (again). Also see git-lfs#3813 and git-lfs#3826.
slonopotamus added a commit to slonopotamus/git-lfs that referenced this pull request Oct 23, 2019
On Windows, there is no way to replace file atomically. Instead, MoveFileExA:

1. Calls CreateFileA(access=Delete, shareMode=Delete)
2. Calls SetRenameInformationFile to rename file
3. Calls CloseFile

The problem is that if parallel process attempts to read destination file between
steps 2 and 3, it will try to do that without shareMode=Delete and will hit a SHARING_VIOLATION error.

There are two solutions here:
1. Do not overwrite file if it already exists
2. Retry reading from file if got a SHARING_VIOLATION
3. Do not use Windows

This commit implements option 1.

Fixes git-lfs#2825 (again). Also see git-lfs#3813 and git-lfs#3826.
slonopotamus added a commit to slonopotamus/git-lfs that referenced this pull request Oct 23, 2019
On Windows, there is no way to replace file atomically. Instead, MoveFileExA:

1. Calls CreateFileA(access=Delete, shareMode=Delete)
2. Calls SetRenameInformationFile to rename file
3. Calls CloseFile

The problem is that if parallel process attempts to read destination file between
steps 2 and 3, it will try to do that without shareMode=Delete and will hit a SHARING_VIOLATION error.

There are two solutions here:
1. Do not overwrite file if it already exists
2. Retry reading from file if got a SHARING_VIOLATION
3. Do not use Windows

This commit implements option 1.

Fixes git-lfs#2825 (again). Also see git-lfs#3813 and git-lfs#3826.
slonopotamus added a commit to slonopotamus/git-lfs that referenced this pull request Oct 25, 2019
…y on Windows

On Windows, there is no way to replace file atomically. Instead, MoveFileExA:

1. Calls CreateFileA(access=Delete, shareMode=Delete)
2. Calls SetRenameInformationFile to rename file
3. Calls CloseFile

The problem is that if parallel process attempts to open destination file for reading between
steps 2 and 3, it will try to do that without shareMode=Delete and will hit a SHARING_VIOLATION error.

In practice, this race condition results in:

  Smudge error: Error opening media file.: open .git\lfs\objects\<sha>: The process cannot access the file because it is being used by another process.

There are two solutions here:

 1. Do not overwrite file if it already exists
 2. Retry reading from file if got a SHARING_VIOLATION

This commit implements option 1.

Fixes git-lfs#2825 (for Windows, other OSes are race-free already). Also see git-lfs#3813 and git-lfs#3826.
slonopotamus added a commit to slonopotamus/git-lfs that referenced this pull request Oct 25, 2019
…y on Windows

On Windows, there is no way to replace file atomically. Instead, MoveFileExA:

1. Calls CreateFileA(access=Delete, shareMode=Delete)
2. Calls SetRenameInformationFile to rename file
3. Calls CloseFile

The problem is that if parallel process attempts to open destination file for reading between
steps 2 and 3, it will try to do that without shareMode=Delete and will hit a SHARING_VIOLATION error.

In practice, this race condition results in:

  Smudge error: Error opening media file.: open .git\lfs\objects\<sha>: The process cannot access the file because it is being used by another process.

There are two solutions here:

 1. Do not overwrite file if it already exists
 2. Retry reading from file if got a SHARING_VIOLATION

This commit implements option 1.

Fixes git-lfs#2825 (for Windows, other OSes are race-free already). Also see git-lfs#3813 and git-lfs#3826.
slonopotamus added a commit to slonopotamus/git-lfs that referenced this pull request Oct 25, 2019
…y on Windows

On Windows, there is no way to replace file atomically. Instead, MoveFileExA:

1. Calls CreateFileA(access=Delete, shareMode=Delete)
2. Calls SetRenameInformationFile to rename file
3. Calls CloseFile

The problem is that if parallel process attempts to open destination file for reading between
steps 2 and 3, it will try to do that without shareMode=Delete and will hit a SHARING_VIOLATION error.

In practice, this race condition results in:

  Smudge error: Error opening media file.: open .git\lfs\objects\<sha>: The process cannot access the file because it is being used by another process.

There are two solutions here:

 1. Do not overwrite file if it already exists
 2. Retry reading from file if got a SHARING_VIOLATION

This commit implements option 1.

Fixes git-lfs#2825 (for Windows, other OSes are race-free already). Also see git-lfs#3813 and git-lfs#3826.
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 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.
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.

shared lfs.storage is not threadsafe

4 participants