public inbox for cygwin-cvs@sourceware.org
help / color / mirror / Atom feed
* [newlib-cygwin/cygwin-3_3-branch] Cygwin: pty: Fix acquiring attach_mutex timing.
@ 2022-05-08 11:07 Takashi Yano
  0 siblings, 0 replies; only message in thread
From: Takashi Yano @ 2022-05-08 11:07 UTC (permalink / raw)
  To: cygwin-cvs

https://sourceware.org/git/gitweb.cgi?p=newlib-cygwin.git;h=ac2f7f888c3eba20941f5e0cc1c4bb5fd523e730

commit ac2f7f888c3eba20941f5e0cc1c4bb5fd523e730
Author: Takashi Yano <takashi.yano@nifty.ne.jp>
Date:   Sun May 8 17:48:06 2022 +0900

    Cygwin: pty: Fix acquiring attach_mutex timing.
    
    - When temporarily attaching a console, the timing of acquiring
      attach_mutex was not appropriate. This sometimes caused master
      forwarding thread to crash on Ctrl-C in Windows 7. This patch
      fixes the issue.

Diff:
---
 winsup/cygwin/fhandler_termios.cc |  2 +-
 winsup/cygwin/fhandler_tty.cc     | 12 ++++++------
 2 files changed, 7 insertions(+), 7 deletions(-)

diff --git a/winsup/cygwin/fhandler_termios.cc b/winsup/cygwin/fhandler_termios.cc
index 0d53ad25f..0bc360078 100644
--- a/winsup/cygwin/fhandler_termios.cc
+++ b/winsup/cygwin/fhandler_termios.cc
@@ -359,6 +359,7 @@ fhandler_termios::process_sigs (char c, tty* ttyp, fhandler_termios *fh)
 	     CTRL_C_EVENT. After sending the event, reattach to the
 	     console to which the process was previously attached.  */
 	  bool console_exists = fhandler_console::exists ();
+	  acquire_attach_mutex (mutex_timeout);
 	  pinfo pinfo_resume = pinfo (myself->ppid);
 	  DWORD resume_pid = 0;
 	  if (pinfo_resume)
@@ -366,7 +367,6 @@ fhandler_termios::process_sigs (char c, tty* ttyp, fhandler_termios *fh)
 	  else
 	    resume_pid = fhandler_pty_common::get_console_process_id
 	      (myself->dwProcessId, false);
-	  acquire_attach_mutex (mutex_timeout);
 	  if ((!console_exists || resume_pid) && fh && !fh->is_console ())
 	    {
 	      FreeConsole ();
diff --git a/winsup/cygwin/fhandler_tty.cc b/winsup/cygwin/fhandler_tty.cc
index c91f40044..b9cc9736d 100644
--- a/winsup/cygwin/fhandler_tty.cc
+++ b/winsup/cygwin/fhandler_tty.cc
@@ -533,6 +533,7 @@ fhandler_pty_master::accept_input ()
       DWORD target_pid = 0;
       if (pinfo_target)
 	target_pid = pinfo_target->dwProcessId;
+      acquire_attach_mutex (mutex_timeout);
       pinfo pinfo_resume = pinfo (myself->ppid);
       DWORD resume_pid;
       if (pinfo_resume)
@@ -544,7 +545,6 @@ fhandler_pty_master::accept_input ()
 	{
 	  /* Slave attaches to a different console than master.
 	     Therefore reattach here. */
-	  acquire_attach_mutex (mutex_timeout);
 	  FreeConsole ();
 	  AttachConsole (target_pid);
 	  cp_to = GetConsoleCP ();
@@ -552,10 +552,10 @@ fhandler_pty_master::accept_input ()
 	  if (resume_pid && console_exists)
 	    AttachConsole (resume_pid);
 	  init_console_handler (false);
-	  release_attach_mutex ();
 	}
       else
 	cp_to = GetConsoleCP ();
+      release_attach_mutex ();
 
       if (get_ttyp ()->term_code_page != cp_to)
 	{
@@ -1244,6 +1244,7 @@ fhandler_pty_slave::reset_switch_to_pcon (void)
 		OpenProcess (PROCESS_DUP_HANDLE, FALSE, get_ttyp ()->pcon_pid);
 	      if (pcon_owner)
 		{
+		  acquire_attach_mutex (mutex_timeout);
 		  pinfo pinfo_resume = pinfo (myself->ppid);
 		  DWORD resume_pid;
 		  if (pinfo_resume)
@@ -1257,7 +1258,6 @@ fhandler_pty_slave::reset_switch_to_pcon (void)
 		      DuplicateHandle (pcon_owner, get_ttyp ()->h_pcon_in,
 				       GetCurrentProcess (), &h_pcon_in,
 				       0, TRUE, DUPLICATE_SAME_ACCESS);
-		      acquire_attach_mutex (mutex_timeout);
 		      FreeConsole ();
 		      AttachConsole (get_ttyp ()->pcon_pid);
 		      init_console_handler (false);
@@ -1268,9 +1268,9 @@ fhandler_pty_slave::reset_switch_to_pcon (void)
 		      FreeConsole ();
 		      AttachConsole (resume_pid);
 		      init_console_handler (false);
-		      release_attach_mutex ();
 		      CloseHandle (h_pcon_in);
 		    }
+		  release_attach_mutex ();
 		  CloseHandle (pcon_owner);
 		}
 	    }
@@ -2878,6 +2878,7 @@ fhandler_pty_master::pty_master_fwd_thread (const master_fwd_thread_param_t *p)
       DWORD target_pid = 0;
       if (pinfo_target)
 	target_pid = pinfo_target->dwProcessId;
+      acquire_attach_mutex (mutex_timeout);
       pinfo pinfo_resume = pinfo (myself->ppid);
       DWORD resume_pid;
       if (pinfo_resume)
@@ -2889,7 +2890,6 @@ fhandler_pty_master::pty_master_fwd_thread (const master_fwd_thread_param_t *p)
 	{
 	  /* Slave attaches to a different console than master.
 	     Therefore reattach here. */
-	  acquire_attach_mutex (mutex_timeout);
 	  FreeConsole ();
 	  AttachConsole (target_pid);
 	  cp_from = GetConsoleOutputCP ();
@@ -2897,10 +2897,10 @@ fhandler_pty_master::pty_master_fwd_thread (const master_fwd_thread_param_t *p)
 	  if (resume_pid && console_exists)
 	    AttachConsole (resume_pid);
 	  init_console_handler (false);
-	  release_attach_mutex ();
 	}
       else
 	cp_from = GetConsoleOutputCP ();
+      release_attach_mutex ();
 
       if (p->ttyp->term_code_page != cp_from)
 	{


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2022-05-08 11:07 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-05-08 11:07 [newlib-cygwin/cygwin-3_3-branch] Cygwin: pty: Fix acquiring attach_mutex timing Takashi Yano

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).