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
This commit is contained in:
Kamil Trzciński 2019-07-04 15:46:46 +02:00
parent b7ba557112
commit 133b9f4108
2 changed files with 12 additions and 6 deletions

View file

@ -0,0 +1,5 @@
---
title: Fix race in forbid_sidekiq_in_transactions.rb
merge_request: 30359
author:
type: fixed

View file

@ -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