Welcome to mirror list, hosted at ThFree Co, Russian Federation.

gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
path: root/config
diff options
context:
space:
mode:
authorYorick Peterse <yorickpeterse@gmail.com>2017-02-20 16:28:05 +0300
committerDouwe Maan <douwe@selenight.nl>2017-06-02 19:43:32 +0300
commit4edb505bb6783525444dd0179d1fb536a5cd1390 (patch)
tree6640d0a27a56beaa980b90a746bacc2eb699a4e7 /config
parent51c196160adfbdb723fa6295cddf74a5ce033d6b (diff)
Forbid Sidekiq scheduling in transactions
Scheduling jobs in transactions can lead to race conditions where a job runs before a transaction has been committed. The added initializer ensures the various Sidekiq scheduling methods raise an error when called inside a transaction. Fixes #27233
Diffstat (limited to 'config')
-rw-r--r--config/initializers/forbid_sidekiq_in_transactions.rb27
1 files changed, 27 insertions, 0 deletions
diff --git a/config/initializers/forbid_sidekiq_in_transactions.rb b/config/initializers/forbid_sidekiq_in_transactions.rb
new file mode 100644
index 00000000000..4eeb4ba4468
--- /dev/null
+++ b/config/initializers/forbid_sidekiq_in_transactions.rb
@@ -0,0 +1,27 @@
+module Sidekiq
+ module Worker
+ module ClassMethods
+ module NoSchedulingFromTransactions
+ NESTING = ::Rails.env.test? ? 1 : 0
+
+ %i(perform_async perform_at perform_in).each do |name|
+ define_method(name) do |*args|
+ if ActiveRecord::Base.connection.open_transactions > NESTING
+ raise <<-MSG.strip_heredoc
+ `#{self}.#{name}` cannot be called inside a transaction as this can lead to race
+ conditions when the worker runs before the transaction is committed and tries to
+ access a model that has not been saved yet.
+
+ Schedule the worker from inside a `run_after_commit` block instead.
+ MSG
+ end
+
+ super(*args)
+ end
+ end
+ end
+
+ prepend NoSchedulingFromTransactions
+ end
+ end
+end