Skip to content

Commit 0209b06

Browse files
committed
mingw: restrict file handle inheritance only on Windows 7 and later
Turns out that it don't work so well on Vista, see #1742 for details. According to https://devblogs.microsoft.com/oldnewthing/?p=8873, it *should* work on Windows Vista and later. But apparently there are issues on Windows Vista when pipes are involved. Given that Windows Vista is past its end of life (official support ended on April 11th, 2017), let's not spend *too* much time on this issue and just disable the file handle inheritance restriction on any Windows version earlier than Windows 7. Signed-off-by: Johannes Schindelin <[email protected]>
1 parent 041d54c commit 0209b06

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

Documentation/config/core.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -557,6 +557,12 @@ core.unsetenvvars::
557557
Defaults to `PERL5LIB` to account for the fact that Git for
558558
Windows insists on using its own Perl interpreter.
559559

560+
core.restrictinheritedhandles::
561+
Windows-only: override whether spawned processes inherit only standard
562+
file handles (`stdin`, `stdout` and `stderr`) or all handles. Can be
563+
`auto`, `true` or `false`. Defaults to `auto`, which means `true` on
564+
Windows 7 and later, and `false` on older Windows versions.
565+
560566
core.createObject::
561567
You can set this to 'link', in which case a hardlink followed by
562568
a delete of the source are used to make sure that object creation

compat/mingw.c

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,7 @@ enum hide_dotfiles_type {
227227
HIDE_DOTFILES_DOTGITONLY
228228
};
229229

230+
static int core_restrict_inherited_handles = -1;
230231
static enum hide_dotfiles_type hide_dotfiles = HIDE_DOTFILES_DOTGITONLY;
231232
static char *unset_environment_variables;
232233

@@ -246,6 +247,15 @@ int mingw_core_config(const char *var, const char *value, void *cb)
246247
return 0;
247248
}
248249

250+
if (!strcmp(var, "core.restrictinheritedhandles")) {
251+
if (value && !strcasecmp(value, "auto"))
252+
core_restrict_inherited_handles = -1;
253+
else
254+
core_restrict_inherited_handles =
255+
git_config_bool(var, value);
256+
return 0;
257+
}
258+
249259
return 0;
250260
}
251261

@@ -1435,7 +1445,7 @@ static pid_t mingw_spawnve_fd(const char *cmd, const char **argv, char **deltaen
14351445
const char *dir,
14361446
int prepend_cmd, int fhin, int fhout, int fherr)
14371447
{
1438-
static int restrict_handle_inheritance = 1;
1448+
static int restrict_handle_inheritance = -1;
14391449
STARTUPINFOEXW si;
14401450
PROCESS_INFORMATION pi;
14411451
LPPROC_THREAD_ATTRIBUTE_LIST attr_list = NULL;
@@ -1451,6 +1461,16 @@ static pid_t mingw_spawnve_fd(const char *cmd, const char **argv, char **deltaen
14511461
is_msys2_sh(*argv) ? quote_arg_msys2 : quote_arg_msvc;
14521462
const char *strace_env;
14531463

1464+
if (restrict_handle_inheritance < 0)
1465+
restrict_handle_inheritance = core_restrict_inherited_handles;
1466+
/*
1467+
* The following code to restrict which handles are inherited seems
1468+
* to work properly only on Windows 7 and later, so let's disable it
1469+
* on Windows Vista and 2008.
1470+
*/
1471+
if (restrict_handle_inheritance < 0)
1472+
restrict_handle_inheritance = GetVersion() >> 16 >= 7601;
1473+
14541474
do_unset_environment_variables();
14551475

14561476
/* Determine whether or not we are associated to a console */

0 commit comments

Comments
 (0)