[PATCH v2] Cygwin: console: Clear readahead buffer on tcflush()
Takashi Yano
takashi.yano@nifty.ne.jp
Wed Aug 19 05:16:02 GMT 2026
Previously, tcflush(TCIFLUSH) only discarded the input events and
did not clear the readahead buffer. Because of this bug, when user
program called select() -> tcflush() -> read(), the last read()
returned the contents of the readahead buffer instead of blocking
as it should.
Correctly, tcflush() must discard all pending input, so read()
should block until new input arrives. With this patch, the read-
ahead buffer and `rapoi` are also cleared as well as input events.
Fixes: 8382778cdb57 ("Cygwin: console: fix select() behaviour")
Suggested-by: Johannes Schindelin <johannes.schindelin@gmx.de>
Signed-off-by: Takashi Yano <takashi.yano@nifty.ne.jp>
Reviewed-by: Johannes Schindelin <Johannes.Schindelin@gmx.de>
---
v2: Add input_mutex guard. Clear rapoi as well.
winsup/cygwin/fhandler/console.cc | 7 +++++++
1 file changed, 7 insertions(+)
diff --git a/winsup/cygwin/fhandler/console.cc b/winsup/cygwin/fhandler/console.cc
index ba35ca44c..a5887ca8d 100644
--- a/winsup/cygwin/fhandler/console.cc
+++ b/winsup/cygwin/fhandler/console.cc
@@ -2351,6 +2351,9 @@ fhandler_console::tcflush (int queue)
if (queue == TCIFLUSH
|| queue == TCIOFLUSH)
{
+ /* tcflush() may be called inside the input_mutex,
+ however, mutex of Win32 can be acquired recursively. */
+ WaitForSingleObject (input_mutex, mutex_timeout);
acquire_attach_mutex (mutex_timeout);
DWORD resume_pid = attach_console (con.owner);
BOOL r = FlushConsoleInputBuffer (get_handle ());
@@ -2362,6 +2365,10 @@ fhandler_console::tcflush (int queue)
res = -1;
}
con.num_processed = 0;
+ eat_readahead (-1);
+ input_ready = false;
+ con.cons_rapoi = NULL;
+ ReleaseMutex(input_mutex);
}
return res;
}
--
2.51.0
More information about the Cygwin-patches
mailing list