Re: ``read -N n -t timeout'' saves partial input only when EOF is seen?
On 11/27/16 3:47 PM, Dmitry Goncharov wrote: > On Fri, Nov 25, 2016 at 12:40:58PM -0500, Chet Ramey wrote: >> On 11/25/16 9:57 AM, Dmitry Goncharov wrote: >>> Auto variables have unspecified values after a call to longjmp. >> >> This is true. It's what the USE_VAR macro is intended to defeat, but let's >> see what marking i volatile does. > > Chet, can you please tell why sigalarm/longjmp was preferred over select? Well, 17 years ago when that code went into `read', SIGALRM was more portable and fit better with the existing code base. Chet -- ``The lyf so short, the craft so long to lerne.'' - Chaucer ``Ars longa, vita brevis'' - Hippocrates Chet Ramey, UTech, CWRUc...@case.eduhttp://cnswww.cns.cwru.edu/~chet/
Re: ``read -N n -t timeout'' saves partial input only when EOF is seen?
On 11/25/16 9:57 AM, Dmitry Goncharov wrote: > Auto variables have unspecified values after a call to longjmp. This is true. It's what the USE_VAR macro is intended to defeat, but let's see what marking i volatile does. Chet -- ``The lyf so short, the craft so long to lerne.'' - Chaucer ``Ars longa, vita brevis'' - Hippocrates Chet Ramey, UTech, CWRUc...@case.eduhttp://cnswww.cns.cwru.edu/~chet/
Re: ``read -N n -t timeout'' saves partial input only when EOF is seen?
Auto variables have unspecified values after a call to longjmp. This patch fixes the bug. regards, Dmitry diff --git a/builtins/read.def b/builtins/read.def index 5e2348c..938b62a 100644 --- a/builtins/read.def +++ b/builtins/read.def @@ -181,7 +181,8 @@ read_builtin (list) WORD_LIST *list; { register char *varname; - int size, i, nr, pass_next, saw_escape, eof, opt, retval, code, print_ps2; + int size, nr, pass_next, saw_escape, eof, opt, retval, code, print_ps2; + volatile int i; int input_is_tty, input_is_pipe, unbuffered_read, skip_ctlesc, skip_ctlnul; int raw, edit, nchars, silent, have_timeout, ignore_delim, fd, lastsig, t_errno; unsigned int tmsec, tmusec; On Thu, Nov 24, 2016 at 10:32 PM, Clark Wang wrote: > On Fri, Nov 25, 2016 at 7:05 AM, L A Walsh wrote: > >> >> >> isabella parakiss wrote: >> >>> that's not true https://gist.github.com/fa4efd90376ff2714901e4429fdee734 >>> read successfully reads the data, but then it's discarded by bash >>> >>> >> >>It's discarded by bash because the read doesn't read 10 >> characters within the time limit. If you use -N 5, you get >> your output. "-t" says it will timeout and return failure if a >> specified number of characters is not read within timeout period. >> > > The manual for -t says: > > If read times out, read saves any partial input read into the specified > variable *name*. > > -clark > > > If timeout is exceeded, then return status is > 128: >> >> bash -c '( printf 12345; sleep 2 ) | ( read -t 1 -N 10 v; echo "s=$?, >> <$v>" )' >> s=142, <> >> >> (status is > 128) >> >> >>> >> >
Re: ``read -N n -t timeout'' saves partial input only when EOF is seen?
On Fri, Nov 25, 2016 at 7:05 AM, L A Walsh wrote: > > > isabella parakiss wrote: > >> that's not true https://gist.github.com/fa4efd90376ff2714901e4429fdee734 >> read successfully reads the data, but then it's discarded by bash >> >> > >It's discarded by bash because the read doesn't read 10 > characters within the time limit. If you use -N 5, you get > your output. "-t" says it will timeout and return failure if a > specified number of characters is not read within timeout period. > The manual for -t says: If read times out, read saves any partial input read into the specified variable *name*. -clark If timeout is exceeded, then return status is > 128: > > bash -c '( printf 12345; sleep 2 ) | ( read -t 1 -N 10 v; echo "s=$?, > <$v>" )' > s=142, <> > > (status is > 128) > > >> >
Re: ``read -N n -t timeout'' saves partial input only when EOF is seen?
isabella parakiss wrote: that's not true https://gist.github.com/fa4efd90376ff2714901e4429fdee734 read successfully reads the data, but then it's discarded by bash It's discarded by bash because the read doesn't read 10 characters within the time limit. If you use -N 5, you get your output. "-t" says it will timeout and return failure if a specified number of characters is not read within timeout period. If timeout is exceeded, then return status is > 128: bash -c '( printf 12345; sleep 2 ) | ( read -t 1 -N 10 v; echo "s=$?, <$v>" )' s=142, <> (status is > 128)
Re: ``read -N n -t timeout'' saves partial input only when EOF is seen?
On 11/24/16, Chet Ramey wrote: > On 11/24/16 2:57 AM, Clark Wang wrote: >> See following example: >> >> # echo $BASH_VERSION >> 4.4.5(2)-release >> # ( printf 12345 ) | ( read -t 1 -N 10 v; echo "<$v>" ) >> <12345> >> # ( printf 12345; sleep 2 ) | ( read -t 1 -N 10 v; echo "<$v>" ) >> <> >> # >> >> The second "read" did not save "12345" to "v". Is this a bug? > > No, it's a race condition. It depends on when the kernel schedules > each process in the pipeline: if the read gets scheduled and times out > before the subshell that runs the printf/sleep combination runs and > writes something to the pipe, you won't get any output. On Mac OS X, > for example, you get <12345>. > > Chet > -- > ``The lyf so short, the craft so long to lerne.'' - Chaucer >``Ars longa, vita brevis'' - Hippocrates > Chet Ramey, UTech, CWRUc...@case.edu > http://cnswww.cns.cwru.edu/~chet/ > > that's not true https://gist.github.com/fa4efd90376ff2714901e4429fdee734 read successfully reads the data, but then it's discarded by bash i don't know how/why it works on your mac, but it sure looks like a bug --- xoxo iza
Re: ``read -N n -t timeout'' saves partial input only when EOF is seen?
On 11/24/16 2:57 AM, Clark Wang wrote: > See following example: > > # echo $BASH_VERSION > 4.4.5(2)-release > # ( printf 12345 ) | ( read -t 1 -N 10 v; echo "<$v>" ) > <12345> > # ( printf 12345; sleep 2 ) | ( read -t 1 -N 10 v; echo "<$v>" ) > <> > # > > The second "read" did not save "12345" to "v". Is this a bug? No, it's a race condition. It depends on when the kernel schedules each process in the pipeline: if the read gets scheduled and times out before the subshell that runs the printf/sleep combination runs and writes something to the pipe, you won't get any output. On Mac OS X, for example, you get <12345>. Chet -- ``The lyf so short, the craft so long to lerne.'' - Chaucer ``Ars longa, vita brevis'' - Hippocrates Chet Ramey, UTech, CWRUc...@case.eduhttp://cnswww.cns.cwru.edu/~chet/
``read -N n -t timeout'' saves partial input only when EOF is seen?
See following example: # echo $BASH_VERSION 4.4.5(2)-release # ( printf 12345 ) | ( read -t 1 -N 10 v; echo "<$v>" ) <12345> # ( printf 12345; sleep 2 ) | ( read -t 1 -N 10 v; echo "<$v>" ) <> # The second "read" did not save "12345" to "v". Is this a bug? -clark