Skip to content

Commit

Permalink
more errno fixes (#415)
Browse files Browse the repository at this point in the history
  • Loading branch information
MisterTea authored Mar 24, 2021
1 parent 81c0788 commit e2736af
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 10 deletions.
12 changes: 12 additions & 0 deletions src/base/ClientConnection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,18 @@ void ClientConnection::pollReconnect() {
} catch (const std::runtime_error& re) {
LOG(INFO) << "Got failure during reconnect";
socketHandler->close(newSocketFd);
} catch (...) {
LOG(ERROR) << "Got an unknown error!";
std::exception_ptr eptr = std::current_exception();
if (eptr) {
try {
std::rethrow_exception(eptr);
} catch (const std::exception &e) {
LOG(ERROR) << "Uncaught c++ exception: " << e.what();
}
} else {
LOG(ERROR) << "Uncaught c++ exception (unknown)";
}
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/base/PipeSocketHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ int PipeSocketHandler::connect(const SocketEndpoint& endpoint) {
VLOG(3) << "Connecting to " << endpoint << " with fd " << sockFd;
int result =
::connect(sockFd, (struct sockaddr*)&remote, sizeof(sockaddr_un));
if (result < 0 && errno != EINPROGRESS) {
auto localErrno = errno;
auto localErrno = errno;
if (result < 0 && localErrno != EINPROGRESS) {
VLOG(3) << "Connection result: " << result << " (" << strerror(localErrno)
<< ")";
#ifdef WIN32
Expand Down
10 changes: 6 additions & 4 deletions src/base/SocketHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,14 @@ int SocketHandler::writeAllOrReturn(int fd, const void* buf, size_t count) {
return -1;
}
ssize_t bytesWritten = write(fd, ((const char*)buf) + pos, count - pos);
auto localErrno = errno;
if (bytesWritten < 0) {
if (errno == EAGAIN || errno == EWOULDBLOCK) {
if (localErrno == EAGAIN || localErrno == EWOULDBLOCK) {
LOG(INFO) << "Got EAGAIN, waiting...";
// This is fine, just keep retrying at 10hz
std::this_thread::sleep_for(std::chrono::microseconds(100 * 1000));
} else {
VLOG(1) << "Failed a call to writeAll: " << strerror(errno);
VLOG(1) << "Failed a call to writeAll: " << strerror(localErrno);
return -1;
}
} else if (bytesWritten == 0) {
Expand All @@ -78,13 +79,14 @@ void SocketHandler::writeAllOrThrow(int fd, const void* buf, size_t count,
throw std::runtime_error("Socket Timeout");
}
ssize_t bytesWritten = write(fd, ((const char*)buf) + pos, count - pos);
auto localErrno = errno;
if (bytesWritten < 0) {
if (errno == EAGAIN || errno == EWOULDBLOCK) {
if (localErrno == EAGAIN || localErrno == EWOULDBLOCK) {
LOG(INFO) << "Got EAGAIN, waiting...";
// This is fine, just keep retrying at 10hz
std::this_thread::sleep_for(std::chrono::microseconds(100 * 1000));
} else {
LOG(WARNING) << "Failed a call to writeAll: " << strerror(errno);
LOG(WARNING) << "Failed a call to writeAll: " << strerror(localErrno);
throw std::runtime_error("Failed a call to writeAll");
}
} else if (bytesWritten == 0) {
Expand Down
3 changes: 2 additions & 1 deletion src/base/UnixSocketHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,9 @@ ssize_t UnixSocketHandler::write(int fd, const void *buf, size_t count) {
w = ::write(fd, ((const char *)buf) + bytesWritten, count - bytesWritten);
#endif
#endif
auto localErrno = errno;
if (w < 0) {
if (errno == EAGAIN || errno == EWOULDBLOCK) {
if (localErrno == EAGAIN || localErrno == EWOULDBLOCK) {
std::this_thread::sleep_for(std::chrono::milliseconds(1));
if (time(NULL) > startTime + 5) {
// Give up
Expand Down
5 changes: 3 additions & 2 deletions src/terminal/UserJumphostHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,18 @@ UserJumphostHandler::UserJumphostHandler(
dstSocketEndpoint(_dstSocketEndpoint),
shuttingDown(false) {
routerFd = routerSocketHandler->connect(routerEndpoint);
auto localErrno = errno;

if (routerFd < 0) {
if (errno == ECONNREFUSED) {
if (localErrno == ECONNREFUSED) {
CLOG(INFO, "stdout")
<< "Error: The Eternal Terminal daemon is not running. Please "
"(re)start the et daemon on the server."
<< endl;
} else {
CLOG(INFO, "stdout")
<< "Error: Connection error communicating with et daemon: "
<< strerror(errno) << "." << endl;
<< strerror(localErrno) << "." << endl;
}
exit(1);
}
Expand Down
3 changes: 2 additions & 1 deletion test/BackedTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,9 @@ void listenFn(shared_ptr<SocketHandler> socketHandler, SocketEndpoint endpoint,
int fd;
while (true) {
fd = socketHandler->accept(serverFd);
auto localErrno = errno;
if (fd == -1) {
if (errno != EAGAIN) {
if (localErrno != EAGAIN) {
FATAL_FAIL(fd);
} else {
std::this_thread::sleep_for(std::chrono::microseconds(100 * 1000));
Expand Down

0 comments on commit e2736af

Please sign in to comment.