Wednesday, June 08, 2022

ksh (and bash) dying with SIGPIPE when using echo builtin command

Hi,

Try:

$ mkfifo fifo
$ while true; do /bin/echo aaaa > fifo; echo -n "$? "; done

then in another shell:

$ tail -f fifo

and everything works as expected. Now repeatedly interrupt and restart
this tail(1) command and from time to time you'll see that /bin/echo dies
with status 141 because of a SIGPIPE signal. My guess is that this happens
when /bin/echo opens the FIFO and tail(1) then closes it (when it's
interrupted) before /bin/echo has had the chance to do the write so the
write then gets a SIGPIPE. So far so good.

Now replace /bin/echo with ksh's builtin echo command:

$ while true; do echo aaaa > fifo; echo -n "$? "; done

and again repeatedly start / interrupt / restart the tail(1) command above.
The builtin echo command is faster than /bin/echo so it might take a few
more tries but sooner or later the builtin echo command gets a SIGPIPE and
then ksh itself dies, which is... unexpected.

I tried with bash and the behavior is the same as with ksh.

I tried with zsh and I get "echo: write error: broken pipe" and zsh doesn't
die, which is what I would have expected from ksh (and bash).

So that looks to me like a bug in ksh (and bash).

Cheers,

Philippe

No comments:

Post a Comment