diff --git a/ProcessUNIX.c b/ProcessUNIX.c
index 3b32ca7d0a26b388e7a88cebb2e2e76747157b9e..958c9d34ec3a4e649a1cc8ab0dc64da650ea4bea 100644
--- a/ProcessUNIX.c
+++ b/ProcessUNIX.c
@@ -1938,6 +1938,7 @@ static int kwsysProcessSetupOutputPipeFile(int* p, const char* name)
 
   /* Set close-on-exec flag on the pipe's end.  */
   if (fcntl(fout, F_SETFD, FD_CLOEXEC) < 0) {
+    close(fout);
     return 0;
   }