Skip to content

Commit fa1e820

Browse files
committed
Cygwin: pty: Do not transfer input to nat-pipe while masked
On the command "cat | non-cygwin-app", `cat` sometimes fails to read key input. This happens when `cat` starts to read input before `non- cygwin-app` configures pseudo console. This is because pipe state is switched to nat-pipe when pseudo console is configured. This patch prevent the pipe state from changing to nat-pipe state if some cygwin process is reading input from the cyg-pipe. Fixes: f206417 ("Cygwin: pty: Reduce unecessary input transfer.") Signed-off-by: Takashi Yano <takashi.yano@nifty.ne.jp> Reviewed-by: Johannes Schindelin <Johannes.Schindelin@gmx.de> (cherry picked from commit 4c0fc56)
1 parent c313089 commit fa1e820

1 file changed

Lines changed: 25 additions & 5 deletions

File tree

winsup/cygwin/fhandler/pty.cc

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -998,6 +998,10 @@ fhandler_pty_slave::open_setup (int flags)
998998
void
999999
fhandler_pty_slave::cleanup ()
10001000
{
1001+
fhandler_pty_slave *arch = (fhandler_pty_slave *) archetype ? : this;
1002+
while (arch->num_reader)
1003+
mask_switch_to_nat_pipe (false, false);
1004+
10011005
/* This used to always call fhandler_pty_common::close when we were execing
10021006
but that caused multiple closes of the handles associated with this pty.
10031007
Since close_all_files is not called until after the cygwin process has
@@ -1255,19 +1259,23 @@ fhandler_pty_slave::write (const void *ptr, size_t len)
12551259
void
12561260
fhandler_pty_slave::mask_switch_to_nat_pipe (bool mask, bool xfer)
12571261
{
1262+
/* This input_mutex guard works as expected only because every
1263+
caller of transfer_input() holds input_mutex. This is a non-
1264+
local precondition. */
1265+
WaitForSingleObject (input_mutex, mutex_timeout);
12581266
char name[MAX_PATH];
12591267
shared_name (name, TTY_SLAVE_READING, get_minor ());
12601268
HANDLE masked = OpenEvent (READ_CONTROL, FALSE, name);
12611269
CloseHandle (masked);
12621270

1263-
WaitForSingleObject (input_mutex, mutex_timeout);
1271+
fhandler_pty_slave *arch = (fhandler_pty_slave *) archetype ? : this;
12641272
if (mask)
12651273
{
1266-
if (InterlockedIncrement (&num_reader) == 1)
1267-
slave_reading = CreateEvent (&sec_none_nih, TRUE, FALSE, name);
1274+
if (InterlockedIncrement (&arch->num_reader) == 1)
1275+
arch->slave_reading = CreateEvent (&sec_none_nih, TRUE, FALSE, name);
12681276
}
1269-
else if (InterlockedDecrement (&num_reader) == 0)
1270-
CloseHandle (slave_reading);
1277+
else if (InterlockedDecrement (&arch->num_reader) == 0)
1278+
CloseHandle (arch->slave_reading);
12711279

12721280
if (!!masked != mask && xfer && get_ttyp ()->switch_to_nat_pipe)
12731281
{
@@ -4120,6 +4128,18 @@ fhandler_pty_slave::transfer_input (tty::xfer_dir dir, HANDLE from, tty *ttyp,
41204128
HANDLE input_available_event,
41214129
HANDLE input_transferred_to_cyg)
41224130
{
4131+
if (dir == tty::to_nat)
4132+
{
4133+
char name[MAX_PATH];
4134+
shared_name (name, TTY_SLAVE_READING, ttyp->get_minor ());
4135+
HANDLE masked = OpenEvent (READ_CONTROL, FALSE, name);
4136+
CloseHandle (masked);
4137+
if (masked)
4138+
/* Cygwin process is reading cyg-pipe.
4139+
Do not transfer input to nat-pipe. */
4140+
return;
4141+
}
4142+
41234143
HANDLE to;
41244144
if (dir == tty::to_nat)
41254145
to = ttyp->to_slave_nat ();

0 commit comments

Comments
 (0)