Cygwin: console: Fix typeahead input for bash

Currently, following misbehaviour occurs in bash.
  1) Run "sleep 10".
  2) Type "cmd<enter>ps<enter>" while "sleep is running".
  3) After "sleep" ends, "ps" does not run in "cmd".
  4) exit from "cmd". Then, "ps" is executed.
This is because process_input_message() handles all the events in
the console input buffer, and stores key input into readahead buffer.
However, since the readahead buffer is unique to process, "cmd"
cannot read it. Since "ps<enter>" is stored in bash's readahead
buffer, it is executed by bash after "cmd" exits. With this patch,
process_input_message() handles only the requested amount of events
by read().

Fixes: 8382778cdb ("Cygwin: console: fix select() behaviour")
Signed-off-by: Takashi Yano <takashi.yano@nifty.ne.jp>
Reviewed-by: Mark Geisert <mark@maxrnd.com>
This commit is contained in:
Takashi Yano committed 2026-06-24 21:21:26 +09:00
1 parent 66324edf64
commit fac73911f5
4 files changed
+16 -5

No files matched your search

+12 -3
View File
@@ -1273,7 +1273,7 @@ wait_retry:
int ret;
acquire_input_mutex (mutex_timeout);
ret = process_input_message ();
ret = process_input_message (buflen);
switch (ret)
{
case input_error:
@@ -1328,9 +1328,10 @@ sig_exit:
}
fhandler_console::input_states
fhandler_console::process_input_message (void)
fhandler_console::process_input_message (size_t len)
{
char tmp[60];
size_t num_chars = 0;
if (!shared_console_info[unit])
return input_error;
@@ -1717,6 +1718,7 @@ fhandler_console::process_input_message (void)
continue;
}
num_chars += nread;
if (toadd)
{
ssize_t ret;
@@ -1734,15 +1736,22 @@ fhandler_console::process_input_message (void)
goto out;
}
}
/* len == 0 if called from select.cc:peek_console() */
if (len && num_chars >= len)
goto out;
}
out:
if (len == 0)
/* If len == 0, cancel reading from console input buffer.
Clear readahead buffer. */
eat_readahead (-1);
/* Discard processed recored. */
DWORD discard_len = min (total_read, i + 1);
/* If input is signalled, do not discard input here because
tcflush() is already called from line_edit(). */
if (stat == input_signalled && !(ti->c_lflag & NOFLSH))
discard_len = 0;
if (discard_len)
if (discard_len && (len || stat != input_ok))
{
acquire_attach_mutex (mutex_timeout);
DWORD resume_pid = attach_console (con.owner);
+1 -1
View File
@@ -2327,7 +2327,7 @@ private:
fh->copy_from (this);
return fh;
}
input_states process_input_message ();
input_states process_input_message (size_t len);
bg_check_types bg_check (int sig, bool dontsignal = false);
void setup_io_mutex (void);
DWORD __acquire_input_mutex (const char *fn, int ln, DWORD ms);
+2
View File
@@ -22,3 +22,5 @@ Fixes:
- Ensure the cons_master_thread runs only when it is really supposed to.
- Fix NOFLSH behaviour in console a bit.
- Fix typeahead input in console for bash.
+1 -1
View File
@@ -1172,7 +1172,7 @@ peek_console (select_record *me, bool)
if (!r || !events_read)
break;
}
if (fhandler_console::input_winch == fh->process_input_message ()
if (fhandler_console::input_winch == fh->process_input_message (0)
&& global_sigs[SIGWINCH].sa_handler != SIG_IGN
&& global_sigs[SIGWINCH].sa_handler != SIG_DFL)
{