Skip to content

Commit b30722e

Browse files
authored
Add exited status to SessionFlusher and rescue ThreadError (#1851)
* Add exited status to SessionFlusher and rescue ThreadError * Specs * Move rescue to add_session * CHANGELOG
1 parent 3323159 commit b30722e

File tree

3 files changed

+54
-2
lines changed

3 files changed

+54
-2
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@
4040
- Respect `report_rescued_exceptions` config [#1847](https://github.com/getsentry/sentry-ruby/pull/1847)
4141
- Fixes [#1840](https://github.com/getsentry/sentry-ruby/issues/1840)
4242
- Rescue event's to JSON conversion error [#1853](https://github.com/getsentry/sentry-ruby/pull/1853)
43+
- Rescue `ThreadError` in `SessionFlusher` and stop creating threads if flusher is killed [#1851](https://github.com/getsentry/sentry-ruby/issues/1851)
44+
- Fixes [#1848](https://github.com/getsentry/sentry-ruby/issues/1848)
4345

4446
### Refactoring
4547

sentry-ruby/lib/sentry/session_flusher.rb

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ class SessionFlusher
88

99
def initialize(configuration, client)
1010
@thread = nil
11+
@exited = false
1112
@client = client
1213
@pending_aggregates = {}
1314
@release = configuration.release
@@ -29,9 +30,16 @@ def flush
2930
end
3031

3132
def add_session(session)
33+
return if @exited
3234
return unless @release
3335

34-
ensure_thread
36+
begin
37+
ensure_thread
38+
rescue ThreadError
39+
log_debug("Session flusher thread creation failed")
40+
@exited = true
41+
return
42+
end
3543

3644
return unless Session::AGGREGATE_STATUSES.include?(session.status)
3745
@pending_aggregates[session.aggregation_key] ||= init_aggregates(session.aggregation_key)
@@ -40,6 +48,8 @@ def add_session(session)
4048

4149
def kill
4250
log_debug("Killing session flusher")
51+
52+
@exited = true
4353
@thread&.kill
4454
end
4555

sentry-ruby/spec/sentry/session_flusher_spec.rb

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,12 +126,52 @@
126126
expect(pending_aggregates.keys.first).to be_a(Time)
127127
expect(pending_aggregates.values.first).to include({ errored: 0, exited: 1 })
128128
end
129+
130+
context "when thread creation fails" do
131+
before do
132+
expect(Thread).to receive(:new).and_raise(ThreadError)
133+
end
134+
135+
it "doesn't create new thread" do
136+
expect do
137+
subject.add_session(session)
138+
end.to change { Thread.list.count }.by(0)
139+
end
140+
141+
it "noops" do
142+
subject.add_session(session)
143+
expect(subject.instance_variable_get(:@pending_aggregates)).to eq({})
144+
end
145+
146+
it "logs error" do
147+
subject.add_session(session)
148+
expect(string_io.string).to match(/Session flusher thread creation failed/)
149+
end
150+
end
151+
152+
context "when killed" do
153+
before do
154+
subject.kill
155+
end
156+
157+
it "noops" do
158+
subject.add_session(session)
159+
expect(subject.instance_variable_get(:@pending_aggregates)).to eq({})
160+
end
161+
162+
it "doesn't create new thread" do
163+
expect(Thread).not_to receive(:new)
164+
165+
expect do
166+
subject.add_session(session)
167+
end.to change { Thread.list.count }.by(0)
168+
end
169+
end
129170
end
130171

131172
describe "#kill" do
132173
it "logs message when killing the thread" do
133174
subject.kill
134-
135175
expect(string_io.string).to match(/Killing session flusher/)
136176
end
137177
end

0 commit comments

Comments
 (0)