Skip to content

Commit

Permalink
Ignore SIGTERM signal exceptions. (#269)
Browse files Browse the repository at this point in the history
* Ignore SIGTERM signal exceptions.

* Fix bug caused by missing jruby signm implementation

* Fix failing test caused by missing signal normalization
  • Loading branch information
minhajuddin authored and joshuap committed Mar 22, 2018
1 parent e495f43 commit c21d5c1
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 2 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ adheres to [Semantic Versioning](http://semver.org/).
hooks which will be invoked with a `notice` before a `notice` is sent. Each
`before_notify` hook MUST be a `callable` (lambda, Proc etc,) with an arity of 1.
- Added the ability to halt notices in callbacks using `notice.halt!`

### Fixed
- Ignore SIGTERM SignalExceptions.

## [3.3.0] - 2018-01-29
### Changed
Expand Down
13 changes: 12 additions & 1 deletion lib/honeybadger/singleton.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ def load_plugins!
# @api private
def install_at_exit_callback
at_exit do
if $! && !$!.is_a?(SystemExit) && Honeybadger.config[:'exceptions.notify_at_exit']
if $! && !ignored_exception?($!) && Honeybadger.config[:'exceptions.notify_at_exit']
Honeybadger.notify($!, component: 'at_exit', sync: true)
end

Expand All @@ -85,4 +85,15 @@ def start(config = {})
end
WARNING
end

private
# @api private
def ignored_exception?(exception)
exception.is_a?(SystemExit) ||
( exception.is_a?(SignalException) &&
( (exception.respond_to?(:signm) && exception.signm == "SIGTERM") ||
# jruby has a missing #signm implementation
["TERM", "SIGTERM"].include?(exception.to_s) )
)
end
end
19 changes: 19 additions & 0 deletions spec/features/at_exit_spec.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
feature "Rescuing exceptions at exit" do
let(:crash_cmd) { "ruby #{ FIXTURES_PATH.join('ruby_crash.rb') }" }

def custom_crash_cmd(crash_type)
"ruby #{ FIXTURES_PATH.join('ruby_custom_crash.rb') } #{ crash_type }"
end

before do
set_environment_variable('HONEYBADGER_API_KEY', 'asdf')
set_environment_variable('HONEYBADGER_LOGGING_LEVEL', 'DEBUG')
Expand All @@ -11,6 +15,21 @@
assert_notification('error' => {'class' => 'RuntimeError', 'message' => 'RuntimeError: badgers!'})
end

it "ignores SystemExit" do
expect(run(custom_crash_cmd("system_exit"))).not_to be_successfully_executed
assert_no_notification
end

it "ignores SignalException of type SIGTERM" do
expect(run(custom_crash_cmd("sigterm"))).not_to be_successfully_executed
assert_no_notification
end

it "reports SignalException of type other than SIGTERM" do
expect(run(custom_crash_cmd("hup"))).not_to be_successfully_executed
assert_notification('error' => {'class' => 'SignalException', 'message' => 'SignalException: SIGHUP'})
end

context "at_exit is disabled" do
before do
set_environment_variable('HONEYBADGER_EXCEPTIONS_NOTIFY_AT_EXIT', 'false')
Expand Down
11 changes: 11 additions & 0 deletions spec/fixtures/ruby_custom_crash.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
require 'honeybadger'

CRASHES = {
"system_exit" => ->{ exit -1 },
"sigterm" => ->{ raise SignalException, "TERM" },
"hup" => ->{ raise SignalException, "SIGHUP" },
}

crash_type = ARGV.first || (raise "Invalid argument")

(CRASHES[crash_type] || ->{ raise "Invalid crash type" }).()

0 comments on commit c21d5c1

Please sign in to comment.