Skip to content

Commit 3ce928c

Browse files
SeanLFclaude
andcommitted
fix: sync HubStorage on post-init isolation_level change; stabilize specs
Two follow-ups from review: - Configuration#isolation_level= now applies the level to HubStorage when the SDK is already initialized, so changing Sentry.configuration.isolation_level after Sentry.init takes effect instead of leaving the SDK on the old backend. The default is assigned to the ivar directly in initialize so a throwaway Configuration.new cannot clobber the active isolation level (only Sentry.init builds the live config). - Make the :fiber isolation_level specs deterministic across Ruby versions by stubbing fiber_storage_available?, and add an explicit downgrade-to-:thread spec. Previously these asserted :fiber unconditionally and would fail on the Ruby < 3.2 CI cells, where :fiber correctly falls back to :thread. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
1 parent 5f89c7f commit 3ce928c

4 files changed

Lines changed: 43 additions & 9 deletions

File tree

sentry-ruby/lib/sentry/configuration.rb

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -555,7 +555,10 @@ def initialize
555555
self.capture_queue_time = true
556556
self.org_id = nil
557557
self.strict_trace_continuation = false
558-
self.isolation_level = :thread
558+
# Assign the ivar directly rather than through the setter: the default must
559+
# not sync HubStorage (a throwaway Configuration.new would otherwise clobber
560+
# the active isolation level). init and the setter handle syncing.
561+
@isolation_level = :thread
559562

560563
spotlight_env = ENV["SENTRY_SPOTLIGHT"]
561564
spotlight_bool = Sentry::Utils::EnvHelper.env_to_bool(spotlight_env, strict: true)
@@ -634,6 +637,11 @@ def isolation_level=(level)
634637
end
635638

636639
@isolation_level = applied
640+
641+
# Keep the active hub storage in sync when the level is changed after the
642+
# SDK is initialized. During `Sentry.init` the config block runs before the
643+
# SDK is initialized, so init applies the level to HubStorage itself.
644+
Sentry::HubStorage.isolation_level = applied if Sentry.initialized?
637645
end
638646

639647
def breadcrumbs_logger=(logger)

sentry-ruby/spec/sentry/configuration_spec.rb

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -702,6 +702,9 @@ class SentryConfigurationSample < Sentry::Configuration
702702
end
703703

704704
it "accepts :thread and :fiber" do
705+
# stub availability so the :fiber assertion is deterministic on Rubies < 3.2
706+
allow(Sentry::HubStorage).to receive(:fiber_storage_available?).and_return(true)
707+
705708
subject.isolation_level = :fiber
706709
expect(subject.isolation_level).to eq(:fiber)
707710

@@ -710,6 +713,7 @@ class SentryConfigurationSample < Sentry::Configuration
710713
end
711714

712715
it "coerces string values" do
716+
allow(Sentry::HubStorage).to receive(:fiber_storage_available?).and_return(true)
713717
subject.isolation_level = "fiber"
714718
expect(subject.isolation_level).to eq(:fiber)
715719
end
@@ -719,6 +723,14 @@ class SentryConfigurationSample < Sentry::Configuration
719723
.to raise_error(ArgumentError, /isolation_level must be one of/)
720724
end
721725

726+
context "when Fiber storage is available" do
727+
it "keeps :fiber" do
728+
allow(Sentry::HubStorage).to receive(:fiber_storage_available?).and_return(true)
729+
subject.isolation_level = :fiber
730+
expect(subject.isolation_level).to eq(:fiber)
731+
end
732+
end
733+
722734
context "when Fiber storage is unavailable" do
723735
it "falls back to :thread with a warning" do
724736
allow(Sentry::HubStorage).to receive(:fiber_storage_available?).and_return(false)
@@ -727,13 +739,6 @@ class SentryConfigurationSample < Sentry::Configuration
727739
expect(subject.isolation_level).to eq(:thread)
728740
end
729741
end
730-
731-
context "when Fiber storage is available", when: { fiber_storage?: [] } do
732-
it "keeps :fiber" do
733-
subject.isolation_level = :fiber
734-
expect(subject.isolation_level).to eq(:fiber)
735-
end
736-
end
737742
end
738743

739744
describe "#validate" do

sentry-ruby/spec/sentry/hub_storage_spec.rb

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,18 @@
1616
expect(described_class.isolation_level).to eq(:thread)
1717
end
1818

19-
it "accepts :fiber" do
19+
it "accepts :fiber when Fiber storage is available" do
20+
allow(described_class).to receive(:fiber_storage_available?).and_return(true)
2021
described_class.isolation_level = :fiber
2122
expect(described_class.isolation_level).to eq(:fiber)
2223
end
2324

25+
it "downgrades :fiber to :thread when Fiber storage is unavailable" do
26+
allow(described_class).to receive(:fiber_storage_available?).and_return(false)
27+
described_class.isolation_level = :fiber
28+
expect(described_class.isolation_level).to eq(:thread)
29+
end
30+
2431
it "raises ArgumentError for an unknown level" do
2532
expect { described_class.isolation_level = :process }
2633
.to raise_error(ArgumentError, /isolation_level must be one of/)

sentry-ruby/spec/sentry_spec.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,20 @@
110110
end
111111
end
112112

113+
describe "changing isolation_level after init", when: { fiber_storage?: [] } do
114+
before { perform_basic_setup }
115+
116+
after { Sentry::HubStorage.isolation_level = :thread }
117+
118+
it "applies the change to the active hub storage" do
119+
expect(Sentry::HubStorage.isolation_level).to eq(:thread)
120+
121+
Sentry.configuration.isolation_level = :fiber
122+
123+
expect(Sentry::HubStorage.isolation_level).to eq(:fiber)
124+
end
125+
end
126+
113127
describe "fiber isolation", when: { fiber_storage?: [] } do
114128
before do
115129
perform_basic_setup { |config| config.isolation_level = :fiber }

0 commit comments

Comments
 (0)