Sockets in C: Error 141?

So, I've been working with sockets in C recently and encountered an issue I didn't easily find a solution. Hopefully this blog can end up on the top of the results because it's the post that helps others out.


So, the idea here is I have a service running that accepts connections for data to process. I have it configured to open a socket, then for each user that connects, it will create a thread and pass the client to that thread as it opens the socket for another potential connection. The thread handles the processing of data that passes for that connection and then terminates. Now, what happens when a client prematurely disconnects from the thread before it's finished? It would appear your application just dies, no "segmentation fault", no output, it just dies. You may notice it has exit code 141.


Turns out what really was happening was the service was sending itself signal 13, or SIGPIPE. If either the service or the client attempts to send data and the pipe is b0rk, it will send SIGPIPE and error out. You can suppress this with a flag.

ssize_t send(int sockfd, const void *buf, size_t len, int flags);

Just use MSG_NOSIGNAL as the [flags] parameter. send(2) will still return -1 and errno will still be set, but this makes a request to the application to not throw SIGPIPE. Also note, this is just a request; if the application is too busy to ignore this request, it will and the application will still get a SIGPIPE on occasion, so be sure to handle it with signal(2).


Popular posts from this blog

Setup and Install Monero(d) -- p2pool -- xmrig

Build xmrig on Linux

Git: Process