public inbox for cygwin-patches@cygwin.com
 help / color / mirror / Atom feed
* [PATCH] Cygwin: pty: Fix handle leak regarding attach_mutex.
@ 2021-10-10  0:49 Takashi Yano
  2021-10-10 12:59 ` Ken Brown
  0 siblings, 1 reply; 2+ messages in thread
From: Takashi Yano @ 2021-10-10  0:49 UTC (permalink / raw)
  To: cygwin-patches

- If the process having master pty opened is forked, attach_mutex
  fails to be closed when master is closed. This patch fixes the
  issue.
---
 winsup/cygwin/fhandler_console.cc | 2 +-
 winsup/cygwin/fhandler_tty.cc     | 6 +++---
 2 files changed, 4 insertions(+), 4 deletions(-)

diff --git a/winsup/cygwin/fhandler_console.cc b/winsup/cygwin/fhandler_console.cc
index ee862b17d..aee5e8284 100644
--- a/winsup/cygwin/fhandler_console.cc
+++ b/winsup/cygwin/fhandler_console.cc
@@ -57,7 +57,7 @@ fhandler_console::console_state NO_COPY *fhandler_console::shared_console_info;
 bool NO_COPY fhandler_console::invisible_console;
 
 /* Mutex for AttachConsole()/FreeConsole() in fhandler_tty.cc */
-HANDLE NO_COPY attach_mutex;
+HANDLE attach_mutex;
 
 static inline void
 acquire_attach_mutex (DWORD t)
diff --git a/winsup/cygwin/fhandler_tty.cc b/winsup/cygwin/fhandler_tty.cc
index 823dabf73..f523dafed 100644
--- a/winsup/cygwin/fhandler_tty.cc
+++ b/winsup/cygwin/fhandler_tty.cc
@@ -57,7 +57,7 @@ struct pipe_reply {
 };
 
 extern HANDLE attach_mutex; /* Defined in fhandler_console.cc */
-static LONG NO_COPY master_cnt = 0;
+static LONG master_cnt = 0;
 
 inline static bool pcon_pid_alive (DWORD pid);
 
@@ -2042,10 +2042,10 @@ fhandler_pty_master::close ()
 	    }
 	  release_output_mutex ();
 	  master_fwd_thread->terminate_thread ();
-	  if (InterlockedDecrement (&master_cnt) == 0)
-	    CloseHandle (attach_mutex);
 	}
     }
+  if (InterlockedDecrement (&master_cnt) == 0)
+    CloseHandle (attach_mutex);
 
   /* Check if the last master handle has been closed.  If so, set
      input_available_event to wake up potentially waiting slaves. */
-- 
2.33.0


^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [PATCH] Cygwin: pty: Fix handle leak regarding attach_mutex.
  2021-10-10  0:49 [PATCH] Cygwin: pty: Fix handle leak regarding attach_mutex Takashi Yano
@ 2021-10-10 12:59 ` Ken Brown
  0 siblings, 0 replies; 2+ messages in thread
From: Ken Brown @ 2021-10-10 12:59 UTC (permalink / raw)
  To: cygwin-patches

On 10/9/2021 8:49 PM, Takashi Yano wrote:
> - If the process having master pty opened is forked, attach_mutex
>    fails to be closed when master is closed. This patch fixes the
>    issue.
> ---
>   winsup/cygwin/fhandler_console.cc | 2 +-
>   winsup/cygwin/fhandler_tty.cc     | 6 +++---
>   2 files changed, 4 insertions(+), 4 deletions(-)
> 
> diff --git a/winsup/cygwin/fhandler_console.cc b/winsup/cygwin/fhandler_console.cc
> index ee862b17d..aee5e8284 100644
> --- a/winsup/cygwin/fhandler_console.cc
> +++ b/winsup/cygwin/fhandler_console.cc
> @@ -57,7 +57,7 @@ fhandler_console::console_state NO_COPY *fhandler_console::shared_console_info;
>   bool NO_COPY fhandler_console::invisible_console;
>   
>   /* Mutex for AttachConsole()/FreeConsole() in fhandler_tty.cc */
> -HANDLE NO_COPY attach_mutex;
> +HANDLE attach_mutex;
>   
>   static inline void
>   acquire_attach_mutex (DWORD t)
> diff --git a/winsup/cygwin/fhandler_tty.cc b/winsup/cygwin/fhandler_tty.cc
> index 823dabf73..f523dafed 100644
> --- a/winsup/cygwin/fhandler_tty.cc
> +++ b/winsup/cygwin/fhandler_tty.cc
> @@ -57,7 +57,7 @@ struct pipe_reply {
>   };
>   
>   extern HANDLE attach_mutex; /* Defined in fhandler_console.cc */
> -static LONG NO_COPY master_cnt = 0;
> +static LONG master_cnt = 0;
>   
>   inline static bool pcon_pid_alive (DWORD pid);
>   
> @@ -2042,10 +2042,10 @@ fhandler_pty_master::close ()
>   	    }
>   	  release_output_mutex ();
>   	  master_fwd_thread->terminate_thread ();
> -	  if (InterlockedDecrement (&master_cnt) == 0)
> -	    CloseHandle (attach_mutex);
>   	}
>       }
> +  if (InterlockedDecrement (&master_cnt) == 0)
> +    CloseHandle (attach_mutex);
>   
>     /* Check if the last master handle has been closed.  If so, set
>        input_available_event to wake up potentially waiting slaves. */

Pushed.  Thanks.

Ken

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2021-10-10 13:00 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-10-10  0:49 [PATCH] Cygwin: pty: Fix handle leak regarding attach_mutex Takashi Yano
2021-10-10 12:59 ` Ken Brown

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).