diff --git a/.rubocop.yml b/.rubocop.yml index 9f76014d7..d957753b4 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -55,3 +55,7 @@ Layout/CaseIndentation: Layout/EndAlignment: Enabled: true EnforcedStyleAlignWith: variable + +# Exceptions should be rescued with `AllExceptionsExceptOnesWeMustNotRescue` +Lint/RescueException: + Enabled: true diff --git a/lib/rake/application.rb b/lib/rake/application.rb index 87ae47b32..06fa4b22f 100644 --- a/lib/rake/application.rb +++ b/lib/rake/application.rb @@ -218,7 +218,7 @@ def standard_exception_handling # :nodoc: rescue OptionParser::InvalidOption => ex $stderr.puts ex.message exit(false) - rescue Exception => ex + rescue AllExceptionsExceptOnesWeMustNotRescue => ex # Exit with error message display_error_message(ex) exit_because_of_exception(ex) diff --git a/lib/rake/promise.rb b/lib/rake/promise.rb index f45af4f3a..6c0b8614e 100644 --- a/lib/rake/promise.rb +++ b/lib/rake/promise.rb @@ -62,7 +62,7 @@ def chore stat :will_execute, item_id: object_id begin @result = @block.call(*@args) - rescue Exception => e + rescue AllExceptionsExceptOnesWeMustNotRescue => e @error = e end stat :did_execute, item_id: object_id diff --git a/lib/rake/rake_module.rb b/lib/rake/rake_module.rb index 03c295624..5057bb2b6 100644 --- a/lib/rake/rake_module.rb +++ b/lib/rake/rake_module.rb @@ -2,6 +2,15 @@ require "rake/application" module Rake + module AllExceptionsExceptOnesWeMustNotRescue + # These exceptions are dangerous to rescue as rescuing them + # would interfere with things we should not interfere with. + AVOID_RESCUING = [NoMemoryError, SignalException, Interrupt, SystemExit] + + def self.===(exception) + AVOID_RESCUING.none? { |ar| ar === exception } + end + end class << self # Current Rake Application diff --git a/lib/rake/thread_pool.rb b/lib/rake/thread_pool.rb index ea9c0ae30..a8b7f819d 100644 --- a/lib/rake/thread_pool.rb +++ b/lib/rake/thread_pool.rb @@ -47,7 +47,7 @@ def join stat :joining @join_cond.wait unless @threads.empty? stat :joined - rescue Exception => e + rescue AllExceptionsExceptOnesWeMustNotRescue => e stat :joined $stderr.puts e $stderr.print "Queue contains #{@queue.size} items. " +