From 104fdaf4464101ff1f7e3112f6874e9d12b755f6 Mon Sep 17 00:00:00 2001 From: Eugen Rochko Date: Thu, 26 May 2022 18:58:19 +0200 Subject: [PATCH] Fix concurrent unfollowing decrementing follower count more than once --- app/services/unfollow_service.rb | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/app/services/unfollow_service.rb b/app/services/unfollow_service.rb index 151f3674f..0f04713d1 100644 --- a/app/services/unfollow_service.rb +++ b/app/services/unfollow_service.rb @@ -13,7 +13,13 @@ class UnfollowService < BaseService @target_account = target_account @options = options - unfollow! || undo_follow_request! + RedisLock.acquire(redis: Redis.current, key: "relationship:#{[source_account.id, target_account.id].sort.join(':')}", autorelease: 90.seconds) do |lock| + if lock.acquired? + unfollow! || undo_follow_request! + else + raise Mastodon::RaceConditionError + end + end end private