From 5e4bc4f79045c72f4543b477d9959a648f8ca0a5 Mon Sep 17 00:00:00 2001 From: Ethan Date: Mon, 8 Jul 2024 14:16:56 -0700 Subject: [PATCH 1/2] Change `rescue Exception` to limit rescuing critical ones Taken from https://github.com/rspec/rspec-support/blob/v3.13.0/lib/rspec/support.rb#L145-L153 --- lib/rake.rb | 12 +++++++++++- lib/rake/application.rb | 2 +- lib/rake/promise.rb | 2 +- lib/rake/thread_pool.rb | 2 +- 4 files changed, 14 insertions(+), 4 deletions(-) diff --git a/lib/rake.rb b/lib/rake.rb index 77cb0c73a..d74b1b022 100644 --- a/lib/rake.rb +++ b/lib/rake.rb @@ -21,7 +21,17 @@ # IN THE SOFTWARE. #++ -module Rake; end +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 +end require_relative "rake/version" diff --git a/lib/rake/application.rb b/lib/rake/application.rb index 33ca872dd..a74cdacc9 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/thread_pool.rb b/lib/rake/thread_pool.rb index d791caa6e..355d22b3c 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. " + From b33d4ed9b10063c0b768db79fdd0322b90197acf Mon Sep 17 00:00:00 2001 From: Ethan Date: Mon, 8 Jul 2024 14:21:58 -0700 Subject: [PATCH 2/2] Configure Rubocop against `rescue Exception` like https://github.com/rspec/rspec-support/blob/v3.13.0/.rubocop_rspec_base.yml#L95-L97 --- .rubocop.yml | 4 ++++ 1 file changed, 4 insertions(+) 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