Skip to content

Commit

Permalink
Make Timeout::Error#exception with multiple arguments not ignore argu…
Browse files Browse the repository at this point in the history
…ments

This makes:

  raise(Timeout::Error.new("hello"), "world")

raise a TimeoutError instance with "world" as the message instead
of "hello", for consistency with other Ruby exception classes.

This required some internal changes to keep the tests passing.

Fixes [Bug #17812]
  • Loading branch information
jeremyevans committed May 3, 2021
1 parent 554660d commit 952154d
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
9 changes: 6 additions & 3 deletions lib/timeout.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,19 +32,21 @@ class Error < RuntimeError
def self.catch(*args)
exc = new(*args)
exc.instance_variable_set(:@thread, Thread.current)
::Kernel.catch(exc) {yield exc}
catch_value = Object.new
exc.instance_variable_set(:@catch_value, catch_value)
::Kernel.catch(catch_value) {yield exc}
end

def exception(*)
# TODO: use Fiber.current to see if self can be thrown
if self.thread == Thread.current
bt = caller
begin
throw(self, bt)
throw(@catch_value, bt)
rescue UncaughtThrowError
end
end
self
super
end
end

Expand Down Expand Up @@ -115,6 +117,7 @@ def timeout(sec, klass = nil, message = nil, &block) #:yield: +sec+
begin
bl.call(klass)
rescue klass => e
message = e.message
bt = e.backtrace
end
else
Expand Down
8 changes: 8 additions & 0 deletions test/test_timeout.rb
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,14 @@ def test_exit_exception
end
end

def test_raise_with_message
bug17812 = '[ruby-core:103502] [Bug #17812]: Timeout::Error doesn\'t let two-argument raise() set a new message'
exc = Timeout::Error.new('foo')
assert_raise_with_message(Timeout::Error, 'bar', bug17812) do
raise exc, 'bar'
end
end

def test_enumerator_next
bug9380 = '[ruby-dev:47872] [Bug #9380]: timeout in Enumerator#next'
e = (o=Object.new).to_enum
Expand Down

0 comments on commit 952154d

Please sign in to comment.