summaryrefslogtreecommitdiff
path: root/lib/declarative_policy.rb
diff options
context:
space:
mode:
authorJan Provaznik <jprovaznik@gitlab.com>2018-07-07 20:22:01 +0200
committerJan Provaznik <jprovaznik@gitlab.com>2018-07-10 12:52:57 +0200
commitfdfc8d04d2007bd2956e39161b00b1c19cbbadcc (patch)
treeadb84c09c3598fe37e1763e2edf2978f77ab92b8 /lib/declarative_policy.rb
parentf3edbafe6310ca1584002b8a351048a3fb3309e3 (diff)
downloadgitlab-ce-fdfc8d04d2007bd2956e39161b00b1c19cbbadcc.tar.gz
[Rails5] Permit concurrent loads
Rails 5 introduced a new (and safer) autoload mechanism which uses locking when loading missing constants. Under some circumstances it's possible that a deadlock occurs in multi-threaded environment (especially when running capybara tests) To avoid these deadlocks, concurrent load is enabled when loading policies - we ere hitting timeout in this case: https://gitlab.com/gitlab-org/gitlab-ce/issues/48263 Note that autoloading is enabled only in development or test envs (not in production).
Diffstat (limited to 'lib/declarative_policy.rb')
-rw-r--r--lib/declarative_policy.rb12
1 files changed, 11 insertions, 1 deletions
diff --git a/lib/declarative_policy.rb b/lib/declarative_policy.rb
index 1dd2855063d..dda6cd38dcd 100644
--- a/lib/declarative_policy.rb
+++ b/lib/declarative_policy.rb
@@ -21,7 +21,17 @@ module DeclarativePolicy
cache = opts[:cache] || {}
key = Cache.policy_key(user, subject)
- cache[key] ||= class_for(subject).new(user, subject, opts)
+ cache[key] ||=
+ if Gitlab.rails5?
+ # to avoid deadlocks in multi-threaded environment when
+ # autoloading is enabled, we allow concurrent loads,
+ # https://gitlab.com/gitlab-org/gitlab-ce/issues/48263
+ ActiveSupport::Dependencies.interlock.permit_concurrent_loads do
+ class_for(subject).new(user, subject, opts)
+ end
+ else
+ class_for(subject).new(user, subject, opts)
+ end
end
def class_for(subject)