[newlib-cygwin/cygwin-3_6-branch] Cygwin: pty: Make pcon_start handling more multi thread durable

Takashi Yano via Cygwin-cvs <[email protected]> Mon, 6 Apr 2026 12:18:41 +0000 (GMT)
Newsgroups gmane.os.cygwin.cvs
Message-ID <[email protected]>
https://sourceware.org/git/gitweb.cgi?p=3Dnewlib-cygwin.git;h=3Da8a61d88fba=
f2b40d6d905c3f9a90e546a35e3fb

commit a8a61d88fbaf2b40d6d905c3f9a90e546a35e3fb
Author: Takashi Yano <[email protected]>
Date:   Wed Mar 25 19:44:50 2026 +0900

    Cygwin: pty: Make pcon_start handling more multi thread durable
   =20
    Currently, if the CSI6n response is divided into "CSI10;2" and "R",
    and another thread call master write() with "c", the data written to
    nat pipe will be interleaved like "CSI10;2cR". The first "CSI10;2"
    make the 'state' 1, and in state =3D=3D 1, all the data written goes
    to 'wpbuf[]'. This may break startup of pseudo console.
   =20
    With this patch, the thread ID of the thread that write the first ESC
    char to 'wpbuf[]' is stored in 'wp_tid', and only if the thread ID
    matches 'wp_tid' will be written to 'wpbuf[]'.
   =20
    Fixes: bb4285206207 ("Cygwin: pty: Implement new pseudo console support=
.")
    Signed-off-by: Takashi Yano <[email protected]>
    Reviewed-by: Johannes Schindelin <[email protected]>
    (cherry picked from commit 2fa51041ac547189a9f04e7c83466c178fe06994)

Diff:
---
 winsup/cygwin/fhandler/pty.cc | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)

diff --git a/winsup/cygwin/fhandler/pty.cc b/winsup/cygwin/fhandler/pty.cc
index 29f780a39..7b000fe8d 100644
--- a/winsup/cygwin/fhandler/pty.cc
+++ b/winsup/cygwin/fhandler/pty.cc
@@ -2191,6 +2191,7 @@ fhandler_pty_master::write (const void *ptr, size_t l=
en)
       static char wpbuf[wpbuf_len];
       static int ixput =3D 0;
       static int state =3D 0;
+      static DWORD wp_tid =3D 0;
=20
       DWORD n;
       WaitForSingleObject (input_mutex, mutex_timeout);
@@ -2203,8 +2204,9 @@ fhandler_pty_master::write (const void *ptr, size_t l=
en)
 		line_edit (wpbuf, ixput, ti, &ret);
 	      ixput =3D 0;
 	      state =3D 1;
+	      wp_tid =3D _my_tls.thread_id;
 	    }
-	  if (state =3D=3D 1)
+	  if (state =3D=3D 1 && wp_tid =3D=3D _my_tls.thread_id)
 	    {
 	      if (ixput < wpbuf_len)
 		wpbuf[ixput++] =3D p[i];
@@ -2220,7 +2222,7 @@ fhandler_pty_master::write (const void *ptr, size_t l=
en)
 	    line_edit (p + i, 1, ti, &ret);
 	  len =3D orig_len - i - 1;
 	  ptr =3D p + i + 1;
-	  if (state =3D=3D 1 && p[i] =3D=3D 'R')
+	  if (state =3D=3D 1 && wp_tid =3D=3D _my_tls.thread_id && p[i] =3D=3D 'R=
')
 	    state =3D 2;
 	  if (state =3D=3D 2)
 	    {
@@ -2231,6 +2233,7 @@ fhandler_pty_master::write (const void *ptr, size_t l=
en)
 		WriteFile (to_slave_nat, wpbuf, ixput, &n, NULL);
 	      ixput =3D 0;
 	      state =3D 0;
+	      wp_tid =3D 0;
 	      get_ttyp ()->req_xfer_input =3D false;
 	      get_ttyp ()->pcon_start =3D false;
 	      break;