Cygwin-3.1.3: read(2) from /dev/consN returns unexpected EOF after select(2) and redirection

Takashi Yano takashi.yano@nifty.ne.jp
Sat Jan 25 09:29:00 GMT 2020


On Fri, 24 Jan 2020 19:20:48 +0800
Koichi Murase wrote:
> Hi, this is another report related to Cygwin console.
> 
> Description:
> 
>   In a noncanonical mode of console (where tty is /dev/consN), if
>   there is some unprocessed inputs from the user side (i.e., terminal
>   side), the combination of select(2) (for read) and redirection
>   causes the next attempt of read(2) to return EOF [i.e., read(0, buf,
>   sz) = 0] even though the TTY is still alive.  Because of this
>   behavior, the shell session unexpectedly closes because the shell
>   considers the TTY has been closed.
> 
>   This only affects the first attempt of read(2) (in each process)
>   after the redirection.
> 
> Repeat-By:
> 
>   This is a reduced test case to reproduce the problem:
> 
>   1. Open cygwin console (pseudo console mode) with Bash
> 
>     This can be done by, for example, opening Run dialog with "Win +
>     R", typing "C:\cygwin64\bin\bash" and finally pressing "Enter".
> 
>   2. Run the following command
> 
>     $ stty -icanon; printf '\e[6n'; read -t 0; : < /dev/null; stty sane
> 
>     As a result, the terminal response CPR (CSI Pn ; Pn R) to DSR(6)
>     request (CSI 6 n) is expected to be inserted in the command line.
>     It should look like the following:
> 
>     $ 7;1R
> 
>     However, in the console, the Bash session is closed by the above
>     command because Bash receives EOF from the TTY and considers the
>     TTY session has been closed.
> 
> 
>   Note: Only the first attempt of read(2) after the redirection
>   returns EOF.  This can be confirmed by the following command where
>   the Bash session does not close.
> 
>     $ stty -icanon; printf '\e[6n'; read -t 0; : < /dev/null; read;
>     stty sane
> 
>   Note: Also the first read(2) fails for each forked process in the
>   session:
> 
>     $ stty -icanon; printf '\e[6n'; read -t 0; : < /dev/null; wc -c;
>     (read||echo fail;read||echo fail2); read; stty sane
>     0
>     fail
> 
>     success
>     $
> 
> Repeat-By (2):
> 
>   This demonstrates the problem more explicitly by system calls.
> 
>   1. Open cygwin console (pseudo console mode)
> 
>   2. Compile and run the attached program `min1.cpp'.
> 
>     $ g++ -o min1.exe min1.cpp
>     $ ./min1
> 
>   The expected result is something like
> 
>     $ ./min1
>     CHR CHR CHR CHR CHR CHR CHR
>     $
> 
>   However, in the console, it prints EOF and exit.  Also the terminal
>   response CPR appears to be lost.
> 
>     $ ./min1
>     EOF
>     $

Thanks for the report. The minimum test case would be:

#include <sys/select.h>
#include <unistd.h>
#include <stdio.h>

int main()
{
  int fd1 = STDIN_FILENO, fd2;
  char c1 = 'A', c2 = 'B';
  int r1, r2;
  fd_set readfds;
  FD_ZERO(&readfds);
  FD_SET(fd1, &readfds);
  select(fd1+1, &readfds, NULL, NULL, NULL);

  fd2 = dup(fd1);

  r1 = read(fd1, &c1, 1);
  r2 = read(fd2, &c2, 1);
  printf("fd=%d: ret=%d (%c)\n", fd1, r1, c1);
  printf("fd=%d: ret=%d (%c)\n", fd2, r2, c2);
  return 0;
}

If "12<enter>" is typed in this test case, the expected result is:
12
fd=0: ret=1 (1)
fd=3: ret=1 (2)

However in the console of cygwin 3.1.2, it is:
12
fd=0: ret=1 (1)
fd=3: ret=0 (B)

That is, the chars typed before dup() cannot be read from new fd.
This occurs because the readahead buffer (rabuf) in the console
is newly created by dup(), and does not inherit from the parent.

This test case does not work as expected also in cygwin 3.0.7.

I will submit a patch for this issue, in which rabuf is shared
between all console instances in a process.

-- 
Takashi Yano <takashi.yano@nifty.ne.jp>

--
Problem reports:       http://cygwin.com/problems.html
FAQ:                   http://cygwin.com/faq/
Documentation:         http://cygwin.com/docs.html
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple



More information about the Cygwin mailing list