@@ -229,7 +229,11 @@ void cluster::start(start_type return_after) {
229
229
if (now >= shard_reconnect_time) {
230
230
/* This shard needs to be reconnected */
231
231
reconnections.erase (reconnect);
232
- discord_client* old = shards[shard_id];
232
+ discord_client* old = nullptr ;
233
+ {
234
+ std::shared_lock lk (shards_mutex);
235
+ old = shards[shard_id];
236
+ }
233
237
/* These values must be copied to the new connection
234
238
* to attempt to resume it
235
239
*/
@@ -238,6 +242,7 @@ void cluster::start(start_type return_after) {
238
242
log (ll_info, " Reconnecting shard " + std::to_string (shard_id));
239
243
/* Make a new resumed connection based off the old one */
240
244
try {
245
+ std::unique_lock lk (shards_mutex);
241
246
if (shards[shard_id] != nullptr ) {
242
247
log (ll_trace, " Attempting resume..." );
243
248
shards[shard_id] = nullptr ;
@@ -255,6 +260,7 @@ void cluster::start(start_type return_after) {
255
260
shards[shard_id]->run ();
256
261
}
257
262
catch (const std::exception& e) {
263
+ std::unique_lock lk (shards_mutex);
258
264
log (ll_info, " Exception when reconnecting shard " + std::to_string (shard_id) + " : " + std::string (e.what ()));
259
265
delete shards[shard_id];
260
266
delete old;
@@ -340,6 +346,7 @@ void cluster::start(start_type return_after) {
340
346
if (s % maxclusters == cluster_id) {
341
347
/* Each discord_client is inserted into the socket engine when we call run() */
342
348
try {
349
+ std::unique_lock lk (shards_mutex);
343
350
this ->shards [s] = new discord_client (this , s, numshards, token, intents, compressed, ws_mode);
344
351
this ->shards [s]->run ();
345
352
}
@@ -455,6 +462,7 @@ void cluster::shutdown() {
455
462
next_timer = {};
456
463
}
457
464
465
+ std::unique_lock lk (shards_mutex);
458
466
/* Terminate shards */
459
467
for (const auto & sh : shards) {
460
468
delete sh.second ;
@@ -581,6 +589,7 @@ void cluster::set_presence(const dpp::presence &p) {
581
589
}
582
590
583
591
json pres = p.to_json ();
592
+ std::shared_lock lk (shards_mutex);
584
593
for (auto & s : shards) {
585
594
if (s.second ->is_connected ()) {
586
595
s.second ->queue_message (s.second ->jsonobj_to_string (pres));
@@ -610,15 +619,16 @@ std::string cluster::get_audit_reason() {
610
619
}
611
620
612
621
discord_client* cluster::get_shard (uint32_t id) const {
622
+ std::shared_lock lk (shards_mutex);
613
623
auto i = shards.find (id);
614
624
if (i != shards.end ()) {
615
625
return i->second ;
616
- } else {
617
- return nullptr ;
618
626
}
627
+ return nullptr ;
619
628
}
620
629
621
- const shard_list& cluster::get_shards () {
630
+ shard_list cluster::get_shards () const {
631
+ std::shared_lock lk (shards_mutex);
622
632
return shards;
623
633
}
624
634
0 commit comments