git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH v2 0/3] mingw: introduce a way to avoid std handle inheritance

From
Johannes Schindelin <johannes.schindelin@gmx.de>
Date
Nov 1, 2017, 17:10 UTC
Message-ID
<cover.1509556153.git.johannes.schindelin@gmx.de>
In-Reply-To
<cover.1509382976.git.johannes.schindelin@gmx.de>

Particularly when calling Git from applications, such as Visual Studio, it is important that stdin/stdout/stderr are closed properly. However, when spawning processes on Windows, those handles must be marked as inheritable if we want to use them, but that flag is a global flag and may very well be used by other spawned processes which then do not know to close those handles.

As a workaround, introduce handling for the environment variables GIT_REDIRECT_STD* to read/write from/to named pipes instead (conceptually similar to Unix sockets, for you Linux folks). These do not need to be marked as inheritable, as the process can simply open the named pipe. No global flags. No problems.

This feature was introduced as an experimental feature into Git for Windows v2.11.0(2) and has been tested ever since. I feel it is well-tested enough that it can be integrated into core Git.

Changes since v1:
- clarified what the values of the environment variables mean.
- the feature is no longer marked experimental (I actually expected *some*
  remark about this, but it seems nobody looked at that patch all that closely
  *frown*)
- edited all commit messages heavily (primarily prodded by Jonathan's feedback).
Johannes Schindelin (3):
  mingw: add experimental feature to redirect standard handles
  mingw: optionally redirect stderr/stdout via the same handle
  mingw: document the standard handle redirection
 Documentation/git.txt | 18 ++++++++++++++++
 compat/mingw.c        | 58 +++++++++++++++++++++++++++++++++++++++++++++++++++
 t/t0001-init.sh       | 12 +++++++++++
 3 files changed, 88 insertions(+)
base-commit: cb5918aa0d50f50e83787f65c2ddc3dcb10159fe
Published-As: https://github.com/dscho/git/releases/tag/redirect-std-handles-v2
Fetch-It-Via: git fetch https://github.com/dscho/git redirect-std-handles-v2
Interdiff vs v1:
 diff --git a/Documentation/git.txt b/Documentation/git.txt
 index 10a98603b39..463b0eb0f5c 100644
 --- a/Documentation/git.txt
 +++ b/Documentation/git.txt
 @@ -712,14 +712,15 @@ of clones and fetches.
  `GIT_REDIRECT_STDIN`::
  `GIT_REDIRECT_STDOUT`::
  `GIT_REDIRECT_STDERR`::
 -	(EXPERIMENTAL) Windows-only: allow redirecting the standard
 -	input/output/error handles. This is particularly useful in
 -	multi-threaded applications where the canonical way to pass
 -	standard handles via `CreateProcess()` is not an option because
 -	it would require the handles to be marked inheritable (and
 -	consequently *every* spawned process would inherit them, possibly
 -	blocking regular Git operations). The primary intended use case
 -	is to use named pipes for communication.
 +	Windows-only: allow redirecting the standard input/output/error
 +	handles to paths specified by the environment variables. This is
 +	particularly useful in multi-threaded applications where the
 +	canonical way to pass standard handles via `CreateProcess()` is
 +	not an option because it would require the handles to be marked
 +	inheritable (and consequently *every* spawned process would
 +	inherit them, possibly blocking regular Git operations). The
 +	primary intended use case is to use named pipes for communication
 +	(e.g. `\\.\pipe\my-git-stdin-123`).
  +
  Two special values are supported: `off` will simply close the
  corresponding standard handle, and if `GIT_REDIRECT_STDERR` is
-- 
2.15.0.windows.1
Previous: Stefan BellerNext: Johannes Schindelin
Message 19 of 22 in “mingw: introduce a way to avoid std handle inheritance”
  1. 0/3 mingw: introduce a way to avoid std handle inheritanceJohannes Schindelin, Oct 30, 2017
  2. 1/3 mingw: add experimental feature to redirect standard handlesJohannes Schindelin, Oct 30, 2017
  3. 2/3 mingw: special-case GIT_REDIRECT_STDERR=2>&1Johannes Schindelin, Oct 30, 2017
  4. 3/3 mingw: document the experimental standard handle redirectionJohannes Schindelin, Oct 30, 2017
  5. Eric SunshineOct 30, 2017
  6. Johannes SchindelinOct 31, 2017
  7. Junio C HamanoNov 1, 2017
  8. Johannes SchindelinNov 1, 2017
  9. Junio C HamanoNov 2, 2017
  10. Johannes SchindelinNov 2, 2017
  11. Junio C HamanoNov 3, 2017
  12. Johannes SchindelinNov 3, 2017
  13. Jonathan NiederOct 30, 2017
  14. Junio C HamanoOct 31, 2017
  15. Johannes SchindelinOct 31, 2017
  16. Jonathan NiederOct 31, 2017
  17. Johannes SchindelinNov 1, 2017
  18. Stefan BellerNov 1, 2017
  19. 0/3 mingw: introduce a way to avoid std handle inheritanceJohannes Schindelin, Nov 1, 2017
  20. 1/3 mingw: add experimental feature to redirect standard handlesJohannes Schindelin, Nov 1, 2017
  21. 2/3 mingw: optionally redirect stderr/stdout via the same handleJohannes Schindelin, Nov 1, 2017
  22. 3/3 mingw: document the standard handle redirectionJohannes Schindelin, Nov 1, 2017

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.