This is the mail archive of the cygwin mailing list for the Cygwin project.

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: Spurious 'grep: writing output' in Cygwin 1.7.5-1 in certain cases

[adding bug-grep]

On 04/19/2010 03:19 PM, Matthew Kidd wrote:
> We are seeing grep emit 'grep: writing output' multiple times in
> certain cases where the output of grep is piped to another program.
> Specifically, we see it in the following cases:
>   grep thisandthat foo.txt | head
>   grep thisandthat foo.txt | less

Expected, if you are blocking SIGPIPE, although it might be improved a
bit.  Grep is telling you that it encountered a write failure due to
EPIPE, and is thus about to exit with non-zero status.  Did you paste
the full error message?  On Linux, I see:

grep: writing output: Broken pipe

with the errno being converted to a string to make it obvious.  If
cygwin is not printing the EPIPE errno string as part of the error
message that you are seeing, then that could be improved; it's hard to
tell from your report, though.

> There appear to be two 'grep: writing output' messages for each
> line grepped.

That is indeed annoying; but it is not cygwin-specific.  It would be
nicer to print a failure message exactly once in the case of EPIPE write
failure, rather than once per matching line.  Would you care to
contribute a patch?

> Not all pipe operations exhibit this behavior. For example with the
> same file, we do not see it in either of these cases:
>   grep thisandthat foo.txt | last
>   grep thisandthat foo.txt | wc

Also to be expected - it is a factor of how much data the kernel can
push through a pipe.  You only get EPIPE write failures if SIGPIPE is
being ignored, and if the writer (grep) tries to push more data into the
pipe after the reader is done.  As readers, last and wc consume the
entire stream, but head and less can exit early, in which case the
kernel pushes back to the writer.  Another thing to note; you will only
get the errors for large files; for smaller files, the entire file can
be written into the pipe before the reader exits.

> Moreover a non-piped grep does not exhibit this behavior:
>   grep thisandthat foo.txt

Of course, because there is no EPIPE write failures if stdout is not a pipe.

> Note: in this case 'grep: writing output' does not appear in
> out.txt; my guess is that they are being sent to STDERR when they
> do occur.

Yes, POSIX requires that error messages, such as diagnosis of write
failure due to EPIPE, goes to stderr.

By the way, if you don't like this message, then consider changing your
environment to not ignore SIGPIPE.  Then grep will silently die from a
signal, instead of noisily exiting with an error message.

Eric Blake    +1-801-349-2682
Libvirt virtualization library

Attachment: signature.asc
Description: OpenPGP digital signature

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]