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.
This is also being discussed here, in relation to GitVersion:
https://github.com/Particular/GitVersion/issues/246
@Kayak58 Thanks a lot for this report. For later reference, it looks like that both you and @gep13 encountered a similar issue working against a repo hosted in Stash.
From the original SO question, you might be able to provide us with a test case. That would be amazing if that could be done, as it would really boost the troubleshooting process.
@nulltoken I have forwarded your request to the owners of the repo I'm trying to clone.
working against a repo hosted in Stash.
An issue like this shouldn't have anything to do with the hoster (or public repo) since the error comes from Windows and is about system calls. There should be some other common setup. Are you cloning/fetching into a remote/network filesystem? (Or do you know if TeamCity has some odd filesystem setup?)
The most likely change to have introduced mmap issues whilst cloning would be libgit2/libgit2#2352 which was introduced to work around oddities in the Windows remote filesystem handling. You should be able to revert those changes without any loss of functionality unless you're fetching into a remote filesystem.
If people don't like compile, @nulltoken maybe you can prepare a libgit2sharp package with a libgit2 version with that PR reverted? (reverting on top of whatever you have in the latest release should work).
The Windows mmap equivalent also returns ERROR_FILE_INVALID if we try to map a zero-length file, but we guard against this by truncating the file to the target size before writing to it. I don't know if there's some process/service that would undo this before we go in and map.
@carlosmn In terms of my setup, I am cloning (within TeamCity) to a local fileshare on the build agent, not into anything remote. I am not aware of any odd filesystem setup being used by TeamCity. I can browse directly to the folders on the filesystem, when the clone does work.
When you say I could modify the changes in https://github.com/libgit2/libgit2/pull/2352/files, I honestly wouldn't know where to start :-) I have never done any C/C++ programming, and wouldn't know how to read the code, let alone compile it :-) So if it were possible that someone, could create a DLL for testing purposes, I would happily compile this into GitVersion for testing.
@carlosmn The repository I am trying to clone is on a remote file system and I have no knowledge about it. Unfortunately the guy in charge at the remote location is on vacation until Monday. But as I said, I am able to clone the repo from both Git Bash and SourceTree.
Okay, slight update on a test that I just completed. This is what I have done:
- Opened Visual Studio
- Created new console application
- Ran the following command in "Package Manager Console"
Install-Package libgit2sharp -version 0.17.0.0
- The I added the following to the Main method of the console application
Repository.Clone("http://<server name>/test.git", @"C:\temp\test");
- And this worked correctly. The end result was a local clone of the Repo hosted in Stash on my local file system
I then did the following
- Ran the following at the Package Manager Console
Install-Package libgit2sharp -version 0.18.0.0
- Re-ran the application
- I then got this exception:

