[PATCH] Cygwin: pty: Add missing CloseHandle() calls.

Takashi Yano takashi.yano@nifty.ne.jp
Wed Jan 1 06:49:00 GMT 2020


---
 winsup/cygwin/fhandler_tty.cc | 22 ++++++++++++++++++++--
 1 file changed, 20 insertions(+), 2 deletions(-)

diff --git a/winsup/cygwin/fhandler_tty.cc b/winsup/cygwin/fhandler_tty.cc
index 65b12fd6c..23156f977 100644
--- a/winsup/cygwin/fhandler_tty.cc
+++ b/winsup/cygwin/fhandler_tty.cc
@@ -2242,11 +2242,27 @@ fhandler_pty_master::close ()
 	  /* Terminate helper process */
 	  SetEvent (get_ttyp ()->h_helper_goodbye);
 	  WaitForSingleObject (get_ttyp ()->h_helper_process, INFINITE);
+	  CloseHandle (get_ttyp ()->h_helper_goodbye);
+	  CloseHandle (get_ttyp ()->h_helper_process);
 	  /* FIXME: Pseudo console can be accessed via its handle
 	     only in the process which created it. What else can we do? */
 	  if (master_pid_tmp == myself->pid)
-	    /* Release pseudo console */
-	    ClosePseudoConsole (get_pseudo_console ());
+	    {
+	      /* ClosePseudoConsole() seems to have a bug that one
+		 internal handle remains opened. This causes handle leak.
+		 This is a workaround for this problem. */
+	      typedef struct
+	      {
+		HANDLE hWritePipe;
+		HANDLE hConDrvReference;
+		HANDLE hConHostProcess;
+	      } HPCON_INTERNAL;
+	      HPCON_INTERNAL *hp = (HPCON_INTERNAL *) get_pseudo_console ();
+	      HANDLE tmp = hp->hConHostProcess;
+	      /* Release pseudo console */
+	      ClosePseudoConsole (get_pseudo_console ());
+	      CloseHandle (tmp);
+	    }
 	  get_ttyp ()->switch_to_pcon_in = false;
 	  get_ttyp ()->switch_to_pcon_out = false;
 	}
@@ -3212,6 +3228,8 @@ fhandler_pty_master::setup_pseudoconsole ()
 		  FALSE, EXTENDED_STARTUPINFO_PRESENT,
 		  NULL, NULL, &si_helper.StartupInfo, &pi_helper);
   WaitForSingleObject (hello, INFINITE);
+  CloseHandle (hello);
+  CloseHandle (pi_helper.hThread);
   /* Retrieve pseudo console handles */
   DWORD rLen;
   char buf[64];
-- 
2.21.0



More information about the Cygwin-patches mailing list