Skip to content

LibGit2Sharp.LockedFileException: The index is locked. This might be due to a concurrent or crashed process #1031

Closed

Description

@michaelnoonan

Wasn't sure if this was an issue or whether we shouldn't be using msbuild /m to enable concurrent builds.

[11:45:48]  [BuildSolution]     17>MSBUILD : warning : WARN [09/02/16 1:45:48:65] Could not determine assembly version: LibGit2Sharp.LockedFileException: The index is locked. This might be due to a concurrent or crashed process [Y:\Work\refs\heads\master\source\Octopus.E2ETests\Octopus.E2ETests.csproj]
[11:45:48]  [BuildSolution] MSBUILD : warning :    at LibGit2Sharp.Core.Ensure.HandleError(Int32 result) [Y:\Work\refs\heads\master\source\Octopus.E2ETests\Octopus.E2ETests.csproj]
[11:45:48]  [BuildSolution] MSBUILD : warning :    at LibGit2Sharp.Core.Proxy.git_checkout_tree(RepositoryHandle repo, ObjectId treeId, GitCheckoutOpts& opts) [Y:\Work\refs\heads\master\source\Octopus.E2ETests\Octopus.E2ETests.csproj]
[11:45:48]  [BuildSolution] MSBUILD : warning :    at LibGit2Sharp.Repository.CheckoutTree(Tree tree, IList`1 paths, IConvertableToGitCheckoutOpts opts) [Y:\Work\refs\heads\master\source\Octopus.E2ETests\Octopus.E2ETests.csproj]
[11:45:48]  [BuildSolution] MSBUILD : warning :    at LibGit2Sharp.Repository.Checkout(Tree tree, CheckoutOptions checkoutOptions, String refLogHeadSpec) [Y:\Work\refs\heads\master\source\Octopus.E2ETests\Octopus.E2ETests.csproj]
[11:45:48]  [BuildSolution] MSBUILD : warning :    at LibGit2Sharp.Repository.Checkout(Branch branch, CheckoutOptions options) [Y:\Work\refs\heads\master\source\Octopus.E2ETests\Octopus.E2ETests.csproj]
[11:45:48]  [BuildSolution] MSBUILD : warning :    at LibGit2Sharp.Repository.Checkout(String committishOrBranchSpec, CheckoutOptions options) [Y:\Work\refs\heads\master\source\Octopus.E2ETests\Octopus.E2ETests.csproj]
[11:45:48]  [BuildSolution] MSBUILD : warning :    at GitTools.Git.GitRepositoryHelper.EnsureLocalBranchExistsForCurrentBranch(Repository repo, String currentBranch) [Y:\Work\refs\heads\master\source\Octopus.E2ETests\Octopus.E2ETests.csproj]
[11:45:48]  [BuildSolution] MSBUILD : warning :    at GitTools.Git.GitRepositoryHelper.NormalizeGitDirectory(String gitDirectory, AuthenticationInfo authentication, Boolean noFetch, String currentBranch) [Y:\Work\refs\heads\master\source\Octopus.E2ETests\Octopus.E2ETests.csproj]
[11:45:48]  [BuildSolution] MSBUILD : warning :    at GitVersion.GitPreparer.Initialise(Boolean normaliseGitDirectory, String currentBranch) [Y:\Work\refs\heads\master\source\Octopus.E2ETests\Octopus.E2ETests.csproj]
[11:45:48]  [BuildSolution] MSBUILD : warning :    at GitVersion.ExecuteCore.ExecuteGitVersion(String targetUrl, String dynamicRepositoryLocation, Authentication authentication, String targetBranch, Boolean noFetch, String workingDirectory, String commitId, Config overrideConfig) [Y:\Work\refs\heads\master\source\Octopus.E2ETests\Octopus.E2ETests.csproj]
[11:45:48]  [BuildSolution] MSBUILD : warning :    at GitVersion.ExecuteCore.TryGetVersion(String directory, VersionVariables& versionVariables, Boolean noFetch, Authentication authentication) [Y:\Work\refs\heads\master\source\Octopus.E2ETests\Octopus.E2ETests.csproj]

This occurs on TeamCity 10.0.0 building ~30 projects. We've only just upgraded to GitVersionTask.3.6.3 from GitVersionTask.3.6.1 which had the gitversion_cache issue.

In the meantime I'll try some more builds and see if it recurs frequently.

Activity

  1. michaelnoonan commented on Sep 2, 2016

    @michaelnoonan
    ContributorAuthor

    The very next build through TeamCity failed with the exact same message and stack trace.

    I'm going to revert to msbuild without the /m for now.

  2. JakeGinnivan commented on Sep 3, 2016

    @JakeGinnivan
    Contributor

    Id switch to the exe, run it with /updateAssemblyInfo before building.

    We would have to grab a mutex, and block builds until the first task has finished calculating the version then unblock the others and they get the version from the cache.

  3. asbjornu commented on Sep 5, 2016

    @asbjornu
    Member

    Perhaps we could use .git/index.lock for this? I believe it would be semantically correct to lock the entire Git repository for all and any change meanwhile we calculate a version. If .git/index.lock exists, we could await version calculation until it's deleted or within a configurable time-out.

  4. JakeGinnivan commented on Sep 5, 2016

    @JakeGinnivan
    Contributor

    Im happy with that

  5. bbowman commented on Mar 10, 2017

    @bbowman

    any updates here?

  6. asbjornu commented on Mar 13, 2017

    @asbjornu
    Member

    @bbowman: I'm sorry, but nothing has happened besides agreeing on a solution that is yet to be implemented. We need someone in the team to have time to implement this, or get someone else to submit a PR. I unfortunately have my hands full. Perhaps you or @michaelnoonan can submit a PR?

  7. jnevins-gcm commented on Aug 16, 2018

    @jnevins-gcm

    any update on this? builds take 4x as long without parallelism :(

  8. asbjornu commented on Aug 16, 2018

    @asbjornu
    Member

    @jnevins-gcm: Sorry, there's nothing new to report on this. If you are able to provide a pull request that fixes this as described in #1031 (comment), it will be merged quickly provided it passes all tests.

  9. jnevins-gcm commented on Aug 16, 2018

    @jnevins-gcm

    Thanks for the update. Even if I fix, how would I manage to get that into GitVersionTask?

    Is there any short term workaround I could do with retries?

  10. asbjornu commented on Jun 27, 2019

    @asbjornu
    Member

    @jnevins-gcm, if you submit a pull request to this repository, it will be merged and a new package of GitVersionTask will show up on NuGet that you can use.

  11. asbjornu commented on Jun 28, 2019

    @asbjornu
    Member

    Now that we've upgraded to LibGit2Sharp version 0.26 in #1713, I hope this problem might be fixed. Please test with the latest v5 beta and reopen if the problem persists.

  12. StevenDevooght commented on Jul 16, 2019

    @StevenDevooght

    Hi,

    We are having the same issue using the latest beta version (5.0.0-beta5.11).

  13. borrillis commented on Sep 4, 2019

    @borrillis

    Also hitting this issue on linux and Windows (but not MacOS) build agents using GitVersion 5.0.1

  14. asbjornu commented on Sep 4, 2019

    @asbjornu
    Member

    @StevenDevooght and @borrillis, can you please tell me more about your build environments that might help us figure out why this is happening?

  15. 61 remaining items

  16. teneko commented on Jun 10, 2020

    @teneko
    Contributor

    Thanks for your answering. When I find time, I will try to look around the code in GitVersionCode and seek for a possiblity to wrap each single action that involves GitVersion.yml, caching or LibGit2Sharp by an file lock. Another approach would be to lock on startup and unlock on exit. But that is quick and dirty and reminds me of the time, where you instantiated one SqlConnection during lifetime. 😃
    Yes would become a breaking change if someone externals is relying on exact these created caches. But not sure if someone is really working with those temporary cach files that are generated by GitVersion and not by someone elses. I won't change any structure.
    I think I have now all infos to proceed when I find time. I hope for weekend or next week.
    One question, what lets me open, is a test case: It is quite difficult to create such an environment to test it. My project with its 10+ sub projects that relies on GitVersionTask could serve that kind of test, but it does fail only each second or third time due to LibGit2Sharp.LockedFileException.
    What is your idea to make a test case? Maybe a kind of penetration test of a huge project with many sub projects someone can provide us?

    Regarding to your distate of Tool and Utility. I can understand you and agree with you, that such a Tool whatever static prupose it does serve, can be lost quickly. But a library is not a library without such functions. Those functions cannot be related to only one purpose easily, So when you have two classes that serves completely its own purposes, but have both one method that share a bit of the same logic like Directory.Exists or File.Exists, then you should outsource them definetely to let them become Directory.Exists and File.Exists. My taste for Tools (or Utilities) are coming for this reason, that I don't want to collide with existing classes from System.*, but want to state its relation to them. So the name LockFileTools comes more or less by habit. When I think more over it: FileLocker does associate a kind of encapsulation between these static functions, so even when I would call it FileLocker: The class itself does not provide a file locker, but each of its static functions. Moreover they do not provide a file locker instead they do lock a file it without an instanstiated context so the name would irritate somehow. But this would change if FileLocker would be instantiable and would require a file path and provides methods like "Lock", then the class itself would be a file locker and therefore named FileLocker. Haha, but I do respect your programming style. ☺️

    To your last words: I am looking forward that this library will become a strong library. 😊

  17. asbjornu commented on Jun 10, 2020

    @asbjornu
    Member

    Thanks for your answering. When I find time, I will try to look around the code in GitVersionCode and seek for a possiblity to wrap each single action that involves GitVersion.yml, caching or LibGit2Sharp by an file lock.

    Sounds great! 👍

    Another approach would be to lock on startup and unlock on exit. But that is quick and dirty and reminds me of the time, where you instantiated one SqlConnection during lifetime. 😃

    It's a viable choice if other options are too difficult. It's much better than crashing and will still yield better performance with parallelization than a serial build would.

    Yes would become a breaking change if someone externals is relying on exact these created caches. But not sure if someone is really working with those temporary cach files that are generated by GitVersion and not by someone elses.

    I don't know either.

    I won't change any structure.

    Good. We can instead harmonize this a bit in v6 and gather everything GitVersion related into a .git/gitversion/ folder.

    I think I have now all infos to proceed when I find time. I hope for weekend or next week.

    Awesome!

    What is your idea to make a test case? Maybe a kind of penetration test of a huge project with many sub projects someone can provide us?

    I think we should be able to surface this bug with enough projects in a single solution, so a very simple repository with just a bare minimum solution with perhaps 100 projects all using GitVersionTask should be able to consistently reproduce this bug, I'd assume.

    I've set up and invited you to GitVersion.TestCases so we can collaborate on the reproduction there. I'm thinking this one repository can have one branch per test case, starting with this parallelization problem.

    But this would change if FileLocker would be instantiable and would require a file path and provides methods like "Lock", then the class itself would be a file locker and therefore named FileLocker.

    My point exactly! I have a general distaste for static as well, since unless it's always paired with readonly and immutable objects, leads to global mutable state, meaning bugs and bad encapsulation. It's also impossible to mock in tests and generally doesn't play well IOC.

    To your last words: I am looking forward that this library will become a strong library. 😊

    So do I! 😃

  18. dmitrynovik commented on Sep 17, 2020

    @dmitrynovik

    Guys, the builds failing because of GitVersion creating *.lock files is a really annoying "feature". Any indication on when the PR "A lock file implementation applied to project GitVersionCore for resolving #1031" can be merged ? Looks like there was a work in progress abandoned in July :(
    "

  19. MarkusHorstmann commented on Feb 5, 2021

    @MarkusHorstmann
    Contributor

    Pending PR #2581 has some mitigations (retries) for this issue. Makes a big difference at least for my builds.

  20. added this to the 5.6.5 milestone on Feb 6, 2021
  21. github-actions commented on Feb 7, 2021

    @github-actions

    🎉 This issue has been resolved in version 5.6.5 🎉
    The release is available on:

    Your GitReleaseManager bot 📦🚀

  22. bording commented on Apr 16, 2021

    @bording
    Contributor

    @arturcic I don't think this issue should be closed. I'm currently using GitVersion.MsBuild 5.6.8 and I'm still seeing LockedFileException and NameConflictException when projects are built in parallel. #1381, which is still open seems to also be relevant here.

    The problem is that there is a race condition in the NormalizeGitDirectory where two projects building at the same time will both attempt to create the needed branches, and one will fail with LockedFileException or NameConflictException.

    Seems like this code needs to be more resilient to failures, because if it just attempted it again, it would see that the required branch does exist.

    EDIT: Instead of a retry, seems like a mutex would be a good way to ensure that the normalization code is not being run in parallel since that's the part that needs to be protected.

  23. dominikjeske commented on Apr 19, 2021

    @dominikjeske

    Unfortunately I'm getting this error also using GitVersion.MsBuild 5.6.8 on my TeamCity 2020.1.3.

    ERROR [04/19/21 11:53:11:56] An unexpected error occurred:
    11:53:11
          LibGit2Sharp.LockedFileException: failed to create locked file 'C:/BuildAgent/work/bb00531d049a7710/.git/refs/heads/pull-requests/716/from.lock': The file exists. 
    
  24. bording commented on Apr 21, 2021

    @bording
    Contributor

    I have opened #2669 with my attempt to fix this problem.

  25. rose-a commented on May 26, 2021

    @rose-a
    Contributor

    Yup, issue is still there with v5.6.9 (GitLab CI)

  26. bording commented on May 26, 2021

    @bording
    Contributor

    @rose-a I had a follow-up PR #2676 that has not been released yet. That will be needed to truly fix the issue.

    @arturcic Any plans to get that out?

  27. arturcic commented on May 26, 2021

    @arturcic
    Member

    @bording i will have a look on the PRs in progress and see when we can release

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

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions