diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2024-01-04 21:13:47 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2024-01-04 21:13:47 +0300 |
commit | cc514c362bcd4b657bf6a6d1d37f5305952df363 (patch) | |
tree | 695c721b87c573d843cfeb23be2183d65b8a9785 /gems | |
parent | 7de116050af7a190085c01bbf819e48e708e8eb2 (diff) |
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'gems')
-rw-r--r-- | gems/gitlab-utils/lib/gitlab/utils.rb | 14 | ||||
-rw-r--r-- | gems/gitlab-utils/spec/gitlab/utils_spec.rb | 20 |
2 files changed, 34 insertions, 0 deletions
diff --git a/gems/gitlab-utils/lib/gitlab/utils.rb b/gems/gitlab-utils/lib/gitlab/utils.rb index b7cb63210ee..d5a514bfae8 100644 --- a/gems/gitlab-utils/lib/gitlab/utils.rb +++ b/gems/gitlab-utils/lib/gitlab/utils.rb @@ -261,6 +261,8 @@ module Gitlab end end + # Use this method to set the `restrict_within_concurrent_ruby` to `true` for the block. + # `raise_if_concurrent_ruby!` will use this flag to raise an error if it's set to `true`. def restrict_within_concurrent_ruby previous = Thread.current[:restrict_within_concurrent_ruby] Thread.current[:restrict_within_concurrent_ruby] = true @@ -270,6 +272,18 @@ module Gitlab Thread.current[:restrict_within_concurrent_ruby] = previous end + # Use this method to disable the `restrict_within_concurrent_ruby` for the block. + # It is mainly used to prevent infinite loop when `ConcurrentRubyThreadIsUsedError` is rescued and sent to Sentry. + # More info: https://gitlab.com/gitlab-org/gitlab/-/issues/432145#note_1671305713 + def allow_within_concurrent_ruby + previous = Thread.current[:restrict_within_concurrent_ruby] + Thread.current[:restrict_within_concurrent_ruby] = false + + yield + ensure + Thread.current[:restrict_within_concurrent_ruby] = previous + end + # Running external methods can allocate I/O bound resources (like PostgreSQL connection or Gitaly) # This is forbidden when running within a concurrent Ruby thread, for example `async` HTTP requests # provided by the `gitlab-http` gem. diff --git a/gems/gitlab-utils/spec/gitlab/utils_spec.rb b/gems/gitlab-utils/spec/gitlab/utils_spec.rb index 69531225eef..02d288acedf 100644 --- a/gems/gitlab-utils/spec/gitlab/utils_spec.rb +++ b/gems/gitlab-utils/spec/gitlab/utils_spec.rb @@ -489,6 +489,26 @@ RSpec.describe Gitlab::Utils, feature_category: :shared do end end + describe '.allow_within_concurrent_ruby' do + it 'assigns restrict_within_concurrent_ruby false to the current thread and ensures it restores' do + expect(Thread.current[:restrict_within_concurrent_ruby]).to be_nil + + described_class.allow_within_concurrent_ruby do + expect(Thread.current[:restrict_within_concurrent_ruby]).to be(false) + end + + expect(Thread.current[:restrict_within_concurrent_ruby]).to be_nil + end + + it 'overrides the value of restrict_within_concurrent_ruby' do + described_class.restrict_within_concurrent_ruby do + described_class.allow_within_concurrent_ruby do + expect(Thread.current[:restrict_within_concurrent_ruby]).to be(false) + end + end + end + end + describe '.raise_if_concurrent_ruby!' do subject(:raise_if_concurrent_ruby!) { described_class.raise_if_concurrent_ruby!('test') } |