Skip to content

Commit

Permalink
fix: prevent concurrent on_disconnect()
Browse files Browse the repository at this point in the history
  • Loading branch information
braindigitalis committed Dec 3, 2024
1 parent f2e1edc commit 8c0c5d2
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 4 deletions.
5 changes: 5 additions & 0 deletions include/dpp/discordclient.h
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,11 @@ class DPP_EXPORT discord_client : public websocket_client
*/
void start_connecting();

/**
* @brief Timer for reconnecting
*/
timer reconnect_timer{0};

private:

/**
Expand Down
2 changes: 1 addition & 1 deletion library/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ if(MSVC)
string(REGEX REPLACE "/W[1|2|3|4]" "/W3" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
else()
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fPIC -Wall -Wno-unused-private-field -Wno-psabi -Wempty-body -Wignored-qualifiers -Wimplicit-fallthrough -Wmissing-field-initializers -Wsign-compare -Wtype-limits -Wuninitialized -Wshift-negative-value -pthread")
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -Og")
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -O0 -g")

if (NOT MINGW)
add_link_options("-rdynamic")
Expand Down
10 changes: 9 additions & 1 deletion src/dpp/discordclient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,8 @@ void discord_client::cleanup()
{
delete etf;
delete zlib;
etf = nullptr;
zlib = nullptr;
}

discord_client::~discord_client()
Expand All @@ -127,9 +129,15 @@ void discord_client::on_disconnect()
log(dpp::ll_debug, "Lost connection to websocket on shard " + std::to_string(shard_id) + ", reconnecting in 5 seconds...");
ssl_client::close();
end_zlib();
owner->start_timer([this](auto handle) {
/* Stop the timer first if its already ticking, to prevent concurrent reconnects */
if (reconnect_timer) {
owner->stop_timer(reconnect_timer);
reconnect_timer = 0;
}
reconnect_timer = owner->start_timer([this](auto handle) {
log(dpp::ll_debug, "Reconnecting shard " + std::to_string(shard_id) + " to wss://" + hostname + "...");
owner->stop_timer(handle);
reconnect_timer = 0;
cleanup();
if (timer_handle) {
owner->stop_timer(timer_handle);
Expand Down
4 changes: 2 additions & 2 deletions src/dpp/socketengines/epoll.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ struct DPP_EXPORT socket_engine_epoll : public socket_engine_base {
ev.events |= EPOLLERR;
}
{
std::unique_lock lock(fds_mutex);
std::shared_lock lock(fds_mutex);
ev.data.ptr = fds.find(e.fd)->second.get();
}
return epoll_ctl(epoll_handle, EPOLL_CTL_ADD, e.fd, &ev) >= 0;
Expand All @@ -169,7 +169,7 @@ struct DPP_EXPORT socket_engine_epoll : public socket_engine_base {
ev.events |= EPOLLERR;
}
{
std::unique_lock lock(fds_mutex);
std::shared_lock lock(fds_mutex);
ev.data.ptr = fds.find(e.fd)->second.get();
}
return epoll_ctl(epoll_handle, EPOLL_CTL_MOD, e.fd, &ev) >= 0;
Expand Down

0 comments on commit 8c0c5d2

Please sign in to comment.