Skip to content

Do not fail when multiple processes download the same lfs file - #3813

Merged
bk2204 merged 1 commit into
git-lfs:masterfrom
slonopotamus:download-rename-race-condition
Sep 10, 2019
Merged

bk2204 merged 1 commit into
git-lfs:masterfrom
slonopotamus:download-rename-race-condition

Conversation

@slonopotamus

Copy link
Copy Markdown
Contributor

When several git-lfs processes use the same LFS storage simultaneously,
there's a race condition between checking whether LFS file exists and renaming temporary
file to final name in LFS storage. When rename fails, check that target file exists.
If it does, pretend that download has finished successfully.

Fixes #2825


I'm not sure how to write a test for this.

@slonopotamus

Copy link
Copy Markdown
Contributor Author

cicleci failure is not related to my 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.

I think this looks like a fine fix from a high level perspective, but I'd like to see a slight change in the details.

Comment thread tq/basic_download.go Resolved
When several git-lfs processes use the same LFS storage simultaneously,
there's a race condition between checking whether LFS file exists and renaming temporary
file to final name in LFS storage. When rename fails, check that target file exists.
If it does, pretend that download has finished successfully.

Fixes git-lfs#2825
@slonopotamus
slonopotamus force-pushed the download-rename-race-condition branch from 150412b to 01bc6a6 Compare September 10, 2019 10:05
@slonopotamus

Copy link
Copy Markdown
Contributor Author

Rebased onto fresh master to fix circleci breakage.

@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.

We lack a good way to verify that this is a sharing violation, so I think it's fine to leave this as it is.

@bk2204
bk2204 merged commit 47fe887 into git-lfs:master Sep 10, 2019
@slonopotamus
slonopotamus deleted the download-rename-race-condition branch September 10, 2019 15:50
slonopotamus added a commit to slonopotamus/git-lfs that referenced this pull request Sep 19, 2019
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 added a commit to slonopotamus/git-lfs that referenced this pull request Sep 19, 2019
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 added a commit to slonopotamus/git-lfs that referenced this pull request Sep 23, 2019
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 added a commit to slonopotamus/git-lfs that referenced this pull request Sep 26, 2019
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 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.
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

2 participants