diff --git a/sentry-ruby/lib/sentry/vernier/profiler.rb b/sentry-ruby/lib/sentry/vernier/profiler.rb index 0ca8c0f39..6ee6b6ced 100644 --- a/sentry-ruby/lib/sentry/vernier/profiler.rb +++ b/sentry-ruby/lib/sentry/vernier/profiler.rb @@ -55,8 +55,7 @@ def start return unless @sampled return if @started - ::Vernier.start_profile - @started = true + @started = ::Vernier.start_profile log("Started") @@ -77,6 +76,12 @@ def stop @result = ::Vernier.stop_profile log("Stopped") + rescue RuntimeError => e + if e.message.include?("Profile not started") + log("Not stopped since not started") + else + log("Failed to stop Vernier: #{e.message}") + end end def active_thread_id diff --git a/sentry-ruby/spec/sentry/vernier/profiler_spec.rb b/sentry-ruby/spec/sentry/vernier/profiler_spec.rb index 03fe7ebde..46a696fee 100644 --- a/sentry-ruby/spec/sentry/vernier/profiler_spec.rb +++ b/sentry-ruby/spec/sentry/vernier/profiler_spec.rb @@ -133,6 +133,20 @@ expect(Vernier).to receive(:stop_profile) profiler.stop end + + it 'does not crash when Vernier was already stopped' do + profiler.set_initial_sample_decision(true) + profiler.start + Vernier.stop_profile + profiler.stop + end + + it 'does not crash when stopping Vernier crashed' do + profiler.set_initial_sample_decision(true) + profiler.start + expect(Vernier).to receive(:stop_profile).and_raise(RuntimeError.new("Profile not started")) + profiler.stop + end end describe "#to_hash" do