Re: read -t0 may report that input is available where none is possible

2021-11-21 Thread Dale R. Worley
More to the essence:

$ read -t0 

read -t0 may report that input is available where none is possible

2021-11-20 Thread Mark March
 true | { sleep 1 ; read -rt0 ; echo $?; }

outputs 0. There can clearly be no data on the stdin of the second process in 
the pipeline.

bash(1) has this to say about read -t:

"If timeout is 0, readĀ  returnsĀ  immediately, without trying to read any data. 
The exit status is 0 if input is available on the specified file descriptor, 
non-zero otherwise."

Either bash considers EOF on the pipe "input", or this is a bug.

This was discussed 2 years ago 
https://lists.gnu.org/archive/html/bug-bash/2019-12/msg00076.html. Chet's 
conclusion then was that the detection of the presence of data on an fd is 
inherently racy. That is true, but still read -rt0 should probably not report 
"input is available" where there can be none. If the current behavior is 
intentional, consider making it clear in the man page. 

The implementation of input_avail() in Bash 5.x appears to just call select() 
on the fd, but select() reports an fd readable also on EOF. The relevant quote 
from man select(2):

"The file descriptors listed in readfds will be watched to see if characters 
become available for reading (more precisely, to see if a read will not block; 
in particular, a file descriptor is also ready on end-of-file)."

-Mark