Clone fails with LibGit2SharpException: Failed to mmap
Trying to clone a remote repository using LibGit2Sharp v.0.18.1.0 throws this exception:
LibGit2Sharp.LibGit2SharpException: Failed to mmap. Invalid handle value: The volume for a file has been externally altered so that the opened file is no longer valid.
at LibGit2Sharp.Core.Ensure.HandleError(Int32 result) at LibGit2Sharp.Core.Ensure.ZeroResult(Int32 result) at LibGit2Sharp.Core.Proxy.git_clone(String url, String workdir, GitCloneOptions& opts) at LibGit2Sharp.Repository.Clone(String sourceUrl, String workdirPath, CloneOptions options)
Using the same url and workdir, I have successfully cloned the repository both using Git Bash and SourceTree. But for my task I need to do it programatically, and LibGit2Sharp seemed like a good choice.
@Kayak58 Beware that (as stated above) this is not an official fix and the next release of LibGit2Sharp won't contain this workaround, unless we're able to fix it at the libgit2 level.
In order to make this happen, we'd really need a repository against which we could reproduce the issue.
@nulltoken @carlosmn To me, naively, this is starting to look more and more like an issue specifically with Stash. I have just used the following code:
try
{
Repository.Clone("https://github.com/chocolatey/chocolatey.git", @"C:\temp\choco");
Console.Write("Complete");
}
catch (Exception ex)
{
Console.Write(ex);
}
Console.Read();
And I was able to clone the Chocolatey Public Repo.
I have just used the following code to access the first public BitBucket Repository that I could find:
try
{
Repository.Clone("https://bitbucket.org/bennettjessy/samples.git", @"C:\temp\bitbucket");
Console.Write("Complete");
}
catch (Exception ex)
{
Console.Write(ex);
}
Console.Read();
And that also worked.
Both of the above were completed on the same, domain joined, computer that I am using to Clone from Stash.
Do either of you know if there is any sort of configuration on the repository in Stash itself that could be causing this problem?
@gep13 @Kayak58 Could you please test if you can reproduce the issue by duplicating (ie. adding a new remote and then pushing with git.git the whole content (--all --tags)) of the Stash repo toward a private BitBucket/Github repository and fetching it from there with LibGit2Sharp?
If the issue happens again, it might be related to the way the content of the repo. If you can't repro it, then it might rather be a Stash specific issue.
Ah, good idea. Let me try that just now...
So, obviously, the code I am using changed a little bit, but having moved the entire repo using:
git push -u bitbucket --all
Where bitbucket is set up as a remote with the correct url, and then running this code:
try
{
Repository.Clone(
"<url>",
@"C:\temp\bitbucket",
new CloneOptions()
{
Credentials = new UsernamePasswordCredentials()
{
Password = "<password>",
Username = "gep13"
}
});
Console.Write("Complete");
}
catch (Exception ex)
{
Console.Write(ex);
}
Console.Read();
The Clone completed successfully.
@gep13 Wow! Thanks for the amazing response time. That makes me wonder "what happens if we do the opposite?". E.g. Considering a repository that you can currently Clone() with LibGit2Sharp from GitHub, what happens if it gets moved under Stash? Can you still Clone() it with LibGit2Sharp or does it start failing?
Another interesting idea...
Ok, I have just taken the source code of Chocolatey and pushed it to a repo in Stash, using the same commands as above, and when I try to Clone through LibGit2Sharp, I get the same mmap error. :-(
Ok, I have just taken the source code of Chocolatey and pushed it to a repo in Stash, using the same commands as above, and when I try to Clone through LibGit2Sharp, I get the same mmap error. :-(
That's actually a pretty useful outcome! :sparkles:
Thanks a lot for having taken time to help us troubleshoot this further.
So, the issue doesn't look related to the content of a repository, but rather the way it's being served by Stash.
Not a problem at all, happy to help out, as I would really like to see a resolution to this problem.
I concur with your conclusion though. Let me know if there is anything that you want me to check, happy to make some changes to Stash repo to test things out.
Well, it might end up being related to Stash in the end...
I just learnt of some other situation where a Git server will send a bunch of empty data packets, which could cause us to try to map a zero-length file (since we assume that a data packet has data and thus every file truncation will increase the size).
So if Stash is for some reason sending zero-length packets, that would be the source of the issue.
@nulltoken Please confirm if you still need a repository for testing, otherwise I will cancel the request.
@Kayak58 Thanks for the follow up!
Considering what we've seen above, I don't think we still need that repo as we should now be able to reproduce the issue with Chocolatey (for instance) by hosting it on Stash.
While I think about it...
@JakeGinnivan don't you also have a Stash Server that you use? Have you successfully ran the latest version of GitVersion against a repository that has been cloned from it?
If so, do you have any special setup for your Stash Server? Thanks!
Not using GitVersion at work, another team does all the build scripts and we fit into there stuff.
Ah, gothca, no worries. Just a thought.
We found the issue which was causing this. It was indeed the zero-length packets, which Stash is presumably sending as a keep-alive, which made us try to map an empty file.
This has already been fixed in master, so updating the libgit2 version will fix this. I hear @nulltoken will be releasing a fixed version of libgit2sharp shortly.
@carlosmn @nulltoken Thank you both so much for doing this! I really appreciate it!
If you could let me know when the new version of LibGit2Sharp is ready, I can pull together a Pull Request for @simoncropp and GitVersion, assuming he doesn't beat me to it :-)
Thanks again! :+1:
Already fixed in vNext through libgit2/libgit2@bc8a08868
@gep see #798 ;-) Once it's merged, i'll issue a new NuGet package.