How to prevent SIGPIPEs (or handle them properly)

I have a small server program that accepts connections on a TCP or local UNIX socket, reads a simple command and (depending on the command) sends a reply.

The problem is that the client may have no interest in the answer and sometimes exits early. So writing to that socket will cause a SIGPIPE and make my server crash.

What’s the best practice to prevent the crash here? Is there a way to check if the other side of the line is still reading? (select() doesn’t seem to work here as it always says the socket is writable). Or should I just catch the SIGPIPE with a handler and ignore it?

10 Answers
10

Leave a Comment