Skip to content

Commit f1bbcf2

Browse files
authored
Use thread variable instead of fiber variable to store the hub (#1380)
* Use thread variable instead of fiber variable to store the hub * Update changelog
1 parent 4658f6f commit f1bbcf2

3 files changed

Lines changed: 17 additions & 3 deletions

File tree

sentry-ruby/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
- Correct type attribute's usages [#1354](https://github.com/getsentry/sentry-ruby/pull/1354)
66
- Fix sampling decision precedence [#1335](https://github.com/getsentry/sentry-ruby/pull/1335)
77
- Fix set_contexts [#1375](https://github.com/getsentry/sentry-ruby/pull/1375)
8+
- Use thread variable instead of fiber variable to store the hub [#1380](https://github.com/getsentry/sentry-ruby/pull/1380)
9+
- Fixes [#1374](https://github.com/getsentry/sentry-ruby/issues/1374)
810

911
## 4.3.1
1012

sentry-ruby/lib/sentry-ruby.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ def init(&block)
6868
client = Client.new(config)
6969
scope = Scope.new(max_breadcrumbs: config.max_breadcrumbs)
7070
hub = Hub.new(client, scope)
71-
Thread.current[THREAD_LOCAL] = hub
71+
Thread.current.thread_variable_set(THREAD_LOCAL, hub)
7272
@main_hub = hub
7373
@background_worker = Sentry::BackgroundWorker.new(config)
7474
end
@@ -92,7 +92,7 @@ def get_current_hub
9292
# ideally, we should do this proactively whenever a new thread is created
9393
# but it's impossible for the SDK to keep track every new thread
9494
# so we need to use this rather passive way to make sure the app doesn't crash
95-
Thread.current[THREAD_LOCAL] || clone_hub_to_current_thread
95+
Thread.current.thread_variable_get(THREAD_LOCAL) || clone_hub_to_current_thread
9696
end
9797

9898
# Returns the current active client.
@@ -107,7 +107,7 @@ def get_current_scope
107107

108108
# Clones the main thread's active hub and stores it to the current thread.
109109
def clone_hub_to_current_thread
110-
Thread.current[THREAD_LOCAL] = get_main_hub.clone
110+
Thread.current.thread_variable_set(THREAD_LOCAL, get_main_hub.clone)
111111
end
112112

113113
# Takes a block and yields the current active scope.

sentry-ruby/spec/sentry_spec.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,18 @@
6464

6565
new_thread.join
6666
end
67+
68+
it "stores the hub in a thread variable (instead of just fiber variable)" do
69+
Sentry.set_tags(outside_fiber: true)
70+
71+
fiber = Fiber.new do
72+
Sentry.set_tags(inside_fiber: true)
73+
end
74+
75+
fiber.resume
76+
77+
expect(Sentry.get_current_scope.tags).to eq({ outside_fiber: true, inside_fiber: true })
78+
end
6779
end
6880

6981
describe ".configure_scope" do

0 commit comments

Comments
 (0)