Skip to content

fix: add thread safety primitives to internal shard list #1436

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

Merged
merged 2 commits into from
May 19, 2025
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
1 change: 1 addition & 0 deletions .cspell.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"version": "0.2",
"language": "en-GB",
"words": [
"merperson",
"EVFILT",
"fflags",
"udata",
Expand Down
9 changes: 7 additions & 2 deletions include/dpp/cluster.h
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,11 @@ class DPP_EXPORT cluster {
*/
std::shared_mutex named_commands_mutex;

/**
* @brief Mutex for protection of shards list
*/
mutable std::shared_mutex shards_mutex;

/**
* @brief Typedef for slashcommand handler type
*/
Expand Down Expand Up @@ -558,9 +563,9 @@ class DPP_EXPORT cluster {
/**
* @brief Get the list of shards
*
* @return shard_list& Reference to map of shards for this cluster
* @return shard_list map of shards for this cluster
*/
const shard_list& get_shards();
shard_list get_shards() const;

/**
* @brief Sets the request timeout.
Expand Down
18 changes: 14 additions & 4 deletions src/dpp/cluster.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,11 @@ void cluster::start(start_type return_after) {
if (now >= shard_reconnect_time) {
/* This shard needs to be reconnected */
reconnections.erase(reconnect);
discord_client* old = shards[shard_id];
discord_client* old = nullptr;
{
std::shared_lock lk(shards_mutex);
old = shards[shard_id];
}
/* These values must be copied to the new connection
* to attempt to resume it
*/
Expand All @@ -238,6 +242,7 @@ void cluster::start(start_type return_after) {
log(ll_info, "Reconnecting shard " + std::to_string(shard_id));
/* Make a new resumed connection based off the old one */
try {
std::unique_lock lk(shards_mutex);
if (shards[shard_id] != nullptr) {
log(ll_trace, "Attempting resume...");
shards[shard_id] = nullptr;
Expand All @@ -255,6 +260,7 @@ void cluster::start(start_type return_after) {
shards[shard_id]->run();
}
catch (const std::exception& e) {
std::unique_lock lk(shards_mutex);
log(ll_info, "Exception when reconnecting shard " + std::to_string(shard_id) + ": " + std::string(e.what()));
delete shards[shard_id];
delete old;
Expand Down Expand Up @@ -340,6 +346,7 @@ void cluster::start(start_type return_after) {
if (s % maxclusters == cluster_id) {
/* Each discord_client is inserted into the socket engine when we call run() */
try {
std::unique_lock lk(shards_mutex);
this->shards[s] = new discord_client(this, s, numshards, token, intents, compressed, ws_mode);
this->shards[s]->run();
}
Expand Down Expand Up @@ -455,6 +462,7 @@ void cluster::shutdown() {
next_timer = {};
}

std::unique_lock lk(shards_mutex);
/* Terminate shards */
for (const auto& sh : shards) {
delete sh.second;
Expand Down Expand Up @@ -581,6 +589,7 @@ void cluster::set_presence(const dpp::presence &p) {
}

json pres = p.to_json();
std::shared_lock lk(shards_mutex);
for (auto& s : shards) {
if (s.second->is_connected()) {
s.second->queue_message(s.second->jsonobj_to_string(pres));
Expand Down Expand Up @@ -610,15 +619,16 @@ std::string cluster::get_audit_reason() {
}

discord_client* cluster::get_shard(uint32_t id) const {
std::shared_lock lk(shards_mutex);
auto i = shards.find(id);
if (i != shards.end()) {
return i->second;
} else {
return nullptr;
}
return nullptr;
}

const shard_list& cluster::get_shards() {
shard_list cluster::get_shards() const {
std::shared_lock lk(shards_mutex);
return shards;
}

Expand Down