Skip to content

Commit b8eacbe

Browse files
nobutmm1
authored andcommitted
timeout.rb: fix for ExitException
* lib/timeout.rb (Timeout#timeout): should not rescue ordinarily raised ExitException, which should not be thrown. * lib/timeout.rb (Timeout::ExitException.catch): set @thread only if it ought to be caught. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@44518 b2dd03c8-39d4-4d8f-98ff-823fe69b080e Conflicts: ChangeLog
1 parent 7c61cbd commit b8eacbe

File tree

2 files changed

+22
-7
lines changed

2 files changed

+22
-7
lines changed

lib/timeout.rb

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,11 @@ class Error < RuntimeError
2828
class ExitException < ::Exception # :nodoc:
2929
attr_reader :klass, :thread
3030

31-
def initialize(*)
32-
super
33-
@thread = Thread.current
34-
freeze
31+
def self.catch
32+
exc = new
33+
exc.instance_variable_set(:@thread, Thread.current)
34+
exc.freeze
35+
::Kernel.catch(exc) {yield exc}
3536
end
3637

3738
def exception(*)
@@ -80,16 +81,22 @@ def timeout(sec, klass = nil) #:yield: +sec+
8081
end
8182
}
8283
return yield(sec)
83-
rescue klass => e
84-
e.backtrace
8584
ensure
8685
if y
8786
y.kill
8887
y.join # make sure y is dead.
8988
end
9089
end
9190
end
92-
bt = klass ? bl.call(klass) : catch((klass = ExitException).new, &bl)
91+
if klass
92+
begin
93+
bl.call(klass)
94+
rescue klass => e
95+
bt = e.backtrace
96+
end
97+
else
98+
bt = ExitException.catch(&bl)
99+
end
93100
rej = /\A#{Regexp.quote(__FILE__)}:#{__LINE__-4}\z/o
94101
bt.reject! {|m| rej =~ m}
95102
level = -caller(CALLER_OFFSET).size

test/test_timeout.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,4 +67,12 @@ def initialize(msg) super end
6767
assert_equal(:ok, timeout(100, err) {:ok})
6868
end
6969
end
70+
71+
def test_exit_exception
72+
assert_raise_with_message(Timeout::ExitException, "boon") do
73+
Timeout.timeout(10, Timeout::ExitException) do
74+
raise Timeout::ExitException, "boon"
75+
end
76+
end
77+
end
7078
end

0 commit comments

Comments
 (0)