libgit2/libgit2 — pull request #2360 by phkelley (2014-05-16 17:29:01+00:00)
Title: Win32: Supply _O_NOINHERIT when calling _wopen

We have a build system which can build Git repositories. The workflow is:
1. Clone the repository.
2. Build the repository, by spawning a child process to do the build -- MSBuild.exe.

We found that if you ask the build system to build two Git repositories simultaneously, it doesn't work. Here is why -- the clone kicks off for both builds simultaneously. Inevitably one of the clones finishes before the other one. The guy who finished first calls MSBuild.exe to start step 2 of the workflow above. This happens while the other clone (the slower clone) is still in progress.

We found that the open handle to the pack file for the slower clone is actually duplicated into the MSBuild.exe child process. (On Windows, a 'handle' is the official word for 'file descriptor'). This is undesirable, and causes the slower clone to fail, because now it can't do things it wants to do to the pack file, because this other process has it open also. How did this happen?

On Windows, handles are normally *not* inheritable. You have to ask for this behavior by jumping through a couple of hoops. But it turns out that the C runtime library (which provides `_wopen`) defaults to jumping through these hoops. Presumably this is to be more like POSIX. The fix is to specify the `_O_NOINHERIT` flag to `_wopen` so that this doesn't happen.

I can't think of any reason why we would want for our handles to be inheritable on Windows.