From 13083e999c936f258080cc5c157711ee2a5b7310 Mon Sep 17 00:00:00 2001 From: Matthew Esposito Date: Thu, 27 Jun 2024 23:32:17 -0400 Subject: [PATCH] fix(oauth): handle extremely rare race condition by atomically compare_exchanging --- src/oauth.rs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/oauth.rs b/src/oauth.rs index dd0fe66..2ed2ff7 100644 --- a/src/oauth.rs +++ b/src/oauth.rs @@ -131,7 +131,11 @@ pub async fn token_daemon() { } pub async fn force_refresh_token() { - OAUTH_IS_ROLLING_OVER.store(true, Ordering::SeqCst); + if !OAUTH_IS_ROLLING_OVER.compare_exchange(false, true, Ordering::SeqCst, Ordering::SeqCst).is_ok() { + trace!("Skipping refresh token roll over, already in progress"); + return; + } + trace!("Rolling over refresh token. Current rate limit: {}", OAUTH_RATELIMIT_REMAINING.load(Ordering::SeqCst)); OAUTH_CLIENT.write().await.refresh().await; OAUTH_RATELIMIT_REMAINING.store(99, Ordering::SeqCst);