From 782fc401528f0749ffdf1b3434cc5576300fa6ea Mon Sep 17 00:00:00 2001 From: Benjamin Schaaf Date: Sat, 7 Sep 2019 03:15:30 +1000 Subject: [PATCH] Add MSG_NOSIGNAL flag to send call to avoid SIGPIPE (#128) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Add MSG_NOSIGNAL flag to send call to avoid SIGPIPE merged-on-behalf-of: Sönke Ludwig --- source/eventcore/drivers/posix/sockets.d | 28 +++++++++++++++++++----- 1 file changed, 22 insertions(+), 6 deletions(-) diff --git a/source/eventcore/drivers/posix/sockets.d b/source/eventcore/drivers/posix/sockets.d index 0e5ac11..ca909da 100644 --- a/source/eventcore/drivers/posix/sockets.d +++ b/source/eventcore/drivers/posix/sockets.d @@ -96,6 +96,16 @@ version (Windows) { extern (C) int close(int fd) nothrow @safe; } +version (Posix) { + version (OSX) { + enum SEND_FLAGS = 0; + } else { + enum SEND_FLAGS = MSG_NOSIGNAL; + } +} else { + enum SEND_FLAGS = 0; +} + final class PosixEventDriverSockets(Loop : PosixEventLoop) : EventDriverSockets { @safe: /*@nogc:*/ nothrow: @@ -464,7 +474,7 @@ final class PosixEventDriverSockets(Loop : PosixEventLoop) : EventDriverSockets } sizediff_t ret; - () @trusted { ret = .send(cast(sock_t)socket, buffer.ptr, min(buffer.length, int.max), 0); } (); + () @trusted { ret = .send(cast(sock_t)socket, buffer.ptr, min(buffer.length, int.max), SEND_FLAGS); } (); if (ret < 0) { auto err = getSocketError(); @@ -517,7 +527,7 @@ final class PosixEventDriverSockets(Loop : PosixEventLoop) : EventDriverSockets auto socket = cast(StreamSocketFD)fd; sizediff_t ret; - () @trusted { ret = .send(cast(sock_t)socket, slot.writeBuffer.ptr, min(slot.writeBuffer.length, int.max), 0); } (); + () @trusted { ret = .send(cast(sock_t)socket, slot.writeBuffer.ptr, min(slot.writeBuffer.length, int.max), SEND_FLAGS); } (); if (ret < 0) { auto err = getSocketError(); @@ -792,10 +802,10 @@ final class PosixEventDriverSockets(Loop : PosixEventLoop) : EventDriverSockets sizediff_t ret; if (target_address) { - () @trusted { ret = .sendto(cast(sock_t)socket, buffer.ptr, min(buffer.length, int.max), 0, target_address.name, target_address.nameLen); } (); + () @trusted { ret = .sendto(cast(sock_t)socket, buffer.ptr, min(buffer.length, int.max), SEND_FLAGS, target_address.name, target_address.nameLen); } (); m_loop.m_fds[socket].datagramSocket.targetAddr = target_address; } else { - () @trusted { ret = .send(cast(sock_t)socket, buffer.ptr, min(buffer.length, int.max), 0); } (); + () @trusted { ret = .send(cast(sock_t)socket, buffer.ptr, min(buffer.length, int.max), SEND_FLAGS); } (); } if (ret < 0) { @@ -838,9 +848,9 @@ final class PosixEventDriverSockets(Loop : PosixEventLoop) : EventDriverSockets sizediff_t ret; if (slot.targetAddr) { - () @trusted { ret = .sendto(cast(sock_t)socket, slot.writeBuffer.ptr, min(slot.writeBuffer.length, int.max), 0, slot.targetAddr.name, slot.targetAddr.nameLen); } (); + () @trusted { ret = .sendto(cast(sock_t)socket, slot.writeBuffer.ptr, min(slot.writeBuffer.length, int.max), SEND_FLAGS, slot.targetAddr.name, slot.targetAddr.nameLen); } (); } else { - () @trusted { ret = .send(cast(sock_t)socket, slot.writeBuffer.ptr, min(slot.writeBuffer.length, int.max), 0); } (); + () @trusted { ret = .send(cast(sock_t)socket, slot.writeBuffer.ptr, min(slot.writeBuffer.length, int.max), SEND_FLAGS); } (); } if (ret < 0) { @@ -940,6 +950,12 @@ final class PosixEventDriverSockets(Loop : PosixEventLoop) : EventDriverSockets () @trusted { sock = socket(family, type, 0); } (); if (sock == -1) return -1; setSocketNonBlocking(cast(SocketFD)sock, true); + + // Prevent SIGPIPE on failed send + version (OSX) { + int val = 1; + () @trusted { setsockopt(sock, SOL_SOCKET, SO_NOSIGPIPE, &val, val.sizeof); } (); + } } return sock; }