[PATCH] Cygwin: pty: Make pcon_start handling more multi thread durable

Takashi Yano takashi.yano@nifty.ne.jp
Wed Mar 25 13:09:05 GMT 2026


Currently, if the CSI6n response is devided 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 == 1, all the data written goes
to 'wpbuf[]'. This may break statup of pseudo console.

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[]'.

Fixes: bb4285206207 ("Cygwin: pty: Implement new pseudo console support.")
Signed-off-by: Takashi Yano <takashi.yano@nifty.ne.jp>
Reviewed-by:
---
 winsup/cygwin/fhandler/pty.cc | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)

diff --git a/winsup/cygwin/fhandler/pty.cc b/winsup/cygwin/fhandler/pty.cc
index 8e6fb9c23..dda892269 100644
--- a/winsup/cygwin/fhandler/pty.cc
+++ b/winsup/cygwin/fhandler/pty.cc
@@ -2230,6 +2230,7 @@ fhandler_pty_master::write (const void *ptr, size_t len)
       static char wpbuf[wpbuf_len];
       static int ixput = 0;
       static int state = 0;
+      static DWORD wp_tid = 0;
 
       DWORD n;
       WaitForSingleObject (input_mutex, mutex_timeout);
@@ -2242,8 +2243,9 @@ fhandler_pty_master::write (const void *ptr, size_t len)
 		line_edit (wpbuf, ixput, ti, &ret);
 	      ixput = 0;
 	      state = 1;
+	      wp_tid = _my_tls.thread_id;
 	    }
-	  if (state == 1)
+	  if (state == 1 && wp_tid == _my_tls.thread_id)
 	    {
 	      if (ixput < wpbuf_len)
 		wpbuf[ixput++] = p[i];
@@ -2259,7 +2261,7 @@ fhandler_pty_master::write (const void *ptr, size_t len)
 	    line_edit (p + i, 1, ti, &ret);
 	  len = orig_len - i - 1;
 	  ptr = p + i + 1;
-	  if (state == 1 && p[i] == 'R')
+	  if (state == 1 && wp_tid == _my_tls.thread_id && p[i] == 'R')
 	    state = 2;
 	  if (state == 2)
 	    {
-- 
2.51.0



More information about the Cygwin-patches mailing list