Skip to content

Commit

Permalink
Make Sentry.close thread-safe
Browse files Browse the repository at this point in the history
  • Loading branch information
solnic committed Oct 18, 2024
1 parent 03293ef commit 2fea332
Showing 1 changed file with 27 additions and 22 deletions.
49 changes: 27 additions & 22 deletions sentry-ruby/lib/sentry-ruby.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ module Sentry

THREAD_LOCAL = :sentry_hub

MUTEX = Mutex.new

class << self
# @!visibility private
def exception_locals_tp
Expand Down Expand Up @@ -226,6 +228,7 @@ def add_attachment(**opts)
# @yieldparam config [Configuration]
# @return [void]
def init(&block)
@mutex = MUTEX
config = Configuration.new
yield(config) if block_given?
config.detect_release
Expand All @@ -248,35 +251,37 @@ def init(&block)
#
# @return [void]
def close
if @session_flusher
@session_flusher.flush
@session_flusher.kill
@session_flusher = nil
end
@mutex.synchronize do
if @session_flusher
@session_flusher.flush
@session_flusher.kill
@session_flusher = nil
end

if @backpressure_monitor
@backpressure_monitor.kill
@backpressure_monitor = nil
end
if @backpressure_monitor
@backpressure_monitor.kill
@backpressure_monitor = nil

Check warning on line 263 in sentry-ruby/lib/sentry-ruby.rb

View check run for this annotation

Codecov / codecov/patch

sentry-ruby/lib/sentry-ruby.rb#L262-L263

Added lines #L262 - L263 were not covered by tests
end

if @metrics_aggregator
@metrics_aggregator.flush(force: true)
@metrics_aggregator.kill
@metrics_aggregator = nil
end
if @metrics_aggregator
@metrics_aggregator.flush(force: true)
@metrics_aggregator.kill
@metrics_aggregator = nil

Check warning on line 269 in sentry-ruby/lib/sentry-ruby.rb

View check run for this annotation

Codecov / codecov/patch

sentry-ruby/lib/sentry-ruby.rb#L267-L269

Added lines #L267 - L269 were not covered by tests
end

if client = get_current_client
client.flush
if client = get_current_client
client.flush

if client.configuration.include_local_variables
exception_locals_tp.disable
if client.configuration.include_local_variables
exception_locals_tp.disable

Check warning on line 276 in sentry-ruby/lib/sentry-ruby.rb

View check run for this annotation

Codecov / codecov/patch

sentry-ruby/lib/sentry-ruby.rb#L276

Added line #L276 was not covered by tests
end
end
end

@background_worker.shutdown
@background_worker.shutdown

@main_hub = nil
Thread.current.thread_variable_set(THREAD_LOCAL, nil)
@main_hub = nil
Thread.current.thread_variable_set(THREAD_LOCAL, nil)
end
end

# Returns true if the SDK is initialized.
Expand Down

0 comments on commit 2fea332

Please sign in to comment.