[newlib-cygwin] Cygwin: pty: Discard pcon input buffer when discard_input is called

Takashi Yano via Cygwin-cvs <[email protected]> Wed, 24 Jun 2026 13:11:20 +0000 (GMT)
Newsgroups gmane.os.cygwin.cvs
Message-ID <[email protected]>
https://sourceware.org/git/gitweb.cgi?p=3Dnewlib-cygwin.git;h=3Dee929717bd0=
9bd5612419e971cba7b6370039431

commit ee929717bd09bd5612419e971cba7b6370039431
Author: Takashi Yano <[email protected]>
Date:   Sat Jun 13 21:48:19 2026 +0900

    Cygwin: pty: Discard pcon input buffer when discard_input is called
   =20
    Previously, the process on pty could not be a child of non-cygwin
    process. In that case, it is not necessary to flush pcon input buffer
    even when discard_input() is called. However, now, the child process
    of non-cygwin app on pseudo console is running on pty. So,
    discard_input() should affect to the pcon input buffer as well.
   =20
    This prevents the problem wihch can be demonstrated by:
      1) Run 'sleep 10' in cmd.exe
      2) Enter 'ps\n' while sleeping
      3) Press Ctrl-C
      4) 'ps' will be executed after terminating 'sleep' by Ctrl-C.
   =20
    Signed-off-by: Takashi Yano <[email protected]>
    Reviwed-by: Mark Geisert <[email protected]>

Diff:
---
 winsup/cygwin/fhandler/pty.cc | 11 ++++++++++-
 1 file changed, 10 insertions(+), 1 deletion(-)

diff --git a/winsup/cygwin/fhandler/pty.cc b/winsup/cygwin/fhandler/pty.cc
index fff0a34b4..06babcde1 100644
--- a/winsup/cygwin/fhandler/pty.cc
+++ b/winsup/cygwin/fhandler/pty.cc
@@ -583,6 +583,14 @@ fhandler_pty_master::discard_input ()
   if (!get_ttyp ()->pcon_activated)
     while (::bytes_available (bytes_in_pipe, from_master_nat) && bytes_in_=
pipe)
       ReadFile (from_master_nat, buf, sizeof(buf), &n, NULL);
+  else
+    {
+      DWORD target_pid =3D get_ttyp ()->nat_pipe_owner_pid;
+      DWORD resume_pid =3D
+	fhandler_pty_common::attach_console_temporarily (target_pid);
+      FlushConsoleInputBuffer (h_pcon_in_dupped);
+      fhandler_pty_common::resume_from_temporarily_attach (resume_pid);
+    }
   get_ttyp ()->discard_input =3D true;
   ReleaseMutex (input_mutex);
 }
@@ -2598,7 +2606,8 @@ fhandler_pty_master::write (const void *ptr, size_t l=
en)
       for (size_t i =3D 0, j =3D 0; i < len; i++)
 	{
 	  process_sig_state r =3D process_sigs (buf[i], get_ttyp (), this);
-	  if (r !=3D done_with_debugger)
+	  if (r !=3D done_with_debugger &&
+	      (r !=3D signalled || (ti.c_lflag & NOFLSH) || buf[i] =3D=3D '\003'))
 	    {
 	      char c =3D buf[i];
 	      /* Workaround for pseudo console in Windows 11 */