Skip to content

Commit 8b533f0

Browse files
committed
fixup! mingw: spawned processes need to inherit only standard handles
Let's make the code a little more robust: when `CreateProcess()` fails and we tried to restrict handle inheritance, and when that failure was *still* not anticipated by the current code, warn loudly but fall back to not restricting the handle inheritance. Use an environment variable to avoid warning more than once per Git operation, because it would get very annoying if a simple `git fetch` reported the same type of warning about six times. This should avoid blocking users if there are still bugs left, but still give us the benefit of proper bug reports. While at it, reformat the CreateProcess() call to conform to Git's conventions. Signed-off-by: Johannes Schindelin <[email protected]>
1 parent 8a71352 commit 8b533f0

File tree

1 file changed

+31
-4
lines changed

1 file changed

+31
-4
lines changed

compat/mingw.c

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1750,13 +1750,40 @@ static pid_t mingw_spawnve_fd(const char *cmd, const char **argv, char **deltaen
17501750
* handle inheritance. This is still better than failing to create
17511751
* processes.
17521752
*/
1753-
if (!ret && GetLastError() == ERROR_NO_SYSTEM_RESOURCES &&
1754-
restrict_handle_inheritance && stdhandles_count) {
1753+
if (!ret && restrict_handle_inheritance && stdhandles_count) {
1754+
DWORD err = GetLastError();
1755+
if (err != ERROR_NO_SYSTEM_RESOURCES &&
1756+
!getenv("SUPPRESS_HANDLE_INHERITANCE_WARNING")) {
1757+
struct strbuf buf = STRBUF_INIT;
1758+
DWORD fl;
1759+
int i;
1760+
1761+
setenv("SUPPRESS_HANDLE_INHERITANCE_WARNING", "1", 1);
1762+
1763+
for (i = 0; i < stdhandles_count; i++) {
1764+
HANDLE h = stdhandles[i];
1765+
strbuf_addf(&buf, "handle #%d: %p (type %lx, "
1766+
"handle info (%d) %lx\n", i, h,
1767+
GetFileType(h),
1768+
GetHandleInformation(h, &fl),
1769+
fl);
1770+
}
1771+
strbuf_addstr(&buf, "\nThis is a bug; please report it "
1772+
"at\nhttps://github.com/git-for-windows/"
1773+
"git/issues/new\n\n"
1774+
"To suppress this warning, please set "
1775+
"the environment variable\n\n"
1776+
"\tSUPPRESS_HANDLE_INHERITANCE_WARNING=1"
1777+
"\n");
1778+
warning("failed to restrict file handles (%ld)\n\n%s",
1779+
err, buf.buf);
1780+
strbuf_release(&buf);
1781+
}
17551782
restrict_handle_inheritance = 0;
17561783
flags &= ~EXTENDED_STARTUPINFO_PRESENT;
17571784
ret = CreateProcessW(*wcmd ? wcmd : NULL, wargs, NULL, NULL,
1758-
TRUE, flags, wenvblk, dir ? wdir : NULL,
1759-
&si.StartupInfo, &pi);
1785+
TRUE, flags, wenvblk, dir ? wdir : NULL,
1786+
&si.StartupInfo, &pi);
17601787
}
17611788

17621789
if (!ret)

0 commit comments

Comments
 (0)