Project

General

Profile

Bug #7091

"tail -f" hangs with pipelines

Added by Dan McDonald over 4 years ago. Updated over 4 years ago.

Status:
Rejected
Priority:
Normal
Category:
cmd - userland programs
Start date:
2016-06-09
Due date:
% Done:

0%

Estimated time:
Difficulty:
Medium
Tags:
needs-triage
Gerrit CR:

Description

Consider this toy script:

#!/sbin/sh

echo "green" 
sleep 5
echo "green" 
sleep 5
echo "green" 
sleep 5
echo "purple" 
sleep 5
echo "green" 
sleep 5
echo "green" 

Now run that in the background, sent to a file ("/tmp/foo"), and then run this pipeline:

tail -f /tmp/foo | grep purple | head -1

Ideally that will exit the second "purple" shows up, while the above script will continue to run for 10 more seconds.

Instead, the pipeline above hangs, and depending on the shell, either exits without output (sh), or hangs in this stack (bash, tcsh):

717:    tail -f /tmp/foo
 feeff347 portfs   (5, 4, 8047b28, 0, 0, 0)
 08052706 follow   (80656f0, 4, a, 0, 2, 0) + 364
 08053b09 main     (8047ccc, fef786e8, 8047d04, 8051a3f, 3, 8047d10) + 496
 08051a3f _start   (3, 8047de8, 8047ded, 8047df0, 0, 8047df9) + 83
#1

Updated by Dan McDonald over 4 years ago

Also, this fails where the output file is tmpfs (per the example), or ZFS (per the larger shell script I have which uncovered this).

#2

Updated by Dan McDonald over 4 years ago

You can kill a lingering tail(1) process without extraordinary means, which makes me wonder if the close() from the writing-to-file program isn't being propagated to tail(1)?

#3

Updated by Jon Strabala over 4 years ago

Dan,

I don't think this is an issue at all with 'tail' rather it is the way multi-stage pipelines work (and in some cases interaction with a terminal) on most if not all *NIX systems - refer to:

http://stackoverflow.com/questions/18972450/unix-linux-pipe-behavior-when-reading-process-terminates-before-writing-process

You will see the same standard behavior if you 'pipe' your shell script as follows without using tail:

./toy_script.sh | grep purple | head -1

Cheers,

Jon Strabala

#4

Updated by Dan McDonald over 4 years ago

  • Status changed from New to Rejected

Even my shell script has a note about this. Rejecting -- not a bug.

Also available in: Atom PDF