Prevent SIGPIPE from Terminating a Linux C Server with MSG_NOSIGNAL
Reproduce SIGPIPE termination on a closed stream socket, compare MSG_NOSIGNAL with process-wide ignoring, and handle EPIPE without confusing signal suppression with connection recovery.
If a Linux C server exits while sending to a disconnected socket, checking send for errors may not be enough. Add MSG_NOSIGNAL to sends on sockets you directly manage, then handle the returned error as a connection failure to avoid process termination from SIGPIPE. The example below compares default behavior, per-call suppression, and process-wide ignoring in actual child processes.
Conceptual illustration of a server continuing to handle other connections after one socket disconnects; not a network screenshot or measurement.
Written and documentation checked: December 4, 2026. Execution environment: Linux/amd64, GCC 14.2.0. This article covers direct C calls on stream sockets. It does not validate recovery across an entire TCP or TLS library stack.
Why the server can exit before logging an error
Consider a server that logs inside if (n == -1) after sending a response, yet disappears without that log. The Linux signal documentation lists process termination as the default action for SIGPIPE. Other threads handling requests are affected by that process termination too. The existence of a return-value check does not make this path safe.
The send documentation for MSG_NOSIGNAL states that suppressing the signal still leaves the EPIPE error. The flag does not restore the connection. It lets the failure return to your control flow.
| Policy | Scope | Result in this example | Selection consideration |
| Default disposition + flags=0 | No explicit suppression | Child terminates from SIGPIPE | Error-handling code is not reached |
| MSG_NOSIGNAL | That send call | -1, EPIPE | Apply at send sites you directly control |
| SIG_IGN through sigaction | Process SIGPIPE disposition | -1, EPIPE | The policy also applies to other modules and threads |
The signal documentation also specifies that dispositions are process-wide and shared by threads. This is a reason to first consider limiting the change to owned send calls rather than having a library alter its host process's policy for its own convenience.
Reproduce a one-byte send to a closed peer
To remove network disconnect timing, socketpair creates two connected AF_UNIX stream sockets. The peer endpoint is closed before fork, leaving no copy of that endpoint in any process. Moving the close after fork while retaining a copy may prevent the same closed-peer condition.
Each child explicitly sets the SIGPIPE disposition and unblocks the signal, avoiding dependence on the launching shell's inherited signal settings. The parent inspects termination status, allowing the comparison to continue after the first case dies from a signal. This is a single-threaded test executable, not an example of adding fork to a multithreaded server.
Save the following as sigpipe-demo.c.
Build and run:
Observed results and verification scope
The program built without warnings under those options and exited with code 0. Its actual output was:
The first line is the parent's waitpid classification, not output from the child after send. The second and fourth lines show that the call returned and its immediately saved errno was EPIPE. The remaining lines classify each child's successful completion of its error check. The final executable returns 0 only when every case satisfies its expected condition.
The process-wide case changes the disposition only inside its separate child. It does not change the parent or contaminate the other cases. This verifies termination behavior and error returns. It does not test throughput, actual TCP retransmission or RST ordering, TLS libraries, or nonblocking event loops.
Responsibilities that remain in a server
If a direct send call already uses flags, combine them with MSG_NOSIGNAL using bitwise OR. The flag is supplied per call, so do not assume it protects another send path that omits it. write has no equivalent flags argument. If TLS or a framework owns the socket, first check that library's sending API and signal-handling contract.
When the return value is -1, preserve errno before calling another function and classify it. For an unrecoverable connection error, clean up that connection's send queue and event registration, then close it according to ownership rules. Retrying forever on the same broken socket is not a solution. These are integration design criteria; the minimal reproduction does not implement a server queue or event loop.
Compare a positive return value with the full requested length. Also follow the send return-value and error contract to distinguish EINTR and a nonblocking socket's EAGAIN or EWOULDBLOCK from a disconnected connection. Partial sends and waiting for writability require separate send-state management. This test covers only the failure path of a one-byte send.
With TCP, a peer close does not guarantee that the first send immediately produces EPIPE. Nor does a successful send prove that the peer application processed the response. Do not interpret this local reproduction as a test of a particular TCP failure sequence or delivery guarantee. A real service must separately validate its transport layer and duplicate-handling contract for reconnects and repeated requests.
Check the destination platform's API support before applying this on another operating system. The executed verification here covers Linux's MSG_NOSIGNAL path. If a process-wide policy change is needed, make that decision deliberately during application initialization and consider its effects on plugins and other modules.