Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

errno fixes #336

Merged
merged 1 commit into from
Jul 30, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions src/base/Connection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -152,16 +152,18 @@ bool Connection::read(Packet* packet) {
lock_guard<std::recursive_mutex> guard(connectionMutex);
VLOG(4) << "After read get connectionMutex";
ssize_t messagesRead = reader->read(packet);
auto localErrno = errno;
if (messagesRead == -1) {
if (isSkippableError(errno)) {
if (isSkippableError(localErrno)) {
// Close the socket and invalidate, then return 0 messages
LOG(INFO) << "Closing socket because " << errno << " " << strerror(errno);
LOG(INFO) << "Closing socket because " << localErrno << " "
<< strerror(localErrno);
closeSocketAndMaybeReconnect();
return 0;
} else {
// Throw the error
STERROR << "Got a serious error trying to read: " << errno << " / "
<< strerror(errno);
STERROR << "Got a serious error trying to read: " << localErrno << " / "
<< strerror(localErrno);
throw std::runtime_error("Failed a call to read");
}
} else {
Expand Down
12 changes: 8 additions & 4 deletions src/base/PipeSocketHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ int PipeSocketHandler::connect(const SocketEndpoint& endpoint) {
int result =
::connect(sockFd, (struct sockaddr*)&remote, sizeof(sockaddr_un));
if (result < 0 && errno != EINPROGRESS) {
VLOG(3) << "Connection result: " << result << " (" << strerror(errno)
auto localErrno = errno;
VLOG(3) << "Connection result: " << result << " (" << strerror(localErrno)
<< ")";
#ifdef WIN32
::shutdown(sockFd, SD_BOTH);
Expand All @@ -28,6 +29,7 @@ int PipeSocketHandler::connect(const SocketEndpoint& endpoint) {
#endif
::close(sockFd);
sockFd = -1;
errno = localErrno;
return sockFd;
}

Expand All @@ -45,7 +47,8 @@ int PipeSocketHandler::connect(const SocketEndpoint& endpoint) {
int so_error;
socklen_t len = sizeof so_error;

FATAL_FAIL(::getsockopt(sockFd, SOL_SOCKET, SO_ERROR, (char*)&so_error, &len));
FATAL_FAIL(
::getsockopt(sockFd, SOL_SOCKET, SO_ERROR, (char*)&so_error, &len));

if (so_error == 0) {
LOG(INFO) << "Connected to endpoint " << endpoint;
Expand All @@ -61,8 +64,9 @@ int PipeSocketHandler::connect(const SocketEndpoint& endpoint) {
sockFd = -1;
}
} else {
LOG(INFO) << "Error connecting to " << endpoint << ": " << errno << " "
<< strerror(errno);
auto localErrno = errno;
LOG(INFO) << "Error connecting to " << endpoint << ": " << localErrno << " "
<< strerror(localErrno);
::close(sockFd);
sockFd = -1;
}
Expand Down
9 changes: 5 additions & 4 deletions src/base/SocketHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,12 @@ void SocketHandler::readAll(int fd, void* buf, size_t count, bool timeout) {
bytesRead = -1;
}
if (bytesRead < 0) {
if (errno == EAGAIN || errno == EWOULDBLOCK) {
auto localErrno = errno;
if (localErrno == EAGAIN || localErrno == EWOULDBLOCK) {
// This is fine, just keep retrying
LOG(INFO) << "Got EAGAIN, waiting...";
} else {
VLOG(1) << "Failed a call to readAll: " << strerror(errno);
VLOG(1) << "Failed a call to readAll: " << strerror(localErrno);
throw std::runtime_error("Failed a call to readAll");
}
} else {
Expand All @@ -51,7 +52,7 @@ int SocketHandler::writeAllOrReturn(int fd, const void* buf, size_t count) {
if (errno == EAGAIN || errno == EWOULDBLOCK) {
LOG(INFO) << "Got EAGAIN, waiting...";
// This is fine, just keep retrying at 10hz
std::this_thread::sleep_for(std::chrono::microseconds(100*1000));
std::this_thread::sleep_for(std::chrono::microseconds(100 * 1000));
} else {
VLOG(1) << "Failed a call to writeAll: " << strerror(errno);
return -1;
Expand Down Expand Up @@ -81,7 +82,7 @@ void SocketHandler::writeAllOrThrow(int fd, const void* buf, size_t count,
if (errno == EAGAIN || errno == EWOULDBLOCK) {
LOG(INFO) << "Got EAGAIN, waiting...";
// This is fine, just keep retrying at 10hz
std::this_thread::sleep_for(std::chrono::microseconds(100*1000));
std::this_thread::sleep_for(std::chrono::microseconds(100 * 1000));
} else {
LOG(WARNING) << "Failed a call to writeAll: " << strerror(errno);
throw std::runtime_error("Failed a call to writeAll");
Expand Down
41 changes: 25 additions & 16 deletions src/base/TcpSocketHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,17 +46,21 @@ int TcpSocketHandler::connect(const SocketEndpoint &endpoint) {
// loop through all the results and connect to the first we can
for (p = results; p != NULL; p = p->ai_next) {
if ((sockFd = socket(p->ai_family, p->ai_socktype, p->ai_protocol)) == -1) {
LOG(INFO) << "Error creating socket: " << errno << " " << strerror(errno);
auto localErrno = errno;
LOG(INFO) << "Error creating socket: " << localErrno << " "
<< strerror(localErrno);
continue;
}

if (::connect(sockFd, p->ai_addr, p->ai_addrlen) == -1 &&
errno != EINPROGRESS) {
auto localErrno = errno;
if (p->ai_canonname) {
LOG(INFO) << "Error connecting with " << p->ai_canonname << ": "
<< errno << " " << strerror(errno);
<< localErrno << " " << strerror(localErrno);
} else {
LOG(INFO) << "Error connecting: " << errno << " " << strerror(errno);
LOG(INFO) << "Error connecting: " << localErrno << " "
<< strerror(localErrno);
}
::close(sockFd);
sockFd = -1;
Expand All @@ -76,7 +80,8 @@ int TcpSocketHandler::connect(const SocketEndpoint &endpoint) {
int so_error;
socklen_t len = sizeof so_error;

FATAL_FAIL(::getsockopt(sockFd, SOL_SOCKET, SO_ERROR, (char*)&so_error, &len));
FATAL_FAIL(
::getsockopt(sockFd, SOL_SOCKET, SO_ERROR, (char *)&so_error, &len));

if (so_error == 0) {
if (p->ai_canonname) {
Expand Down Expand Up @@ -118,12 +123,13 @@ int TcpSocketHandler::connect(const SocketEndpoint &endpoint) {
continue;
}
} else {
auto localErrno = errno;
if (p->ai_canonname) {
LOG(INFO) << "Error connecting with " << p->ai_canonname << ": "
<< errno << " " << strerror(errno);
<< localErrno << " " << strerror(localErrno);
} else {
LOG(INFO) << "Error connecting to " << endpoint << ": " << errno << " "
<< strerror(errno);
LOG(INFO) << "Error connecting to " << endpoint << ": " << localErrno
<< " " << strerror(localErrno);
}
::close(sockFd);
sockFd = -1;
Expand Down Expand Up @@ -170,9 +176,10 @@ set<int> TcpSocketHandler::listen(const SocketEndpoint &endpoint) {
for (p = servinfo; p != NULL; p = p->ai_next) {
int sockFd;
if ((sockFd = socket(p->ai_family, p->ai_socktype, p->ai_protocol)) == -1) {
auto localErrno = errno;
LOG(INFO) << "Error creating socket " << p->ai_family << "/"
<< p->ai_socktype << "/" << p->ai_protocol << ": " << errno
<< " " << strerror(errno);
<< p->ai_socktype << "/" << p->ai_protocol << ": " << localErrno
<< " " << strerror(localErrno);
continue;
}
initServerSocket(sockFd);
Expand All @@ -188,14 +195,16 @@ set<int> TcpSocketHandler::listen(const SocketEndpoint &endpoint) {

if (::bind(sockFd, p->ai_addr, p->ai_addrlen) == -1) {
// This most often happens because the port is in use.
auto localErrno = errno;
STERROR << "Error binding " << p->ai_family << "/" << p->ai_socktype
<< "/" << p->ai_protocol << ": " << errno << " "
<< strerror(errno);
<< "/" << p->ai_protocol << ": " << localErrno << " "
<< strerror(localErrno);
cout << "Error binding " << p->ai_family << "/" << p->ai_socktype << "/"
<< p->ai_protocol << ": " << errno << " " << strerror(errno) << endl;
<< p->ai_protocol << ": " << localErrno << " "
<< strerror(localErrno) << endl;
stringstream oss;
oss << "Error binding port " << port << ": " << errno << " "
<< strerror(errno);
oss << "Error binding port " << port << ": " << localErrno << " "
<< strerror(localErrno);
string s = oss.str();
close(sockFd);
throw std::runtime_error(s.c_str());
Expand Down Expand Up @@ -258,8 +267,8 @@ void TcpSocketHandler::initSocket(int fd) {
struct linger so_linger;
so_linger.l_onoff = 1;
so_linger.l_linger = 5;
FATAL_FAIL_UNLESS_EINVAL(
setsockopt(fd, SOL_SOCKET, SO_LINGER, (const char*)&so_linger, sizeof so_linger));
FATAL_FAIL_UNLESS_EINVAL(setsockopt(
fd, SOL_SOCKET, SO_LINGER, (const char *)&so_linger, sizeof so_linger));
}
}
} // namespace et
11 changes: 7 additions & 4 deletions src/base/UnixSocketHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,15 @@ ssize_t UnixSocketHandler::read(int fd, void *buf, size_t count) {
lock_guard<recursive_mutex> guard(*(it->second));
VLOG(4) << "Unixsocket handler read from fd: " << fd;
#ifdef WIN32
ssize_t readBytes = ::recv(fd, (char*)buf, count, 0);
ssize_t readBytes = ::recv(fd, (char *)buf, count, 0);
#else
ssize_t readBytes = ::read(fd, buf, count);
#endif
if (readBytes < 0 && errno != EAGAIN && errno != EWOULDBLOCK) {
STERROR << "Error reading: " << errno << " " << strerror(errno);
auto localErrno = errno;
if (readBytes < 0 && localErrno != EAGAIN && localErrno != EWOULDBLOCK) {
STERROR << "Error reading: " << localErrno << " " << strerror(localErrno);
}
errno = localErrno;
return readBytes;
}

Expand All @@ -76,7 +78,7 @@ ssize_t UnixSocketHandler::write(int fd, const void *buf, size_t count) {
lock_guard<recursive_mutex> guard(*(it->second));
int w;
#ifdef WIN32
w = ::send(fd, ((const char*)buf) + bytesWritten, count - bytesWritten, 0);
w = ::send(fd, ((const char *)buf) + bytesWritten, count - bytesWritten, 0);
#else
#ifdef MSG_NOSIGNAL
w = ::send(fd, ((const char *)buf) + bytesWritten, count - bytesWritten,
Expand Down Expand Up @@ -143,6 +145,7 @@ int UnixSocketHandler::accept(int sockFd) {
FATAL_FAIL(-1); // STFATAL with the error
}

errno = acceptErrno;
return -1;
}

Expand Down