From 13f9414bd94dbac97d2a770700bbae9077c8827f Mon Sep 17 00:00:00 2001 From: Bruno Haible Date: Sun, 13 Dec 2020 21:45:55 +0100 Subject: [PATCH] spawn-pipe: Fix hanging processes on Windows (regression 2020-11-30). * lib/spawn-pipe.c (create_pipe): After spawning the subprocess, close the stdin_handle and/or stdout_handle. --- ChangeLog | 6 ++++++ lib/spawn-pipe.c | 26 ++++++++++++++++++++------ 2 files changed, 26 insertions(+), 6 deletions(-) diff --git a/ChangeLog b/ChangeLog index 86293259b7..b3f62960fa 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,9 @@ +2020-12-13 Bruno Haible + + spawn-pipe: Fix hanging processes on Windows (regression 2020-11-30). + * lib/spawn-pipe.c (create_pipe): After spawning the subprocess, close + the stdin_handle and/or stdout_handle. + 2020-12-12 Bruno Haible Fix gnulib-tool error when some modules occur in tests/. diff --git a/lib/spawn-pipe.c b/lib/spawn-pipe.c index ba07d20ebc..bd34959d50 100644 --- a/lib/spawn-pipe.c +++ b/lib/spawn-pipe.c @@ -222,6 +222,8 @@ create_pipe (const char *progname, bool must_close_ofd0 = pipe_stdin; /* Create standard file handles of child process. */ + HANDLE stdin_handle = INVALID_HANDLE_VALUE; + HANDLE stdout_handle = INVALID_HANDLE_VALUE; nulloutfd = -1; stdinfd = -1; stdoutfd = -1; @@ -243,7 +245,7 @@ create_pipe (const char *progname, to pass NULL, the child process would inherit a copy of the environment block - ignoring the effects of putenv() and [un]setenv(). */ { - HANDLE stdin_handle = + stdin_handle = (HANDLE) _get_osfhandle (pipe_stdin ? ofd[0] : prog_stdin == NULL ? STDIN_FILENO : stdinfd); if (pipe_stdin) @@ -261,7 +263,7 @@ create_pipe (const char *progname, close (ofd[0]); /* implies CloseHandle (stdin_handle); */ stdin_handle = duplicate; } - HANDLE stdout_handle = + stdout_handle = (HANDLE) _get_osfhandle (pipe_stdout ? ifd[1] : prog_stdout == NULL ? STDOUT_FILENO : stdoutfd); if (pipe_stdout) @@ -306,10 +308,22 @@ create_pipe (const char *progname, if (nulloutfd >= 0) close (nulloutfd); - if (must_close_ofd0) - close (ofd[0]); - if (must_close_ifd1) - close (ifd[1]); + if (pipe_stdin) + { + if (must_close_ofd0) + close (ofd[0]); + else + if (stdin_handle != INVALID_HANDLE_VALUE) + CloseHandle (stdin_handle); + } + if (pipe_stdout) + { + if (must_close_ifd1) + close (ifd[1]); + else + if (stdout_handle != INVALID_HANDLE_VALUE) + CloseHandle (stdout_handle); + } # else /* __KLIBC__ */ if (!(directory == NULL && strcmp (directory, ".") == 0)) -- 2.11.4.GIT