Difference: Your large program uses $SIG{ PIPE } = "IGNORE";.

Solution: Add local $SIG{ PIPE } = "DEFAULT"; in scope of your open. Alternatively, you could redirect zcat's STDERR to /dev/null.


Take a look at this:

seq 1000000000 | gzip | zcat | head

It should take a long time to generate and unzip that stream, but it doesn't.

$ time ( seq 1000000000 | gzip | zcat | head ) 1 2 3 4 5 6 7 8 9 10 real 0m0.051s user 0m0.056s sys 0m0.005s

This is what happens:

  1. Once head has printed its ten lines, it exits.
  2. This breaks the pipe from zcat to head.
  3. The next time zcat attempts to write to the pipe, SIGPIPE is sent to it.
  4. Upon receiving SIGPIPE, zcat is killed.
  5. gzip is similarly killed by SIGPIPE.
  6. seq is similarly killed by SIGPIPE.

The above is what happens in your standalone script.

In your larger program, you appear to have used $SIG{ PIPE } = "IGNORE";. It's still just as quick, but the error message now appears.

$ seq 1000000000 | gzip | perl -e'$SIG{ PIPE } = "IGNORE"; exec("zcat" +)' | head 1 2 3 4 5 6 7 8 9 10 gzip: stdout: Broken pipe

This is what happens now:

  1. zcat inherits its parent's disposition of ignoring SIGPIPE.
  2. Once head has printed its ten lines, it exits.
  3. This breaks the pipe from zcat to head.
  4. The next time zcat attempts to write to the pipe, SIGPIPE is sent to it.
  5. zcat ignores the SIGPIPE.
  6. The write call returns error EPIPE.
  7. zcat exits with a message as a result of the error. (zcat is an alias for gzip, which is why the message says gzip.)
  8. gzip is killed by SIGPIPE.
  9. seq is killed by SIGPIPE.

Add local $SIG{ PIPE } = "DEFAULT"; in scope of your open.


In reply to Re: zcat pipe gives "gzip: stdout: Broken pipe" error by ikegami
in thread zcat pipe gives "gzip: stdout: Broken pipe" error by Special_K

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.