From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 18611 invoked by alias); 16 Jan 2013 02:54:23 -0000 Mailing-List: contact zsh-workers-help@zsh.org; run by ezmlm Precedence: bulk X-No-Archive: yes List-Id: Zsh Workers List List-Post: List-Help: X-Seq: 30974 Received: (qmail 28753 invoked from network); 16 Jan 2013 02:54:08 -0000 X-Spam-Checker-Version: SpamAssassin 3.3.2 (2011-06-06) on f.primenet.com.au X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,RCVD_IN_DNSWL_NONE autolearn=ham version=3.3.2 Received-SPF: none (ns1.primenet.com.au: domain at closedmail.com does not designate permitted sender hosts) From: Bart Schaefer Message-id: <130115185341.ZM18422@torch.brasslantern.com> Date: Tue, 15 Jan 2013 18:53:41 -0800 In-reply-to: <87r4lmlae4.fsf@gmail.com> Comments: In reply to Christian Neukirchen "SIGPIPE echoing to fifo exits zsh" (Jan 15, 4:43pm) References: <87r4lmlae4.fsf@gmail.com> <20130115231654.GB2518@localhost.localdomain> In-reply-to: <20130115231654.GB2518@localhost.localdomain> Comments: In reply to Han Pingtian "Re: SIGPIPE echoing to fifo exits zsh" (Jan 16, 7:16am) X-Mailer: OpenZMail Classic (0.9.2 24April2005) To: zsh-workers@zsh.org Subject: Re: SIGPIPE echoing to fifo exits zsh MIME-version: 1.0 Content-type: text/plain; charset=us-ascii On Jan 15, 4:43pm, Christian Neukirchen wrote: } } juno% repeat 1000 echo a >/tmp/fifo } } Program received signal SIGPIPE, Broken pipe. } } Using /bin/echo (coreutils 8.20), the code works flawlessy (all lines } get through the FIFO). On Jan 16, 7:16am, Han Pingtian wrote: } Subject: Re: SIGPIPE echoing to fifo exits zsh } } Looks like the crash occurs at this line in bin_print(): } } 4084 /* Testing EBADF special-cases >&- redirections */ } 4085 if ((fout != stdout) ? (fclose(fout) != 0) : } 4086 (fflush(fout) != 0 && errno != EBADF)) { Hrm. If this were a script instead of an interactive command at the prompt, the correct behavior would in fact be to exit on SIGPIPE. I don't know offhand if that's also (per POSIX spec for example) correct in this instance. You can prevent the shell from exiting by simply blocking the SIGPIPE signal: trap '' PIPE As for why the builtin echo gets a SIGPIPE and /bin/echo does not ... there's a race condition involved. With the trap above in place, if you try a=0 repeat 100 echo $((++a)) > /tmp/fifo you'll probably see something like this on the reading end: 1 5 14 24 34 43 51 60 70 80 89 97 100 What's happening is that the the repeat loop writes a bunch of data to the pipe (several executions of "echo") before "head -1" wakes up and reads it. Then "head" exits, closing the pipe, which SIGPIPE's one of the echo commands. A new "head" then re-opens the pipe so another few passes of the loop go by before there's another exit+SIGPIPE event. With /bin/echo it takes about the same amount of time for each of the two loops to spawn their external processes, so the race is much less likely to happen. If you put a "sleep 1" or really almost any delay into the repeat loop -- or use a single command like "cat" that doesn't repeatedly open/close the read end of the pipe -- you'll see all the lines get through even with the builtin echo. You can see this even more glaringly by using while :; do head -1 ; done < /tmp/fifo as the reading end; with that, each run of "head" will consume multiple lines of the output from the writing end, discard all but the first line each time, and produce output similar to 1 53 83 without ever triggering the SIGPIPE on the write side. In short, when using FIFOs, synchronizing the amount of data written/read by each side is the programmer's job; commands like "head" are not meant to work that way.