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

Do not report SignalExceptions in at_exit handler #477

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
20 changes: 11 additions & 9 deletions lib/bugsnag.rb
Original file line number Diff line number Diff line change
Expand Up @@ -118,15 +118,17 @@ def notify(exception, auto_notify=false, &block)
def register_at_exit
return if at_exit_handler_installed?
@exit_handler_added = true
at_exit do
if $!
Bugsnag.notify($!, true) do |report|
report.severity = 'error'
report.severity_reason = {
:type => Bugsnag::Report::UNHANDLED_EXCEPTION
}
end
end
at_exit { $! && at_exit_handler($!) }
end

def at_exit_handler(exception)
return if exception.is_a?(SignalException)

Bugsnag.notify(exception, true) do |report|
report.severity = 'error'
report.severity_reason = {
:type => Bugsnag::Report::UNHANDLED_EXCEPTION
}
end
end

Expand Down
67 changes: 46 additions & 21 deletions spec/bugsnag_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,33 +41,58 @@
Bugsnag.register_at_exit
end

describe 'at_exit_handler' do
context 'with a non-signal exception' do
it 'reports the exception' do
exception = BugsnagTestException.new('Oh no')

report_mock = double('report')
expect(report_mock).to receive(:severity=).with('error')
expect(report_mock).to receive(:severity_reason=).with({
:type => Bugsnag::Report::UNHANDLED_EXCEPTION
})

expect(Bugsnag).to receive(:notify).with(exception, true).and_yield(report_mock)

Bugsnag.at_exit_handler(exception)
end
end

context 'with a signal exception' do
it 'does not report the exception' do
exception = SignalException.new('SIGTERM')

expect(Bugsnag).to_not receive(:notify).with(exception, true)

Bugsnag.at_exit_handler(exception)
end
end
end

context 'with aliased at_exit' do
before do
module Kernel
alias_method :old_at_exit, :at_exit
def at_exit
begin
raise BugsnagTestException.new("Oh no")
rescue
yield
context 'and a non-signal exception' do
before do
module Kernel
alias_method :old_at_exit, :at_exit
def at_exit
begin
raise BugsnagTestException.new("Oh no")
rescue
yield
end
end
end
end
end

it "sends an exception when at_exit is called" do
report_mock = double('report')
expect(report_mock).to receive(:severity=).with('error')
expect(report_mock).to receive(:severity_reason=).with({
:type => Bugsnag::Report::UNHANDLED_EXCEPTION
})
expect(Bugsnag).to receive(:notify).with(kind_of(BugsnagTestException), true).and_yield(report_mock)
Bugsnag.register_at_exit
end
it "calls the at_exit_handler" do
expect(Bugsnag).to receive(:at_exit_handler)
Bugsnag.register_at_exit
end

after do
module Kernel
alias_method :at_exit, :old_at_exit
after do
module Kernel
alias_method :at_exit, :old_at_exit
end
end
end
end
Expand Down