[newlib-cygwin/cygwin-3_6-branch] Cygwin: console: Release pipe_sw_mutex in pcon_hand_over_proc()

Takashi Yano via Cygwin-cvs <[email protected]> Thu, 26 Mar 2026 11:36:20 +0000 (GMT)
Newsgroups gmane.os.cygwin.cvs
Message-ID <[email protected]>
https://sourceware.org/git/gitweb.cgi?p=3Dnewlib-cygwin.git;h=3Dc641981e99c=
5d2d198217b842420129ff3f9efd9

commit c641981e99c5d2d198217b842420129ff3f9efd9
Author: Takashi Yano <[email protected]>
Date:   Tue Mar 24 11:25:40 2026 +0900

    Cygwin: console: Release pipe_sw_mutex in pcon_hand_over_proc()
   =20
    Currently, pipe_sw_mutex is held in the process which is running
    in console inherited from pseudo console until the process ends.
    Due to this behaviour, the process may cause deadlock when it
    attempts to acquire input_mutex in set_input_mode() called via
    close_ctty(). This deadlock occurs because the pty master
    acquire input_mutex first and acquire pipe_sw_mutex next while
    the process exiting acquire pipe_sw_mutex first.
   =20
    To avoid this deadlock, this patch releases pipe_sw_mutex in
    pcon_hand_over_proc(). In addition, pointless pipe_sw_mutex
    acquire/release is drppped in pcon_hand_over_proc().
   =20
    Fixes: 04f386e9af99 ("Cygwin: console: Inherit pcon hand over from pare=
nt pty")
    Signed-off-by: Takashi Yano <[email protected]>
    Reviewed-by: Corinna Vinschen <[email protected]>
    (cherry picked from commit 9ef8e3ad3bec51afddb26e472857962bd1b028e3)

Diff:
---
 winsup/cygwin/fhandler/console.cc | 5 +----
 1 file changed, 1 insertion(+), 4 deletions(-)

diff --git a/winsup/cygwin/fhandler/console.cc b/winsup/cygwin/fhandler/con=
sole.cc
index 831df4f2c..9e116118a 100644
--- a/winsup/cygwin/fhandler/console.cc
+++ b/winsup/cygwin/fhandler/console.cc
@@ -1946,8 +1946,6 @@ fhandler_console::pcon_hand_over_proc (void)
   char buf[MAX_PATH];
   shared_name (buf, PIPE_SW_MUTEX, parent_pty);
   HANDLE mtx =3D OpenMutex (MAXIMUM_ALLOWED, FALSE, buf);
-  WaitForSingleObject (mtx, INFINITE);
-  ReleaseMutex (mtx);
   DWORD res =3D WaitForSingleObject (mtx, INFINITE);
   if (res =3D=3D WAIT_OBJECT_0 || res =3D=3D WAIT_ABANDONED)
     {
@@ -1958,8 +1956,7 @@ fhandler_console::pcon_hand_over_proc (void)
     }
   else
     system_printf("Acquiring pcon_ho_mutex failed.");
-  /* Do not release the mutex.
-     Hold onto the mutex until this process completes. */
+  ReleaseMutex (mtx);
 }
=20
 bool