Skip to content

Commit 4c0fc56

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>
1 parent 0d516c2 commit 4c0fc56

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
@@ -1282,6 +1282,10 @@ fhandler_pty_slave::open_setup (int flags)
12821282
void
12831283
fhandler_pty_slave::cleanup ()
12841284
{
1285+
fhandler_pty_slave *arch = (fhandler_pty_slave *) archetype ? : this;
1286+
while (arch->num_reader)
1287+
mask_switch_to_nat_pipe (false, false);
1288+
12851289
if (get_ttyp ()->pcon_activated && get_ttyp ()->getpgid () == myself->pgid)
12861290
req_fixup_pcon_state ();
12871291

@@ -1543,19 +1547,23 @@ fhandler_pty_slave::write (const void *ptr, size_t len)
15431547
void
15441548
fhandler_pty_slave::mask_switch_to_nat_pipe (bool mask, bool xfer)
15451549
{
1550+
/* This input_mutex guard works as expected only because every
1551+
caller of transfer_input() holds input_mutex. This is a non-
1552+
local precondition. */
1553+
WaitForSingleObject (input_mutex, mutex_timeout);
15461554
char name[MAX_PATH];
15471555
shared_name (name, TTY_SLAVE_READING, get_minor ());
15481556
HANDLE masked = OpenEvent (READ_CONTROL, FALSE, name);
15491557
CloseHandle (masked);
15501558

1551-
WaitForSingleObject (input_mutex, mutex_timeout);
1559+
fhandler_pty_slave *arch = (fhandler_pty_slave *) archetype ? : this;
15521560
if (mask)
15531561
{
1554-
if (InterlockedIncrement (&num_reader) == 1)
1555-
slave_reading = CreateEvent (&sec_none_nih, TRUE, FALSE, name);
1562+
if (InterlockedIncrement (&arch->num_reader) == 1)
1563+
arch->slave_reading = CreateEvent (&sec_none_nih, TRUE, FALSE, name);
15561564
}
1557-
else if (InterlockedDecrement (&num_reader) == 0)
1558-
CloseHandle (slave_reading);
1565+
else if (InterlockedDecrement (&arch->num_reader) == 0)
1566+
CloseHandle (arch->slave_reading);
15591567

15601568
if (!!masked != mask && xfer && get_ttyp ()->switch_to_nat_pipe)
15611569
{
@@ -4460,6 +4468,18 @@ fhandler_pty_slave::transfer_input (tty::xfer_dir dir, HANDLE from, tty *ttyp,
44604468
HANDLE input_available_event,
44614469
HANDLE input_transferred_to_cyg)
44624470
{
4471+
if (dir == tty::to_nat)
4472+
{
4473+
char name[MAX_PATH];
4474+
shared_name (name, TTY_SLAVE_READING, ttyp->get_minor ());
4475+
HANDLE masked = OpenEvent (READ_CONTROL, FALSE, name);
4476+
CloseHandle (masked);
4477+
if (masked)
4478+
/* Cygwin process is reading cyg-pipe.
4479+
Do not transfer input to nat-pipe. */
4480+
return;
4481+
}
4482+
44634483
HANDLE to;
44644484
if (dir == tty::to_nat)
44654485
to = ttyp->to_slave_nat ();

0 commit comments

Comments
 (0)