Skip to content

Commit

Permalink
error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
braindigitalis committed Nov 19, 2024
1 parent 366f2dc commit a819c5f
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 64 deletions.
14 changes: 9 additions & 5 deletions src/dpp/socketengine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,15 @@ void socket_engine_base::prune() {
to_delete_count = 0;
}
if (time(nullptr) != last_time) {
/* Every minute, rehash all cache containers.
* We do this from the socket engine now, not from
* shard 0, so no need to run shards to have timers!
*/
owner->tick_timers();
try {
/* Every minute, rehash all cache containers.
* We do this from the socket engine now, not from
* shard 0, so no need to run shards to have timers!
*/
owner->tick_timers();
} catch (const std::exception& e) {
owner->log(dpp::ll_error, "Uncaught exception in tick_timers: " + std::string(e.what()));
}

if ((time(nullptr) % 60) == 0) {
dpp::garbage_collection();
Expand Down
52 changes: 29 additions & 23 deletions src/dpp/socketengines/epoll.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,36 +90,42 @@ struct socket_engine_epoll : public socket_engine_base {
continue;
}

if ((ev.events & EPOLLHUP) != 0U) {
if (eh->on_error) {
eh->on_error(fd, *eh, EPIPE);
}
continue;
}
try {

if ((ev.events & EPOLLERR) != 0U) {
socklen_t codesize = sizeof(int);
int errcode{};
if (getsockopt(fd, SOL_SOCKET, SO_ERROR, &errcode, &codesize) < 0) {
errcode = errno;
if ((ev.events & EPOLLHUP) != 0U) {
if (eh->on_error) {
eh->on_error(fd, *eh, EPIPE);
}
continue;
}
if (eh->on_error) {
eh->on_error(fd, *eh, errcode);

if ((ev.events & EPOLLERR) != 0U) {
socklen_t codesize = sizeof(int);
int errcode{};
if (getsockopt(fd, SOL_SOCKET, SO_ERROR, &errcode, &codesize) < 0) {
errcode = errno;
}
if (eh->on_error) {
eh->on_error(fd, *eh, errcode);
}
continue;
}
continue;
}

if ((ev.events & EPOLLOUT) != 0U) {
eh->flags = modify_event(epoll_handle, eh, eh->flags & ~WANT_WRITE);
if (eh->on_write) {
eh->on_write(fd, *eh);
if ((ev.events & EPOLLOUT) != 0U) {
eh->flags = modify_event(epoll_handle, eh, eh->flags & ~WANT_WRITE);
if (eh->on_write) {
eh->on_write(fd, *eh);
}
}
}

if ((ev.events & EPOLLIN) != 0U) {
if (eh->on_read) {
eh->on_read(fd, *eh);
if ((ev.events & EPOLLIN) != 0U) {
if (eh->on_read) {
eh->on_read(fd, *eh);
}
}

} catch (const std::exception& e) {
eh->on_error(fd, *eh, 0);
}
}
prune();
Expand Down
36 changes: 21 additions & 15 deletions src/dpp/socketengines/kqueue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,24 +69,30 @@ struct socket_engine_kqueue : public socket_engine_base {
continue;
}

const short filter = kev.filter;
if (kev.flags & EV_EOF || kev.flags & EV_ERROR) {
if (eh->on_error) {
eh->on_error(kev.ident, *eh, kev.fflags);
try {

const short filter = kev.filter;
if (kev.flags & EV_EOF || kev.flags & EV_ERROR) {
if (eh->on_error) {
eh->on_error(kev.ident, *eh, kev.fflags);
}
continue;
}
continue;
}
if (filter == EVFILT_WRITE) {
const int bits_to_clr = WANT_WRITE;
eh->flags &= ~bits_to_clr;
if (eh->on_write) {
eh->on_write(kev.ident, *eh);
if (filter == EVFILT_WRITE) {
const int bits_to_clr = WANT_WRITE;
eh->flags &= ~bits_to_clr;
if (eh->on_write) {
eh->on_write(kev.ident, *eh);
}
}
}
else if (filter == EVFILT_READ) {
if (eh->on_read) {
eh->on_read(kev.ident, *eh);
else if (filter == EVFILT_READ) {
if (eh->on_read) {
eh->on_read(kev.ident, *eh);
}
}

} catch (const std::exception& e) {
eh->on_error(kev.ident, *eh, 0);
}
}
prune();
Expand Down
46 changes: 26 additions & 20 deletions src/dpp/socketengines/poll.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,30 +73,36 @@ struct socket_engine_poll : public socket_engine_base {
}
socket_events* eh = iter->second.get();

if ((revents & POLLHUP) != 0) {
eh->on_error(fd, *eh, 0);
continue;
}
try {

if ((revents & POLLERR) != 0) {
socklen_t codesize = sizeof(int);
int errcode{};
if (getsockopt(fd, SOL_SOCKET, SO_ERROR, (char*)&errcode, &codesize) < 0) {
errcode = errno;
if ((revents & POLLHUP) != 0) {
eh->on_error(fd, *eh, 0);
continue;
}
eh->on_error(fd, *eh, errcode);
continue;
}

if ((revents & POLLIN) != 0) {
eh->on_read(fd, *eh);
}
if ((revents & POLLERR) != 0) {
socklen_t codesize = sizeof(int);
int errcode{};
if (getsockopt(fd, SOL_SOCKET, SO_ERROR, (char*)&errcode, &codesize) < 0) {
errcode = errno;
}
eh->on_error(fd, *eh, errcode);
continue;
}

if ((revents & POLLIN) != 0) {
eh->on_read(fd, *eh);
}

if ((revents & POLLOUT) != 0) {
int mask = eh->flags;
mask &= ~WANT_WRITE;
eh->flags = mask;
eh->on_write(fd, *eh);
if ((revents & POLLOUT) != 0) {
int mask = eh->flags;
mask &= ~WANT_WRITE;
eh->flags = mask;
eh->on_write(fd, *eh);
}

} catch (const std::exception& e) {
eh->on_error(fd, *eh, 0);
}
}
prune();
Expand Down
4 changes: 3 additions & 1 deletion src/dpp/sslclient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -483,7 +483,9 @@ void ssl_client::on_write(socket fd, const struct socket_events& e) {
}

void ssl_client::on_error(socket fd, const struct socket_events&, int error_code) {
throw dpp::connection_exception(err_socket_error, strerror(errno));
if (sfd != INVALID_SOCKET) {
this->close();
}
}

void ssl_client::read_loop()
Expand Down

0 comments on commit a819c5f

Please sign in to comment.