]> Savannah Git Hosting - gnulib.git/commitdiff
spawn-pipe: Fix hanging processes on Windows (regression 2020-11-30).
authorBruno Haible <bruno@clisp.org>
Sun, 13 Dec 2020 20:45:55 +0000 (21:45 +0100)
committerBruno Haible <bruno@clisp.org>
Sun, 13 Dec 2020 20:45:55 +0000 (21:45 +0100)
* lib/spawn-pipe.c (create_pipe): After spawning the subprocess, close
the stdin_handle and/or stdout_handle.

ChangeLog
lib/spawn-pipe.c

index 86293259b70fd50f5296b2d5b55abadaa08a5b9f..b3f62960fa6592f2b08c71af6fece4d05c07bbe1 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,9 @@
+2020-12-13  Bruno Haible  <bruno@clisp.org>
+
+       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  <bruno@clisp.org>
 
        Fix gnulib-tool error when some modules occur in tests/.
index ba07d20ebc403cb8817677e53704dceec9de6efb..bd34959d506b52df7b0055ec33efa145d1223350 100644 (file)
@@ -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))