Dear bash(1) team,
I have recently identified a failure mode in bash(1) scripts that has
plagued us with random process failures for over 20 years!
The read(1) built-in command has the -t option to specify a timeout on
the read and it appears that the TMOUT variable is also honoured as an
alternative if it is defined (and this is where I think there is an issue).
>From the bash(1) manual entry...
The time-out is applied if read(1) is reading from a terminal,
pipe or other special file.
Since TMOUT can be inheritted from a much 'higher' process a script has
no knowledge about whether it has been set or not nor whether the value
is appropriate for the script.
Because the read(1) can time-out if the input is from a pipe then the
following can randomly fail in the "while read l" command if the input
generation command takes longer than TMOUT seconds to geneate any output.
someCommandThatTakesManyManySecondsBeforeGeneratingAnyOutput | while
read l
do
echo "$(date) $l"
done
The issue can be demonstrated using...
sh -c "sleep 10 ; date" | while TMOUT=10 read l ; do echo l=$l ; done
If TMOUT is -gt 10 then the "echo l=" output is seen; if TMOUT is -lt
10 then the "echo l=" output is not seen and since TMOUT can be
specified by a parent process rather than on the command line then
random failures occur.
I can (and now do) 'unset TMOUT' where I know my scripts are
susceptible to this issue but would ask that you consider whether TMOUT
should ever be considered when reading from a pipe.
I would argue that read(1) from a pipe should not time-out unless the
code explicitly specifies a time-out with the -t option.
Regards
Martin Wheatley
-----------------------------------------------------------------------
Martin Wheatley
Solaris Systems Administrator (JET)
Computing Division
Providing services on behalf of United Kingdom Atomic Energy Authority
Culham Campus, Abingdon, OX14 3DB, UK
Tel. +44 1235 464784
Mob.
Email: [email protected]
-----------------------------------------------------------------------
The content of this email is confidential and intended for the
recipient specified in the message only. It is forbidden to share any
part of this message with any third party, without the written consent
of the sender. If you received this message by mistake, please reply to
this message and then delete it, so that we can ensure it does not
occur in the future.