From 40253a7adda3e8970949a2fef736f69395e953aa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Janiszewski?= Date: Fri, 27 May 2016 18:44:23 +0200 Subject: [PATCH] Prevent SIGPIPE-ing on Linux when clients drop (#3755) `send()` can only write to connected sockets [1]. In case where client drops out, a socket may become closed by OS but we can still enqueue packets for its connection and eventually call `send()`, which will generate a SIGPIPE and shut the server down, because there was no `MSG_NOSIGNAL` set. This commit makes sure `MSG_NOSIGNAL` is used on Linux. [1] http://linux.die.net/man/2/send --- src/network/network.cpp | 2 +- src/network/network.h | 6 ++++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/src/network/network.cpp b/src/network/network.cpp index 20b2b346b8..956d11823b 100644 --- a/src/network/network.cpp +++ b/src/network/network.cpp @@ -428,7 +428,7 @@ bool NetworkConnection::SendPacket(NetworkPacket& packet) tosend.insert(tosend.end(), (uint8*)&sizen, (uint8*)&sizen + sizeof(sizen)); tosend.insert(tosend.end(), packet.data->begin(), packet.data->end()); while (1) { - int sentBytes = send(socket, (const char*)&tosend[packet.transferred], tosend.size() - packet.transferred, 0); + int sentBytes = send(socket, (const char*)&tosend[packet.transferred], tosend.size() - packet.transferred, FLAG_NO_PIPE); if (sentBytes == SOCKET_ERROR) { return false; } diff --git a/src/network/network.h b/src/network/network.h index 248c2f2c9b..f454b73174 100644 --- a/src/network/network.h +++ b/src/network/network.h @@ -81,6 +81,7 @@ extern "C" { #ifndef SHUT_RDWR #define SHUT_RDWR SD_BOTH #endif + #define FLAG_NO_PIPE 0 #else #include #include @@ -94,6 +95,11 @@ extern "C" { #define LAST_SOCKET_ERROR() errno #define closesocket close #define ioctlsocket ioctl + #if defined(__LINUX__) + #define FLAG_NO_PIPE MSG_NOSIGNAL + #else + #define FLAG_NO_PIPE 0 + #endif // defined(__LINUX__) #endif // __WINDOWS__ // Fixes issues on OS X