bug-gawk
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [bug-gawk] Behavior of fflush with SIGPIPE on stdout [PATCH]


From: FERRIEUX Alexandre - IMT/OLN
Subject: Re: [bug-gawk] Behavior of fflush with SIGPIPE on stdout [PATCH]
Date: Sat, 25 Mar 2017 15:30:35 +0100
User-agent: Mozilla/5.0 (X11; Linux i686; rv:8.0) Gecko/20111113 Thunderbird/8.0

On 25/03/2017 15:04, Andrew J. Schorr wrote:
On Sat, Mar 25, 2017 at 02:50:19PM +0100, FERRIEUX Alexandre - IMT/OLN wrote:
 >$ uname -a
 >CYGWIN_NT-10.0 ti47 2.5.0(0.297/5/3) 2016-04-11 09:58 x86_64 Cygwin
 >
 >$ false | head -1; echo $?
 >1
 >
 >$ cat /etc/services | head -1 ; echo $?
 ># Copyright (c) 1993-2004 Microsoft Corp.
 >0
 >
 >I don't know why that's the case.

 You forgot "set -o pipefail".

I did not forget. If I had forgotten, the example with "false" above
would not have worked:

$ false | head -1; echo $?
0

Ah yes, sorry :)


$ set -o pipefail

$ false | head -1; echo $?
1

$ cat /etc/services | head -1; echo $?
# Copyright (c) 1993-2004 Microsoft Corp.
0

Yes, that may happen if the output from "cat" fits within a pipe buffer: a single write() completes without blocking ; then "head" exits after reading part or all of it (depending on stdin buffer settings), but it's way too late for any feedback like sending a SIGPIPE, since nobody's writing at the time.

Use the command "yes" instead of "cat /etc/services". It never stops.

 Here is a variant that doesn't need it:

   (cat /etc/services;echo $?>&2) | head -1
   # /etc/services:
   141

Strangely, that does not work on my Cygwin system:

$ (cat /etc/services;echo $?>&2) | head -1
0
# Copyright (c) 1993-2004 Microsoft Corp.

I guess I may be experiencing a Cygwin bug of some sort. Did you actually
test on Cygwin?

Nope, I don't have any Windows. But I tend to hope that the development of such a venerable pillar of the unix universe as Awk, will not take Windows (even Cygwin) as a reference. Windows may not get what a file descriptor or exit status means, we still need gawk to get it right on Unix.

You may wonder why the detailed exit status matters.
First, of course, a script may inspect $? and act accordingly.
But a much more prevalent use is the following:
        - I'm debugging a big contraption made of dozens of pipe-connected 
processes (not necessarily written by me)
        - half of them are Awk processes
        - At some point, things go awry, some of the processes exiting 
prematurely, and the others following by starvation.

In this situation, I need to understand why each one died: EOF, SIGPIPE, or 
intentional exit ?

To this effect, "strace -f" gives all the answers in one single command. This is true of all unix filters that use the default SIGPIPE behavior (without even setting a handler): cut, sed, grep, old awk. If this stops being true for shiny gawk for no better reason than "Windows people don't need it", that's tough.







reply via email to

[Prev in Thread] Current Thread [Next in Thread]