Skip to content

Commit 8f65643

Browse files
committed
ignore SIGPIPE so a closed peer can't kill the process
A write to a socket whose peer has closed or reset raises SIGPIPE, which by default terminates the process rather than returning EPIPE. Ignore it at load via a constructor in common.h (included by every socket module), so send/write report the error through the normal return path. The loopback tests don't trip this, so a test-running CI alone would not catch it; this fixes the bug directly. (The tls library, which mirrors this code, had the same gap.)
1 parent f6db7f1 commit 8f65643

1 file changed

Lines changed: 9 additions & 0 deletions

File tree

src/common.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,22 @@
77
#include <netdb.h>
88
#include <netinet/in.h>
99
#include <netinet/tcp.h>
10+
#include <signal.h>
1011
#include <string.h>
1112
#include <sys/socket.h>
1213
#include <sys/types.h>
1314
#include <unistd.h>
1415

1516
#define SOCK_BUF_SIZE 4096
1617

18+
/* A write to a peer that has closed/reset the connection otherwise raises
19+
SIGPIPE, which by default kills the whole process. Ignore it at load so
20+
send/write return EPIPE instead. Runs before main via the constructor. */
21+
__attribute__((constructor))
22+
static void carp_sock_ignore_sigpipe(void) {
23+
signal(SIGPIPE, SIG_IGN);
24+
}
25+
1726
__attribute__((unused))
1827
static String sock_error_string() {
1928
const char* msg = strerror(errno);

0 commit comments

Comments
 (0)