Problems with flushing stdin in mintty

Takashi Yano takashi.yano@nifty.ne.jp
Sun Jul 13 11:34:56 GMT 2025


On Sun, 13 Jul 2025 11:40:10 +0200
Christoph Reiter wrote:
> I'm having the problem that under cygwin flushing stdin for some reason doesn't
> work when running in mintty. It works when running cygwin in the Windows
> terminal though. In my case the program using this is pacman, where it asks
> various questions and if you hit some keys in-between questions they leak into
> the next one, resulting in invalid input.
> 
> Example program showing the problem. Type some text in the first 5 seconds, and
> see the input leak into the next input. Any ideas/workarounds welcome.
> 
> ```c
> // gcc -o flush_stdin flush_stdin.c
> #include <stdio.h>
> #include <unistd.h>
> #include <termios.h>
> 
> int main() {
>     printf("type something in the next 5 secs\n");
>     for (int i=0; i < 5; i++) {
>         sleep(1);
>         printf("%d\n", i);
>     }
> 
>     // Flush stdin
>     int fd = fileno(stdin);
>     if (fd != -1) {
>         printf("flushing stdin\n");
>         if (tcflush(fd, TCIFLUSH) == -1) {  // this doesn't seem to do anything
>             perror("Error flushing stdin");
>             return 1;
>         }
>     } else {
>         perror("Error getting file descriptor for stdin");
>         return 1;
>     }
> 
>     char buffer[100];
>     printf("Enter some text:\n");
>     fgets(buffer, sizeof(buffer), stdin);
>     printf("You entered: %s\n", buffer);
> 
>     return 0;
> }
> ```

Thanks for the report.

This seems to be a bug of pty code. If you enter 'return' key
in the first 5 sec, the input will be flushed.

I'll look into that.

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


More information about the Cygwin mailing list