Project

General

Profile

Bug #11688

Improper behaviour of shell redirections combined with "tee"

Added by Hubert Garavel about 1 year ago. Updated about 1 year ago.

Status:
New
Priority:
Normal
Assignee:
-
Category:
-
Start date:
Due date:
% Done:

0%

Estimated time:
Difficulty:
Medium
Tags:
Gerrit CR:

Description

We observed that, on Illumos/OpenIndiana, some output lines disappear when
shell redirections are combined with "tee".

This problem does not occur on Solaris 10 nor on Linux.

The following shell script allows to reproduce the issue. It writes 100 lines
on the standard output, but after redirections and "tee", some lines may be
randomly lost, i.e., one sees only 98, 96, 95, 94 (etc.) lines rather than 100.

#! /bin/sh

# the bug does not occur if #! /bin/sh is replaced by #! /bin/bash
# the bug does not occur if #! /bin/sh is replaced by #! /bin/ksh
# the bug does not occur if #! /bin/sh is replaced by #! /bin/ksh93

RUN() {
# this function echoes all numbers from 1 to 100 on standard output
# the bug also occurs if function RUN is replaced by a separate shell script
N=1
while [ $N -le 100 ]
do
    echo "$N" | tee 
    # the "tee" command must be present for the bug to be observed
    # the bug also occurs if "tee" is replaced by "tee filename" 
    # the bug also occurs if "tee" is replaced by "tee -a filename" 
    N=`expr $N + 1`
done
}

P=1
while [ $P -le 20 ]
do
    ( RUN 2>&1 ) > aux ; wc -l aux
    rm aux
    P=`expr $P + 1`
done

The normal output (as observed on Linux or Solaris 10) consists of 20 lines
containing "100 aux". On Illumos, the output can be the following:

      98 aux
      96 aux
      94 aux
      93 aux
      94 aux
      96 aux
      93 aux
      95 aux
      96 aux
      93 aux
      95 aux
      97 aux
      99 aux
      96 aux
      97 aux
      98 aux
     100 aux
      96 aux
      97 aux
     100 aux

Frédéric Lang and Hubert Garavel

History

#1

Updated by Hubert Garavel about 1 year ago

Further observations:

The bug disappears if

    ( RUN 2>&1 ) > aux ; wc -l aux

is replaced by
    RUN 2>&1 > aux ; wc -l aux

or
    ( RUN 2>&1 ) | wc -l

or
    RUN 2>&1 | wc -l

#2

Updated by Hubert Garavel about 1 year ago

The bug also disappears if

     ( RUN 2>&1 ) > aux ; wc -l aux

is replaced by
     ( RUN ) > aux ; wc -l aux

#3

Updated by Hubert Garavel about 1 year ago

Erratum: in the above bug report, the two lines

# the bug does not occur if #! /bin/sh is replaced by #! /bin/ksh
# the bug does not occur if #! /bin/sh is replaced by #! /bin/ksh93

are incorrect. Deeper experiments have shown that the problem
occurs with ksh and ksh93.

However, it does not occur with bash. So, the bug seems inside
ksh, ksh93, and /bin/sh (which links to /bin/i86/ksh93).

#4

Updated by Hubert Garavel about 1 year ago

The bug also occurs if

    echo "$N" | tee

is replaced with

    printf "%d\n" "$N" | tee

However, the bug disappears if "echo" is replaced with "/bin/echo"
(respectively, if "printf" is replaced with "/bin/printf").

This suggests that the bug lies in the built-in implementations of "echo" and "printf".

The fact that the bug occurs randomly suggests that it could very well be a race condition.

Frédéric Lang and Hubert Garavel

Also available in: Atom PDF