diff --git a/sentry-ruby/CHANGELOG.md b/sentry-ruby/CHANGELOG.md index 17fdbce1f..399b27d7a 100644 --- a/sentry-ruby/CHANGELOG.md +++ b/sentry-ruby/CHANGELOG.md @@ -5,6 +5,8 @@ - Correct type attribute's usages [#1354](https://github.com/getsentry/sentry-ruby/pull/1354) - Fix sampling decision precedence [#1335](https://github.com/getsentry/sentry-ruby/pull/1335) - Fix set_contexts [#1375](https://github.com/getsentry/sentry-ruby/pull/1375) +- Use thread variable instead of fiber variable to store the hub [#1380](https://github.com/getsentry/sentry-ruby/pull/1380) + - Fixes [#1374](https://github.com/getsentry/sentry-ruby/issues/1374) ## 4.3.1 diff --git a/sentry-ruby/lib/sentry-ruby.rb b/sentry-ruby/lib/sentry-ruby.rb index 0e49c50be..82c33fffe 100644 --- a/sentry-ruby/lib/sentry-ruby.rb +++ b/sentry-ruby/lib/sentry-ruby.rb @@ -68,7 +68,7 @@ def init(&block) client = Client.new(config) scope = Scope.new(max_breadcrumbs: config.max_breadcrumbs) hub = Hub.new(client, scope) - Thread.current[THREAD_LOCAL] = hub + Thread.current.thread_variable_set(THREAD_LOCAL, hub) @main_hub = hub @background_worker = Sentry::BackgroundWorker.new(config) end @@ -92,7 +92,7 @@ def get_current_hub # ideally, we should do this proactively whenever a new thread is created # but it's impossible for the SDK to keep track every new thread # so we need to use this rather passive way to make sure the app doesn't crash - Thread.current[THREAD_LOCAL] || clone_hub_to_current_thread + Thread.current.thread_variable_get(THREAD_LOCAL) || clone_hub_to_current_thread end # Returns the current active client. @@ -107,7 +107,7 @@ def get_current_scope # Clones the main thread's active hub and stores it to the current thread. def clone_hub_to_current_thread - Thread.current[THREAD_LOCAL] = get_main_hub.clone + Thread.current.thread_variable_set(THREAD_LOCAL, get_main_hub.clone) end # Takes a block and yields the current active scope. diff --git a/sentry-ruby/spec/sentry_spec.rb b/sentry-ruby/spec/sentry_spec.rb index 6872c6cee..4069ff4f2 100644 --- a/sentry-ruby/spec/sentry_spec.rb +++ b/sentry-ruby/spec/sentry_spec.rb @@ -64,6 +64,18 @@ new_thread.join end + + it "stores the hub in a thread variable (instead of just fiber variable)" do + Sentry.set_tags(outside_fiber: true) + + fiber = Fiber.new do + Sentry.set_tags(inside_fiber: true) + end + + fiber.resume + + expect(Sentry.get_current_scope.tags).to eq({ outside_fiber: true, inside_fiber: true }) + end end describe ".configure_scope" do