Skip to content

Commit

Permalink
also set SIGPIPE handler to SIG_IGN in Server.cpp, use send() for soc…
Browse files Browse the repository at this point in the history
…ket writing with MSG_NOSIGNAL to explicitly disable SIGPIPE signals again (may be necessary on some systems), should fix #9
  • Loading branch information
patrickbr committed Dec 21, 2023
1 parent 338df82 commit 8f57a6a
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 8 deletions.
25 changes: 18 additions & 7 deletions src/qlever-petrimaps/server/Server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// Authors: Patrick Brosi <[email protected]>

#include <png.h>
#include <sys/socket.h>

#include <algorithm>
#include <chrono>
Expand Down Expand Up @@ -60,6 +61,10 @@ Server::Server(size_t maxMemory, const std::string& cacheDir, int cacheLifetime)
// _____________________________________________________________________________
util::http::Answer Server::handle(const util::http::Req& req, int con) const {
UNUSED(con);

// ignore SIGPIPE
signal(SIGPIPE, SIG_IGN);

util::http::Answer a;
try {
Params params;
Expand Down Expand Up @@ -125,6 +130,9 @@ util::http::Answer Server::handle(const util::http::Req& req, int con) const {
// _____________________________________________________________________________
util::http::Answer Server::handleHeatMapReq(const Params& pars,
int sock) const {
// ignore SIGPIPE
signal(SIGPIPE, SIG_IGN);

if (pars.count("width") == 0 || pars.find("width")->second.empty())
throw std::invalid_argument("No width (?width=) specified.");
if (pars.count("height") == 0 || pars.find("height")->second.empty())
Expand Down Expand Up @@ -520,7 +528,8 @@ util::http::Answer Server::handleHeatMapReq(const Params& pars,
size_t writes = 0;

while (writes != buff.size()) {
int64_t out = write(sock, buff.c_str() + writes, buff.size() - writes);
int64_t out =
send(sock, buff.c_str() + writes, buff.size() - writes, MSG_NOSIGNAL);
if (out < 0) {
if (errno == EWOULDBLOCK || errno == EAGAIN || errno == EINTR) continue;
throw std::runtime_error("Failed to write to socket");
Expand Down Expand Up @@ -871,8 +880,8 @@ inline void pngWriteCb(png_structp png_ptr, png_bytep data, png_size_t length) {
size_t writes = 0;

while (writes != length) {
int64_t out =
write(sock, reinterpret_cast<char*>(data) + writes, length - writes);
int64_t out = send(sock, reinterpret_cast<char*>(data) + writes,
length - writes, MSG_NOSIGNAL);
if (out < 0) {
if (errno == EWOULDBLOCK || errno == EAGAIN || errno == EINTR) continue;
break;
Expand Down Expand Up @@ -1028,7 +1037,8 @@ util::http::Answer Server::handleExportReq(const Params& pars, int sock) const {
size_t writes = 0;

while (writes != buff.size()) {
int64_t out = write(sock, buff.c_str() + writes, buff.size() - writes);
int64_t out =
send(sock, buff.c_str() + writes, buff.size() - writes, MSG_NOSIGNAL);
if (out < 0) {
if (errno == EWOULDBLOCK || errno == EAGAIN || errno == EINTR) continue;
throw std::runtime_error("Failed to write to socket");
Expand Down Expand Up @@ -1093,8 +1103,8 @@ util::http::Answer Server::handleExportReq(const Params& pars, int sock) const {
size_t writes = 0;

while (writes != buff.size()) {
int64_t out =
write(sock, buff.c_str() + writes, buff.size() - writes);
int64_t out = send(sock, buff.c_str() + writes, buff.size() - writes,
MSG_NOSIGNAL);
if (out < 0) {
if (errno == EWOULDBLOCK || errno == EAGAIN || errno == EINTR)
continue;
Expand All @@ -1108,7 +1118,8 @@ util::http::Answer Server::handleExportReq(const Params& pars, int sock) const {
writes = 0;

while (writes != buff.size()) {
int64_t out = write(sock, buff.c_str() + writes, buff.size() - writes);
int64_t out =
send(sock, buff.c_str() + writes, buff.size() - writes, MSG_NOSIGNAL);
if (out < 0) {
if (errno == EWOULDBLOCK || errno == EAGAIN || errno == EINTR) continue;
throw std::runtime_error("Failed to write to socket");
Expand Down
2 changes: 1 addition & 1 deletion src/util
Submodule util updated 1 files
+4 −3 http/Server.cpp

0 comments on commit 8f57a6a

Please sign in to comment.