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?
You generally want to ignore the
SIGPIPEand handle the error directly in your code. This is because signal handlers in C have many restrictions on what they can do.The most portable way to do this is to set the
SIGPIPEhandler toSIG_IGN. This will prevent any socket or pipe write from causing aSIGPIPEsignal.To ignore the
SIGPIPEsignal, use the following code:If you’re using the
send()call, another option is to use theMSG_NOSIGNALoption, which will turn theSIGPIPEbehavior off on a per call basis. Note that not all operating systems support theMSG_NOSIGNALflag.Lastly, you may also want to consider the
SO_SIGNOPIPEsocket flag that can be set withsetsockopt()on some operating systems. This will preventSIGPIPEfrom being caused by writes just to the sockets it is set on.