bash sets O_NONBLOCK on pts

2019-10-02 Thread Matteo Croce
Configuration Information [Automatically generated, do not change]:
Machine: x86_64
OS: linux-gnu
Compiler: gcc
Compilation CFLAGS: -O2 -g -pipe -Wall -Werror=format-security
-Wp,-D_FORTIFY_SOURCE=2 -Wp,-D_GLIBCXX_ASSERTIONS -fexceptions
-fstack-protector-strong -grecord-gcc-switches
-specs=/usr/lib/rpm/redhat/redhat-hardened-cc1
-specs=/usr/lib/rpm/redhat/redhat-annobin-cc1 -m64 -mtune=generic
-fasynchronous-unwind-tables -fstack-clash-protection -fcf-protection
-Wno-parentheses -Wno-format-security
uname output: Linux mcroce-redhat 5.3.1-matteo #87 SMP Mon Sep 30
14:20:06 CEST 2019 x86_64 x86_64 x86_64 GNU/Linux
Machine Type: x86_64-redhat-linux-gnu

Bash Version: 5.0
Patch Level: 7
Release Status: release

Description:
Sometimes bash leaves the pts with O_NONBLOCK set, and all programs
reading from stdin will get an EAGAIN:

# cat
cat: -: Resource temporarily unavailable

# strace -e read cat
read(0, 0x7f2a70511000, 131072) = -1 EAGAIN (Resource
temporarily unavailable)
: Resource temporarily unavailable
+++ exited with 1 +++

Initially I thought it was a kernel bug, so I filed a bug on the
kernel bugzilla:

https://bugzilla.kernel.org/show_bug.cgi?id=201641

but then I tested with fcntl() that the stding had te flag set.

Repeat-By:
random after some usage

Fix:
`exec bash` resolves it

-- 
Matteo Croce
per aspera ad upstream




Re: bash sets O_NONBLOCK on pts

2019-10-02 Thread Matteo Croce
On Wed, Oct 2, 2019 at 4:28 PM Chet Ramey  wrote:
>
> On 10/2/19 8:27 AM, Matteo Croce wrote:
>
> > Bash Version: 5.0
> > Patch Level: 7
> > Release Status: release
> >
> > Description:
> > Sometimes bash leaves the pts with O_NONBLOCK set, and all programs
> > reading from stdin will get an EAGAIN:
>
> Without a way to reliably reproduce this, it's just a transient issue.
>

Well, it's not so uncommon, I had it a few times. Reading on internet
it seems that other users have it but don't notice it.
A symptom is an interactive tool like apt which exits at the first prompt.

-- 
Matteo Croce
per aspera ad upstream




Re: bash sets O_NONBLOCK on pts

2019-10-02 Thread Matteo Croce
On Wed, Oct 2, 2019 at 7:50 PM Andrew Church  wrote:
>
> >Well, it's not so uncommon, I had it a few times. Reading on internet
> >it seems that other users have it but don't notice it.
>
> The fault could be in some other program accessing the terminal.  Bash
> does not clear O_NONBLOCK on displaying a prompt, so if a previously
> executed program sets O_NONBLOCK on stdin and then exits, that state
> will remain until some other program unsets it.  For example:
>
> $ cat >foo.c
> #include 
> int main(void) {fcntl(0, F_SETFL, O_NONBLOCK); return 0;}
> ^D
> $ cc foo.c
> $ ./a.out
> $ cat
> cat: -: Resource temporarily unavailable
>
>   --Andrew Church
> http://achurch.org/

It seems to me that bash restores the flag, cat prints an error when not:

$ cat
$

the same is not true if running multiple commands:

$ ./foo; cat
cat: -: Resource temporarily unavailable
$

Why this different behaviour?

Regards,

--
Matteo Croce
per aspera ad upstream




Re: bash sets O_NONBLOCK on pts

2019-10-02 Thread Matteo Croce
On Wed, Oct 2, 2019 at 8:16 PM Andreas Schwab  wrote:
>
> On Okt 02 2019, Matteo Croce  wrote:
>
> > It seems to me that bash restores the flag, cat prints an error when not:
> >
> > $ cat
> > $
> >
> > the same is not true if running multiple commands:
> >
> > $ ./foo; cat
> > cat: -: Resource temporarily unavailable
> > $
> >
> > Why this different behaviour?
>
> Because it's reset immediately before reading the next line with
> readline.
>
> Andreas.
>
> --
> Andreas Schwab, sch...@linux-m68k.org
> GPG Key fingerprint = 7578 EB47 D4E5 4D69 2510  2552 DF73 E780 A9DA AEC1
> "And now for something completely different."

Exactly, while in my bug I can do any command I want without resetting it.
Only `exec bash` restores it.

-- 
Matteo Croce
per aspera ad upstream