Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement close API that cleans up state and reverses init logic #1844

Merged
merged 8 commits into from
Jul 19, 2022
Merged
28 changes: 26 additions & 2 deletions sentry-ruby/lib/sentry-ruby.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,11 @@ def exception_locals_tp
end

# @!attribute [rw] background_worker
# @return [BackgroundWorker]
# @return [BackgroundWorker, nil]
attr_accessor :background_worker

# @!attribute [r] session_flusher
# @return [SessionFlusher]
# @return [SessionFlusher, nil]
attr_reader :session_flusher

##### Patch Registration #####
Expand Down Expand Up @@ -215,8 +215,31 @@ def init(&block)

at_exit do
@session_flusher&.kill
sl0thentr0py marked this conversation as resolved.
Show resolved Hide resolved
@background_worker&.shutdown
end
end

# Flushes pending events and cleans up SDK state.
# SDK will stop sending events and all top-level APIs will be no-ops after this.
#
# @return [void]
def close
if @background_worker
@background_worker.shutdown
@background_worker = nil
end

if @session_flusher
@session_flusher.kill
@session_flusher = nil
end

if config.capture_exception_frame_locals
exception_locals_tp.disable
end

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

# Returns true if the SDK is initialized.
Expand Down Expand Up @@ -287,6 +310,7 @@ def get_current_scope
#
# @return [void]
def clone_hub_to_current_thread
return unless initialized?
Thread.current.thread_variable_set(THREAD_LOCAL, get_main_hub.clone)
end

Expand Down