[newlib-cygwin] Cygwin: pty: Make pcon_start handling more multi thread durable

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

commit 2fa51041ac547189a9f04e7c83466c178fe06994
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]>

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 3c6b90ef5..c7ad1d059 100644
--- a/winsup/cygwin/fhandler/pty.cc
+++ b/winsup/cygwin/fhandler/pty.cc
@@ -2208,6 +2208,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);
@@ -2220,8 +2221,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];
@@ -2237,7 +2239,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)
 	    {
@@ -2248,6 +2250,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;