Skip to content

Commit

Permalink
Fixed race condition
Browse files Browse the repository at this point in the history
Using load and store could introduce a double store/load.
  • Loading branch information
1Git2Clone committed Jul 8, 2024
1 parent 40f7dbb commit a770601
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 5 deletions.
6 changes: 2 additions & 4 deletions src/event_handler/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,10 @@ pub async fn event_handler(
&& msg.contains("damn")
&& msg.contains("mains")
{
let mentions = data.poise_mentions.load(Ordering::SeqCst) + 1;
data.poise_mentions.store(mentions, Ordering::SeqCst);
data.poise_mentions.fetch_add(1, Ordering::SeqCst);
new_message.reply(ctx, "Any last words?").await?;
} else if msg.contains("hutao") || msg.contains("hu tao") {
let mentions = data.poise_mentions.load(Ordering::SeqCst) + 1;
data.poise_mentions.store(mentions, Ordering::SeqCst);
let mentions = data.poise_mentions.fetch_add(1, Ordering::SeqCst);
new_message
.reply(ctx, format!("Hu Tao has been mentioned {} times", mentions))
.await?;
Expand Down
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ async fn main() {
Box::pin(async move {
poise::builtins::register_globally(ctx, &framework.options().commands).await?;
Ok(Data {
poise_mentions: AtomicU32::new(0),
poise_mentions: AtomicU32::new(1),
// It's better to clone the bot user once when it starts rather than do http
// requests for the serenity::CurrentUser on every comman invocation.
bot_user: Arc::from(ready.user.clone()),
Expand Down

0 comments on commit a770601

Please sign in to comment.