Does any of the TCP Settings impact SIGPIPE?
Question
I tested the same testcode (Server - Client) on two systems. In one I am getting a SIGPIPE, in another I am not getting SIGPIPE.
The Test Scenario is something like below:
Client
- Connect to a Server
- Receive Data from Server
- Send Data
- Close the Socket
Server
- Accept Connection from a Server
- Send Data to Client
- Receive Data
- Send Data
- Close the Socket
In one system (Client & Server running on the same system), SIGPIPE is happening randomly on the client or server side.
But, in another system, this problem is not happening with the same test code.
I wonder if broken pipe has something to do with TCP Settings.
Solution
You can also ignore SIGPIPE from socket for prevent application crash by this code:
int set = 1;
setsockopt(sd, SOL_SOCKET, SO_NOSIGPIPE, (void *)&set, sizeof(int));
Where sd is socket where you receiving SIGPIPEs.
OTHER TIPS
The SIGPIPE
signal is raised if you try to write to a socket that the other end has already closed. There is a race condition here, if one end closes the socket around the same time as the other end attempts to send data - the close notification might be received before or after the send, which is why the SIGPIPE
is occuring in one test environment and not the other.
Generally, socket-aware applications should ignore SIGPIPE
, and instead synchronously handle the resulting EPIPE
error from send()
.