Skip to content

Commit 8e92ab1

Browse files
nobutmm1
authored andcommitted
lib/timeout.rb: fallback to Timeout::Error
* lib/timeout.rb (Timeout::ExitException.catch): pass arguments for new instance. * lib/timeout.rb (Timeout::ExitException#exception): fallback to Timeout::Error if couldn't throw. [ruby-dev:47872] [Bug ruby#9380] * lib/timeout.rb (Timeout#timeout): initialize ExitException with message for the fallback case. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@44523 b2dd03c8-39d4-4d8f-98ff-823fe69b080e Conflicts: ChangeLog
1 parent 17614ef commit 8e92ab1

File tree

2 files changed

+23
-4
lines changed

2 files changed

+23
-4
lines changed

lib/timeout.rb

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,23 @@ class Error < RuntimeError
2828
class ExitException < ::Exception # :nodoc:
2929
attr_reader :thread
3030

31-
def self.catch
32-
exc = new
31+
def self.catch(*args)
32+
exc = new(*args)
3333
exc.instance_variable_set(:@thread, Thread.current)
3434
exc.freeze
3535
::Kernel.catch(exc) {yield exc}
3636
end
3737

3838
def exception(*)
39-
throw(self, caller) if self.thread == Thread.current
39+
if self.thread == Thread.current
40+
bt = caller
41+
begin
42+
throw(self, bt)
43+
rescue ArgumentError => e
44+
raise unless e.message.start_with?("uncaught throw")
45+
raise Error, message, backtrace
46+
end
47+
end
4048
self
4149
end
4250
end
@@ -95,7 +103,7 @@ def timeout(sec, klass = nil) #:yield: +sec+
95103
bt = e.backtrace
96104
end
97105
else
98-
bt = ExitException.catch(&bl)
106+
bt = ExitException.catch(message, &bl)
99107
end
100108
rej = /\A#{Regexp.quote(__FILE__)}:#{__LINE__-4}\z/o
101109
bt.reject! {|m| rej =~ m}

test/test_timeout.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,4 +75,15 @@ def test_exit_exception
7575
end
7676
end
7777
end
78+
79+
def test_enumerator_next
80+
bug9380 = '[ruby-dev:47872] [Bug #9380]: timeout in Enumerator#next'
81+
e = (o=Object.new).to_enum
82+
def o.each
83+
sleep
84+
end
85+
assert_raise_with_message(Timeout::Error, 'execution expired', bug9380) do
86+
Timeout.timeout(0.01) {e.next}
87+
end
88+
end
7889
end

0 commit comments

Comments
 (0)