Skip to content

shared lfs.storage is not threadsafe #2825

Closed

Description

@yodovicz

When multiple same modules share one lfs storage (by git config lfs.storage=STORAGE). Access from multiple instances with command 'git lfs fetch' fails. But it's expected to shared the storage.

Activity

  1. ttaylorr commented on Jan 20, 2018

    @ttaylorr
    Contributor

    Access from multiple instances with command 'git lfs fetch' fails.

    Can you clarify the issue here? It is my understanding that if two processes (A) and (B) are using git-lfs-fetch(1) concurrently with a shared lfs.storage configuration, that they will both download the same large file, thus wasting energy?

    If so, I think that this can be solved with a lock file in the same way that Git prevents multiple writes to the index at once. Something like:

    f, err := os.Create(".git/lfs/lock", os.O_CREATE | os.O_EXCL)
    if err != nil {
            // Abort, failed to acquire lock.
    }
    defer os.Remove(f)
    
    // Operation involving storage ... 

    Is this the issue that you're experiencing? If so, I think that this would be a wonderful contribution to LFS, and is something that I'd be happy to help someone out with.

  2. yodovicz commented on Jan 22, 2018

    @yodovicz
    Author

    The problem is, that git-lfs.exe is called from git.exe and it's not easy to recognize which git operation will invoke git-lfs. Locking is the solution, but every operation on repository must be locked because of lfs shared storage. This slows down the parallel checkout (or other operation).
    For example:
    process (A) and (B). I have repository (X) and (Y), these repos contains submodules (K), (L) and (M), where
    (X)->(K), (L)
    (Y)->(K), (L), (M)
    parallel execution of git recursive fetch (could be reset(invoke git-lfs fetch), checkout):
    (A) git fetch -> (X)
    (B) git fetch -> (Y)
    is ok, because thay have no shared storage and recursive submodules has it's own storage space. parallel processing is valid without any sharing violation.
    But when I add shared lfs storage, I get multiple sharing violation errors. Locking is safe but i need to lock every git operation, which slows down all git operations (git-lfs fetch is invoked by git and i'm not able to decide if I need to lock the shared storage or not, it's possible to decide, but it's not so eazy).
    If there is shared storage available in git-lfs, is there possibility to add some locking (via lfs config) to avoid sis sharing violation.

    This is and simple example, I have more than 30 repos with multiple list of submodules (the same) processed in parallel.

    I tried to not use lfs storage, but git-lfs.exe deadock when used multiple instances at the same repo (same URL, different space). That's the reason I decide to use lfs.storage, but the result is sharing violation.

  3. ttaylorr commented on Jan 31, 2018

    @ttaylorr
    Contributor

    But when I add shared lfs storage, I get multiple sharing violation errors. Locking is safe but i need to lock every git operation, which slows down all git operations (git-lfs fetch is invoked by git and i'm not able to decide if I need to lock the shared storage or not, it's possible to decide, but it's not so eazy).

    If there is shared storage available in git-lfs, is there possibility to add some locking (via lfs config) to avoid sis sharing violation.

    Is the idea something like git config --bool lfs.lockstorage true to enable locking as an opt-in operation to not prevent slow-downs in other areas of operation?

    I think that something like this would work and solve your problem, but I'm not sure if we should push forward in implementing this, since users can easily opt out (git config --bool lfs.lockstorage false) and the race condition re-appears.

  4. yodovicz commented on Feb 1, 2018

    @yodovicz
    Author

    Yes, git config --bool lfs.lockstorage true could be the slolution. Because now there is no way to do that.
    If someone sets lfs.lockstorage false together with lfs.storage, he should know what he is doing (the false value could be default).

  5. added a commit that references this issue on Sep 9, 2019
    150412b
  6. slonopotamus commented on Sep 9, 2019

    @slonopotamus
    Contributor

    I am strongly against locking approach, because it will

    1. Force serial access even in cases where nothing needed to be downloaded
    2. Will definitely produce situations when lock file got stuck because git-lfs was hard-killed or something like that

    Instead, my suggestion is: if git-lfs suddenly detected that file that it was downloading is already downloaded, just silently pretend that everything is OK. See #3813 that attempts to implement this strategy.

  7. added a commit that references this issue on Sep 10, 2019
    01bc6a6
  8. slonopotamus commented on Sep 18, 2019

    @slonopotamus
    Contributor

    My fix in #3813 is a good thing, but not enough.

    There's one more race: for incomplete files, git-lfs uses deterministic file names (actually, just object hash). So, it is possible for os.Rename to fail because source file is currently open by concurrent process.

    @bk2204 Please, reopen this issue. I'm working on a new PR.

  9. slonopotamus commented on Sep 18, 2019

    @slonopotamus
    Contributor

    A (somewhat) full problem analysis:

    1. There's a race between os.OpenFile(..., O_RDWR, ...) and os.OpenFile(..., os.O_CREATE|os.O_WRONLY|os.O_EXCL, ...) in basicDownloadAdapter.checkResumeDownload on any OS.
    2. Multiple processes can os.OpenFile(..., O_RDWR, ...) in basicDownloadAdapter.checkResumeDownload, it is not exclusive and doesn't lock anything except moving/deleting file on Windows, even though comment in code says the different thing. This in fact happens to us, multiple processes open the same temp file and simultaneously write to it. On Windows, all git-lfs processes except the last one fail because they cannot rename temp file. On POSIX systems, all git-lfs processes except the first one failed before Do not fail when multiple processes download the same lfs file #3813. After Do not fail when multiple processes download the same lfs file #3813, POSIX systems no longer fail here but Windows still does.
    3. os.Remove calls in basicDownloadAdapter.download are not checked for success. On Windows, it means that file might not be deleted, but later opened with os.O_WRONLY|os.O_CREATE|os.O_TRUNC. I'm not sure how O_TRUNK operates when other process already has the same file in write mode.
    4. There's a race between file closing after successfull download in basicDownloadAdapter.download and tools.RenameFileCopyPermissions, Do not fail when multiple processes download the same lfs file #3813 covers it.
    5. There are races inside tools.RenameFileCopyPermissions, Do not fail when multiple processes download the same lfs file #3813 covers them.

    Thoughts:

    1. If there was no download resuming, fixing all this stuff would be trivial: just download file under random temp name then move. If move failed but target file exists, pretend everything is fine (this is what Do not fail when multiple processes download the same lfs file #3813 did). But removal of download resuming is not an option, right? @bk2204
    2. With resuming of downloads, things become now we need to share half-downloaded file somehow.
      2.1. Cheap and temporary solution: add config option that allows to opt-out from resuming of downloads but fixes concurrency.
      2.2. Use smth like gofrs/flock to make sure that only a single process operates on a file
      2.3. Guard operations with half-downloaded files with os.Rename: writes only happen when file has random temp name. At the start of download, an attempt to os.Rename("<sha>.tmp", "<random>") is made. If rename is successful, we've successfully stole partially downloaded file and can resume download. If rename isn't successful, we download the whole file. If we fail in the middle of download (broken network, CTRL+C, etc), we os.Rename("<random>", "<sha>.tmp") so we can resume download next time. In case of hard fail (SIGKILL, poweroff) we won't be able to resume download, though this doesn't look like a big problem. To be honest, I haven't heard of any software that works this way.
      2.4. Introduce a huge lock file (like .git/index.lock) that will guard all write operations to LFS storage. Ideally, this lock should be even more complex because in git-lfs there are three types of operations - read, fetch and prune. However, like with .git/index.lock, such thing will cause pains because it will from time to time get stuck.

    I also want to note that this issue is not specific to shared lfs.storage across several working copies. It is also possible to trigger multiple git-lfs processes working on the same file even with a single working copy, for example by running several git show <commit-ish>:<path/to/lfs/file> at the same time

  10. reopened this on Sep 18, 2019
  11. bk2204 commented on Sep 18, 2019

    @bk2204
    Member

    Removal of download resuming is not an option. Folks in many parts of the world have unreliable connections, and even people who usually have good connections sometimes get on airplanes where the WiFi is bad.

    We definitely need some sort of locking here. Unfortunately, flock is not a viable choice because it doesn't work on NFS, and I'm sure we have folks using NFS. That means that we need to use fcntl locking or dotlocking (using the algorithm specified in Linux's open(2) manpage under O_EXCL) and, if we're using fcntl locking, avoid opening multiple file descriptors to the same file. I have no clue which if any of these will work on Windows, though.

    Adopting the same algorithm Git uses when writing refs for use when we're writing objects sounds like a reasonable plan; we know it's generally reliable and we know it works on NFS. Since we may not want to rewrite the object to the side and rename over it, it sounds like this is describing option 2.3, which could be reasonable.

  12. added 4 commits that reference this issue on Sep 19, 2019
    715246c
    8722912
    ac195f0
    1edb976
  13. added 7 commits that reference this issue on Oct 23, 2019
    a06e30c
    ab01dd5
    f3fbcec
    be45a43
    c0c0152
    14e034f
    0c8edfc
  14. slonopotamus commented on Oct 25, 2019

    @slonopotamus
    Contributor

    #3880 got just merged. Hopefully, this ends the story of issues with parallel usage of LFS storage.

    UPD: no, it didn't. But #3890 did.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions