diff --git a/sentry-ruby/lib/sentry/test_helper.rb b/sentry-ruby/lib/sentry/test_helper.rb index 0387ba354..4ede32933 100644 --- a/sentry-ruby/lib/sentry/test_helper.rb +++ b/sentry-ruby/lib/sentry/test_helper.rb @@ -159,6 +159,9 @@ def reset_sentry_globals! # Don't check initialized? because sometimes we stub it in tests if Sentry.instance_variable_defined?(:@main_hub) Sentry::GLOBALS.each do |var| + worker = Sentry.instance_variable_get(:"@#{var}") + worker.kill if worker.respond_to?(:kill) + Sentry.instance_variable_set(:"@#{var}", nil) end diff --git a/sentry-ruby/lib/sentry/threaded_periodic_worker.rb b/sentry-ruby/lib/sentry/threaded_periodic_worker.rb index aff5682d5..2ec35c108 100644 --- a/sentry-ruby/lib/sentry/threaded_periodic_worker.rb +++ b/sentry-ruby/lib/sentry/threaded_periodic_worker.rb @@ -30,10 +30,15 @@ def ensure_thread end def kill - log_debug("[#{self.class.name}] thread killed") - @exited = true - @thread&.kill + + # Only a started worker has a thread to kill (and to log about). + # Guarding here keeps a never-started worker's teardown silent, so + # killing one during test reset can't emit a stray debug line. + return unless @thread + + log_debug("[#{self.class.name}] thread killed") + @thread.kill end end end diff --git a/sentry-ruby/spec/sentry/session_flusher_spec.rb b/sentry-ruby/spec/sentry/session_flusher_spec.rb index de91ea1bd..af88dbd79 100644 --- a/sentry-ruby/spec/sentry/session_flusher_spec.rb +++ b/sentry-ruby/spec/sentry/session_flusher_spec.rb @@ -172,8 +172,14 @@ describe "#kill" do it "logs message when killing the thread" do + subject.ensure_thread subject.kill expect(string_io.string).to include("[#{described_class.name}] thread killed") end + + it "stays silent when no thread was ever started" do + subject.kill + expect(string_io.string).not_to include("thread killed") + end end end