Couldn't this also be handled by writing the following?

   (ls /tmp > /dev/null 2>&1; echo $? >$TEErc; true) | tee -a junk 

Wouldn't that be sufficient to keep the writer of the pipeline open so
that tee would not close until after the echo had finished?  If that
works, then it looks better to me than a sleep (ugh!) or turning on
pipefail.  Also, it should be portable.

> -----Original Message-----
> From: [email protected] 
> [mailto:[email protected]] On Behalf Of 
> David Korn
> Sent: Monday, December 06, 2010 12:37 PM
> To: [email protected]
> Subject: Re: [ast-developers] ksh file io race condition
> 
> Subject: Re: [ast-developers] ksh file io race condition
> --------
> 
> > Hi,
> > 
> > we've found that ksh can fail to read file it should create 
> with previous comman
> > d. This occurs even when the file
> > creation is inside parentheses and the access occurs 
> outside the parentheses.
> > 
> > 
> > Reproducer:
> > 
> > #!/bin/ksh
> > 
> > n=0
> > TEErc=Trcfile
> > while [ $n -lt 1000 ]
> > do
> >     echo $n
> >     >$TEErc
> >     (ls /tmp > /dev/null 2>&1; echo $? >$TEErc) | tee -a junk
> >     rc=$(<$TEErc)
> >     if [ -z "$rc" ]
> >     then
> >             echo hit the bug, rc is empty
> >             ls -l $TEErc
> >             sync
> >             sleep 1
> >             echo after sleep and sync
> >             ls -l $TEErc
> >             exit
> >     fi
> >     rm $TEErc
> >     n=$(expr $n + 1)
> > done
> > rm junk
> 
> First of all, let me explain what causes this.
> 
> When you run a pipeline, the shell only waits for the last process
> to complete unless the pipefail option is on.
> 
> Thus, as soon as tee completes, the pipeline is complete and 
> the command
>       rc=$(<$TEErc)
> can run.
> 
> Now, the
>       echo $? >$TEErc
> causes the writer of the pipeline to close since it is no 
> longer needed.
> This in turn cause the redirection causes tee to complete and 
> there is now
> a race between the parent shell running
>       rc=$(<$TEErc)
> and the child contining after to redirection to run
>       echo $?
> 
> If you put a sleep before
>       rc=$(<$TEErc)
> 
> you will see the exit status in rc.
> 
> 
> Now, the standard does allow (but does not require) the shell to wait
> for all processes (the way pipefail) does with ksh93.
> 
> For example
>       sleep 2 | :
> can either complete immediately or in two seconds as far as 
> the standard
> is concerned.
> 
> What I have done for the next release of ksh93u is to modify 
> the shell so
> that it will not close the write end of the pipe when file 
> descriptor 1 is
> redirected on the left hand side.  This removes the race 
> conditon in this case.
> However, using set -o pipefail is a better solution but that is not
> in the standard.
> 
> I will consider changing the default behavior of pipefail to 
> be enabled in
> ksh93v.  I would like to know if any existing script break.
> 
> David Korn
> [email protected]
> _______________________________________________
> ast-developers mailing list
> [email protected]
> https://mailman.research.att.com/mailman/listinfo/ast-developers
> 
> 

_______________________________________________
ast-developers mailing list
[email protected]
https://mailman.research.att.com/mailman/listinfo/ast-developers

Reply via email to