From 618fb5b34dae44a391001784e50f3f823549ca11 Mon Sep 17 00:00:00 2001 From: Michael Brown Date: Tue, 13 Dec 2022 17:09:01 -0500 Subject: [PATCH] FIX: properly count DistributedMutex locking attempts When originally introduced, `attempts` was only used in the read-only check context. With the introduction of the exponential backoff in cda370db, `attempts` was also used to count loop iterations, but was left inside the if block instead of being incremented every loop, meaning the exponential backoff was only happening when the site was recently readonly. Co-authored-by: jbrw --- lib/distributed_mutex.rb | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/lib/distributed_mutex.rb b/lib/distributed_mutex.rb index bc29d149512..b456244a6d7 100644 --- a/lib/distributed_mutex.rb +++ b/lib/distributed_mutex.rb @@ -88,14 +88,11 @@ class DistributedMutex # Exponential backoff, max duration 1s interval = attempts < 10 ? (0.001 * 2**attempts) : 1 sleep interval + attempts += 1 # in readonly we will never be able to get a lock - if @using_global_redis && Discourse.recently_readonly? - attempts += 1 - - if attempts > CHECK_READONLY_ATTEMPTS - raise Discourse::ReadOnly - end + if @using_global_redis && Discourse.recently_readonly? && attempts > CHECK_READONLY_ATTEMPTS + raise Discourse::ReadOnly end end end