From 133b9f41088288a53d0b7cca0e9d5bab4a633567 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kamil=20Trzci=C5=84ski?= Date: Thu, 4 Jul 2019 15:46:46 +0200 Subject: [PATCH] Fix race in forbid_sidekiq_in_transactions.rb Current code uses module attribute which stores value global instead of locally (thread locally). This results in concurrent accesses to overwrite the each other values --- .../fix-sidekiq-transaction-check-race.yml | 5 +++++ .../initializers/forbid_sidekiq_in_transactions.rb | 13 +++++++------ 2 files changed, 12 insertions(+), 6 deletions(-) create mode 100644 changelogs/unreleased/fix-sidekiq-transaction-check-race.yml diff --git a/changelogs/unreleased/fix-sidekiq-transaction-check-race.yml b/changelogs/unreleased/fix-sidekiq-transaction-check-race.yml new file mode 100644 index 00000000000..89ae4abfe11 --- /dev/null +++ b/changelogs/unreleased/fix-sidekiq-transaction-check-race.yml @@ -0,0 +1,5 @@ +--- +title: Fix race in forbid_sidekiq_in_transactions.rb +merge_request: 30359 +author: +type: fixed diff --git a/config/initializers/forbid_sidekiq_in_transactions.rb b/config/initializers/forbid_sidekiq_in_transactions.rb index a69f1ba090e..bb190af60b5 100644 --- a/config/initializers/forbid_sidekiq_in_transactions.rb +++ b/config/initializers/forbid_sidekiq_in_transactions.rb @@ -2,15 +2,16 @@ module Sidekiq module Worker EnqueueFromTransactionError = Class.new(StandardError) - mattr_accessor :skip_transaction_check - self.skip_transaction_check = false - def self.skipping_transaction_check(&block) - skip_transaction_check = self.skip_transaction_check - self.skip_transaction_check = true + previous_skip_transaction_check = self.skip_transaction_check + Thread.current[:sidekiq_worker_skip_transaction_check] = true yield ensure - self.skip_transaction_check = skip_transaction_check + Thread.current[:sidekiq_worker_skip_transaction_check] = previous_skip_transaction_check + end + + def self.skip_transaction_check + Thread.current[:sidekiq_worker_skip_transaction_check] end module ClassMethods