Re: IOError: [Errno 32] Broken pipe

From: Donn Cave (donn_at_u.washington.edu)
Date: 06/29/04


Date: Mon, 28 Jun 2004 17:13:12 -0700

In article <Pine.LNX.4.44.0406281844290.30693-100000@ccc2.wpi.edu>,
 Christopher T King <squirrel@WPI.EDU> wrote:

> On 28 Jun 2004, Jay Donnell wrote:
>
> > I'm working on a simple script to manipulate csv files. Right now it
> > just prints the first field of the file for each line. Everything
> > works fine, but if I use 'head' or 'more' and quit while in more then
> > I get
> > IOError: [Errno 32] Broken pipe
> >
> > Anyone know why this is happening?
>
> That's normal, at least with Unix. When the program on the receiving end
> of a pipe decides to close its end for some reason, Unix sends the signal
> 'SIGPIPE' to the sending end. Python catches this and turns it into an
> IOError exception. The only way around this (that I can think of) is to
> catch the exception and exit the program gracefully. If you try to send
> more data, you will get more IOErrors, since your program has nowhere left
> to send data.

Actually the problem is not that Python catches SIGPIPE, but
rather that it ignores it - as in, signal(SIGPIPE, SIG_IGN)
Then the write returns an error 32 EPIPE, which naturally
turns into an exception.

To restore normal UNIX behavior,

   import signal

   signal.signal(signal.SIGPIPE, signal.SIG_DFL)

And also do that after any instantiation of a socket object,
because it happens there too.

   Donn Cave, donn@u.washington.edu