So I am in a position to re-produce the error.
Not sure if this helps in any way, but at least it is reproducible outwith GitVersion (which is where my issue lies) and also narrows down the problem to changes completed between 0.17.0.0 and 0.18.0.0.
Please let me know if there is anything else I can try in order to help progress this further. Happy to also do some form of remote session i.e. Skype, Google Hangout, etc, if that would help.
So if it were possible that someone, could create a DLL for testing purposes, I would happily compile this into GitVersion for testing.
@gep13 Thanks a lot for the help. I'll work on building a customized set of libgit2 binaries without the libgit2/libgit2#2352 changes. I hope to have them ready for tomorrow. If I don't mess up anything, they should be a drop-in replacement for those in your v0.18.1 package subfolder structure.
@nulltoken thank you very much! :+1:
@gep13 Done.
Download the following file. Change the ".jpg" extension in ".7z". Voilà!
https://cloud.githubusercontent.com/assets/92363/3981409/efec63e4-286d-11e4-9d12-e07cc2c67c08.jpg
Both the libgit2 binaries and the LibGit2Sharp assembly have to be replaced (as the later contains a reference to the first ones).
Note: Hmmm. I think I've slightly abused the image uploading system :blush:
Thanks for this! :-)
However, I have just tried to replace the files, and I am still getting the same error :-(
This is what I did:
- Download the jpg
- Unblock the file
- Renamed to 7z
- Extracted file using 7zip
- Went to packages folder of my temp project and found
packages\LibGit2Sharp.0.18.1.0\lib\net40 - Deleted the dll and xml file at this location
- Copied in dll and xml from extracted 7z file
- Went to
NativeBinaries\amd64folder in packages folder and deleted all files - Copied equivalent files from 7z file
- Went to
NativeBinaries\x86folder in packages folder and deleted all files - Copied equivalent files from 7z file
- Back to Visual Studio, rebuild
- Met with the same error as before
Any ideas? Did I miss something?
Thanks!
P.S. Regarding the abuse, I won't tell anyone if you don't :+1:
Ok, scratch all of the above. I have just closed and re-opened Visual Studio, and tried again, and now it seems to be working happily with the modified version of the dll that you provided.
I will need to do a further test with including the dll in the GitVersion project to see if I can get it to work there as well.
If it does, where does that leave this issue?
now it seems to be working happily with the modified version of the dll that you provided
Awesome! So libgit2/libgit2#2352 was indeed the root of the regression. Thanks @carlosmn for the hint.
If it does, where does that leave this issue?
First of all, please note that the provided custom build should be used with caution. Although it seems to be working, the libgit2 version it's been built against doesn't officially exist per se.
Next steps would be along something like:
- Either libgit2/libgit2#2352 is reverted in libgit2 and a new LibGit2Sharp release will shortly follow
- Or it is fixed. Which may take more time without a proper repro case.
/cc @vmg @phkelley @ethomson
@gep13 That's an interesting question. Those commits were made in order to support Windows. Removing them would swap out a working setup for another.
It's made tricker by not yet having a definite reason as to why this is failing on your machine but libgit2sharp's tests themselves don't fail, which are also run on TC.
I don't quite get what you meant above when you said you had a "local fileshare" rather than anything remote. Doesn't "fileshare" imply that the files/volume are hosted on a different computer and mounted via SMB?
FWIW none of the LibGit2Sharp tests cover working over a mounted volume (as it is quite complex to setup the context in an automated way with potentially low privileges...)
First of all, please note that the provided custom build should be used with caution. Although it seems to be working, the libgit2 version it's been built against doesn't officially exist per se.
Understood :-)
It's made tricker by not yet having a definite reason as to why this is failing on your machine but libgit2sharp's tests themselves don't fail, which are also run on TC.
To be clear, the test that I have just ran, was a brand new console application, running on my own machine (not the TeamCity server) and I was able to reproduce the problem
I don't quite get what you meant above when you said you had a "local fileshare" rather than anything remote. Doesn't "fileshare" imply that the files/volume are hosted on a different computer and mounted via SMB?
Perhaps I didn't use the right terminology, let me try again...
I have a Stash Server, which is running on a Virtual Machine. The houses several repositories, all of which are accessed over HTTP, using the URL to the git repository.
In my sample console application, as well as in TeamCity, we are cloning the repository from the Stash server onto a location file location, i.e. the TeamCity working directory c:\ProgramData\JetBrains\TeamCity... and in the case of the sample application above c:\temp\test. I am not cloning the repository into a UNC path, or SMB file share, or mapped drive, etc.
Does that make sense?
Thanks again!
Thanks for the clarification. If it can simply happen on a local volume, I wonder why we haven't seen it beforehand. Some of the libgit2 tests grab some data off of BitBucket, which should be equivalent to grabbing it from Stash...
What OS version are you running?
Thanks for the clarification.
@carlosmn Not a problem.
For the demo application that I was running out of Visual Studio, I am using Windows 7. And the TeamCity Build Agent where GitVersion is failing is running Windows Server 2008 R2.
I also tried my sample application on my own desktop machine (running Windows 8) against a repo I have in BitBucket, and this also worked.
Random thought...
Could this be something to do with being on a Windows Domain?
Thank you guys. I was not able to test until now, but it looks like the fix is working, at least I got the repo cloned :-) I am very impressed with your fast and serious response!