* SIGPIPE echoing to fifo exits zsh
@ 2013-01-15 15:43 Christian Neukirchen
2013-01-15 23:16 ` Han Pingtian
0 siblings, 1 reply; 5+ messages in thread
From: Christian Neukirchen @ 2013-01-15 15:43 UTC (permalink / raw)
To: zsh-workers
Hi,
I noticed this thing:
shell1% mkfifo /tmp/fifo
shell1% while :; do head -1 /tmp/fifo; done
shell2% gdb zsh
(gdb) r -f
juno% repeat 1000 echo a >/tmp/fifo
Program received signal SIGPIPE, Broken pipe.
0x00007ffff71a7100 in __write_nocancel () from /usr/lib/libc.so.6
(gdb) bt
#0 0x00007ffff71a7100 in __write_nocancel () from /usr/lib/libc.so.6
#1 0x00007ffff71410d3 in _IO_new_file_write () from /usr/lib/libc.so.6
#2 0x00007ffff7140fb2 in new_do_write () from /usr/lib/libc.so.6
#3 0x00007ffff71422c5 in __GI__IO_do_write () from /usr/lib/libc.so.6
#4 0x00007ffff7141680 in __GI__IO_file_sync () from /usr/lib/libc.so.6
#5 0x00007ffff713654b in fflush () from /usr/lib/libc.so.6
#6 0x000000000041a2dd in bin_print ()
#7 0x000000000041d117 in execbuiltin ()
#8 0x000000000042b2d1 in ?? ()
#9 0x000000000042b866 in ?? ()
#10 0x000000000042bdaf in ?? ()
#11 0x000000000042cf5f in execlist ()
#12 0x000000000044c354 in execfor ()
#13 0x000000000042a73b in ?? ()
#14 0x000000000042b866 in ?? ()
#15 0x000000000042bdaf in ?? ()
#16 0x000000000042cf5f in execlist ()
#17 0x000000000042d47d in execode ()
#18 0x000000000043d972 in loop ()
#19 0x0000000000440b1e in zsh_main ()
#20 0x00007ffff70eca15 in __libc_start_main () from /usr/lib/libc.so.6
#21 0x000000000040f3d1 in _start ()
Using /bin/echo (coreutils 8.20), the code works flawlessy (all lines
get through the FIFO).
Thanks,
--
Christian Neukirchen <chneukirchen@gmail.com> http://chneukirchen.org
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: SIGPIPE echoing to fifo exits zsh
2013-01-15 15:43 SIGPIPE echoing to fifo exits zsh Christian Neukirchen
@ 2013-01-15 23:16 ` Han Pingtian
2013-01-16 2:53 ` Bart Schaefer
0 siblings, 1 reply; 5+ messages in thread
From: Han Pingtian @ 2013-01-15 23:16 UTC (permalink / raw)
To: zsh-workers
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)) {
On Tue, Jan 15, 2013 at 04:43:31PM +0100, Christian Neukirchen wrote:
> Hi,
>
> I noticed this thing:
>
> shell1% mkfifo /tmp/fifo
> shell1% while :; do head -1 /tmp/fifo; done
>
> shell2% gdb zsh
> (gdb) r -f
> juno% repeat 1000 echo a >/tmp/fifo
>
> Program received signal SIGPIPE, Broken pipe.
> 0x00007ffff71a7100 in __write_nocancel () from /usr/lib/libc.so.6
> (gdb) bt
> #0 0x00007ffff71a7100 in __write_nocancel () from /usr/lib/libc.so.6
> #1 0x00007ffff71410d3 in _IO_new_file_write () from /usr/lib/libc.so.6
> #2 0x00007ffff7140fb2 in new_do_write () from /usr/lib/libc.so.6
> #3 0x00007ffff71422c5 in __GI__IO_do_write () from /usr/lib/libc.so.6
> #4 0x00007ffff7141680 in __GI__IO_file_sync () from /usr/lib/libc.so.6
> #5 0x00007ffff713654b in fflush () from /usr/lib/libc.so.6
> #6 0x000000000041a2dd in bin_print ()
> #7 0x000000000041d117 in execbuiltin ()
> #8 0x000000000042b2d1 in ?? ()
> #9 0x000000000042b866 in ?? ()
> #10 0x000000000042bdaf in ?? ()
> #11 0x000000000042cf5f in execlist ()
> #12 0x000000000044c354 in execfor ()
> #13 0x000000000042a73b in ?? ()
> #14 0x000000000042b866 in ?? ()
> #15 0x000000000042bdaf in ?? ()
> #16 0x000000000042cf5f in execlist ()
> #17 0x000000000042d47d in execode ()
> #18 0x000000000043d972 in loop ()
> #19 0x0000000000440b1e in zsh_main ()
> #20 0x00007ffff70eca15 in __libc_start_main () from /usr/lib/libc.so.6
> #21 0x000000000040f3d1 in _start ()
>
> Using /bin/echo (coreutils 8.20), the code works flawlessy (all lines
> get through the FIFO).
>
> Thanks,
> --
> Christian Neukirchen <chneukirchen@gmail.com> http://chneukirchen.org
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: SIGPIPE echoing to fifo exits zsh
2013-01-15 23:16 ` Han Pingtian
@ 2013-01-16 2:53 ` Bart Schaefer
2013-01-16 9:30 ` Han Pingtian
2013-01-16 9:44 ` Peter Stephenson
0 siblings, 2 replies; 5+ messages in thread
From: Bart Schaefer @ 2013-01-16 2:53 UTC (permalink / raw)
To: zsh-workers
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.
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: SIGPIPE echoing to fifo exits zsh
2013-01-16 2:53 ` Bart Schaefer
@ 2013-01-16 9:30 ` Han Pingtian
2013-01-16 9:44 ` Peter Stephenson
1 sibling, 0 replies; 5+ messages in thread
From: Han Pingtian @ 2013-01-16 9:30 UTC (permalink / raw)
To: zsh-workers
On Tue, Jan 15, 2013 at 06:53:41PM -0800, Bart Schaefer wrote:
> 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.
Thanks so much, Bart.
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: SIGPIPE echoing to fifo exits zsh
2013-01-16 2:53 ` Bart Schaefer
2013-01-16 9:30 ` Han Pingtian
@ 2013-01-16 9:44 ` Peter Stephenson
1 sibling, 0 replies; 5+ messages in thread
From: Peter Stephenson @ 2013-01-16 9:44 UTC (permalink / raw)
To: zsh-workers
On Tue, 15 Jan 2013 18:53:41 -0800
Bart Schaefer <schaefer@brasslantern.com> wrote:
> 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.
I tried a few shells and got a roughly 50-50 split. As far as I could
make out, in fact, bash behaved differently depending whether or not
invoked as "sh", but I haven't looked further.
pws
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2013-01-16 9:45 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2013-01-15 15:43 SIGPIPE echoing to fifo exits zsh Christian Neukirchen
2013-01-15 23:16 ` Han Pingtian
2013-01-16 2:53 ` Bart Schaefer
2013-01-16 9:30 ` Han Pingtian
2013-01-16 9:44 ` Peter Stephenson
Code repositories for project(s) associated with this public inbox
https://git.vuxu.org/mirror/zsh/
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).