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
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 configuration.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
137 changes: 137 additions & 0 deletions sentry-ruby/spec/sentry_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -784,4 +784,141 @@
end
end

describe ".close" do
context "when closing initialized SDK" do
it "not initialized?" do
expect(described_class.initialized?).to eq(true)
described_class.close
expect(described_class.initialized?).to eq(false)
end

it "removes main hub" do
expect(described_class.get_main_hub).to be_a(Sentry::Hub)
described_class.close
expect(described_class.get_main_hub).to eq(nil)
end

it "removes thread local" do
expect(Thread.current.thread_variable_get(described_class::THREAD_LOCAL)).to be_a(Sentry::Hub)
described_class.close
expect(Thread.current.thread_variable_get(described_class::THREAD_LOCAL)).to eq(nil)

end

it "calls background worker shutdown" do
expect(described_class.background_worker).to receive(:shutdown)
described_class.close
expect(described_class.background_worker).to eq(nil)
end

it "kills session flusher" do
expect(described_class.session_flusher).to receive(:kill)
described_class.close
expect(described_class.session_flusher).to eq(nil)
end

it "disables Tracepoint" do
perform_basic_setup do |config|
config.capture_exception_frame_locals = true
end

expect(described_class.exception_locals_tp).to receive(:disable)
sl0thentr0py marked this conversation as resolved.
Show resolved Hide resolved
described_class.close
end

it "no-ops top level API getters" do
described_class.close
expect(described_class.configuration).to eq(nil)
expect(described_class.csp_report_uri).to eq(nil)
expect(described_class.get_main_hub).to eq(nil)
expect(described_class.get_current_hub).to eq(nil)
expect(described_class.get_current_client).to eq(nil)
expect(described_class.get_current_scope).to eq(nil)
expect(described_class.last_event_id).to eq(nil)
end

it "no-ops top level API actions" do
sl0thentr0py marked this conversation as resolved.
Show resolved Hide resolved
described_class.close

expect_any_instance_of(Sentry::DummyTransport).not_to receive(:send_event)
expect_any_instance_of(Sentry::DummyTransport).not_to receive(:send_envelope)

described_class.send_event(event)
described_class.capture_event(event)
described_class.capture_message("test")

exception = ZeroDivisionError.new("divided by 0")
described_class.capture_exception(exception)
expect(described_class.exception_captured?(exception)).to eq(false)

expect do
described_class.with_exception_captured { 1 / 0 }
end.to raise_error(ZeroDivisionError)

expect_any_instance_of(Sentry::Scope).not_to receive(:set_tags)
described_class.set_tags(foo: "bar")

expect_any_instance_of(Sentry::Scope).not_to receive(:set_extras)
described_class.set_extras(foo: "bar")

expect_any_instance_of(Sentry::Scope).not_to receive(:set_user)
described_class.set_user(id: 1)

expect_any_instance_of(Sentry::Scope).not_to receive(:set_context)
described_class.set_context("character", { name: "John", age: 25 })

expect_any_instance_of(Sentry::Scope).not_to receive(:add_breadcrumb)
crumb = Sentry::Breadcrumb.new(message: "foo")
described_class.add_breadcrumb(crumb)

expect_any_instance_of(Sentry::Hub).not_to receive(:configure_scope)
described_class.configure_scope do |scope|
scope.set_tags(foo: "bar")
end

expect_any_instance_of(Sentry::Hub).not_to receive(:with_scope)
described_class.configure_scope do |scope|
scope.set_tags(foo: "bar")
Sentry.capture_message("test message")
end

expect_any_instance_of(Sentry::Hub).not_to receive(:with_session_tracking)
described_class.with_session_tracking do
1 + 1
end

expect_any_instance_of(Sentry::Hub).not_to receive(:start_transaction)
described_class.start_transaction(name: "foobar")

expect_any_instance_of(Sentry::Span).not_to receive(:with_child_span)
described_class.with_child_span do |child_span|
1 + 1
end
end
end

it "can reinitialize closed SDK" do
perform_basic_setup

expect do
described_class.capture_event(event)
end.to change { transport.events.count }.by(1)

described_class.close

expect do
described_class.capture_event(event)
end.to change { transport.events.count }.by(0)

perform_basic_setup

expect(described_class.initialized?).to eq(true)

new_transport = described_class.get_current_client.transport

expect do
described_class.capture_event(event)
end.to change { new_transport.events.count }.by(1)
end
end
end