From 526332c5454b67f1c498dc82e71657258a79d7e9 Mon Sep 17 00:00:00 2001 From: Claire Date: Wed, 2 Jun 2021 17:41:25 +0200 Subject: Fix account deletion sometimes failing because of optimistic locks (#16317) * Fix account deletion sometimes failing because of optimistic locks In some rare occasions[1], deleting accounts would fail with a `StaleObjectError` exception. Indeed, account deletion manually sets the `AccountStat` values without handling cases where the optimistic locking on `AccountStat` would fail. To my knowledge, with the rewrite of account counters in #15913, the `DeleteAccountService` is now the only place that changes the counters in a way that is not atomic. Since in this specific case, we do not care about the previous values of the account counters, it appears we don't need locking at all for this table anymore. [1]: https://discourse.joinmastodon.org/t/account-cant-be-deleted/3602 * Bump MAX_SUPPORTED_VERSION in maintenance script --- app/models/account_stat.rb | 3 ++- app/models/concerns/account_counters.rb | 2 -- 2 files changed, 2 insertions(+), 3 deletions(-) (limited to 'app/models') diff --git a/app/models/account_stat.rb b/app/models/account_stat.rb index a826a9af3..44da4f0d0 100644 --- a/app/models/account_stat.rb +++ b/app/models/account_stat.rb @@ -11,10 +11,11 @@ # created_at :datetime not null # updated_at :datetime not null # last_status_at :datetime -# lock_version :integer default(0), not null # class AccountStat < ApplicationRecord + self.locking_column = nil + belongs_to :account, inverse_of: :account_stat update_index('accounts#account', :account) diff --git a/app/models/concerns/account_counters.rb b/app/models/concerns/account_counters.rb index fd3f161ad..3fabb5205 100644 --- a/app/models/concerns/account_counters.rb +++ b/app/models/concerns/account_counters.rb @@ -49,7 +49,6 @@ module AccountCounters ON CONFLICT (account_id) DO UPDATE SET #{key} = account_stats.#{key} + :value, last_status_at = now(), - lock_version = account_stats.lock_version + 1, updated_at = now() RETURNING id; SQL @@ -59,7 +58,6 @@ module AccountCounters VALUES (:account_id, :default_value, now(), now()) ON CONFLICT (account_id) DO UPDATE SET #{key} = account_stats.#{key} + :value, - lock_version = account_stats.lock_version + 1, updated_at = now() RETURNING id; SQL -- cgit