mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
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
This commit is contained in:
parent
9f5537c5b3
commit
97c0aaea71
3 changed files with 29 additions and 8 deletions
|
@ -1,4 +1,10 @@
|
||||||
Tue Jan 7 12:42:35 2014 Nobuyoshi Nakada <nobu@ruby-lang.org>
|
Tue Jan 7 12:43:06 2014 Nobuyoshi Nakada <nobu@ruby-lang.org>
|
||||||
|
|
||||||
|
* 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.
|
||||||
|
|
||||||
* lib/timeout.rb (Timeout#timeout): when a custom exception is given,
|
* lib/timeout.rb (Timeout#timeout): when a custom exception is given,
|
||||||
no instance is needed to be caught, so defer creating new instance
|
no instance is needed to be caught, so defer creating new instance
|
||||||
|
|
|
@ -28,10 +28,11 @@ module Timeout
|
||||||
class ExitException < ::Exception # :nodoc:
|
class ExitException < ::Exception # :nodoc:
|
||||||
attr_reader :klass, :thread
|
attr_reader :klass, :thread
|
||||||
|
|
||||||
def initialize(*)
|
def self.catch
|
||||||
super
|
exc = new
|
||||||
@thread = Thread.current
|
exc.instance_variable_set(:@thread, Thread.current)
|
||||||
freeze
|
exc.freeze
|
||||||
|
::Kernel.catch(exc) {yield exc}
|
||||||
end
|
end
|
||||||
|
|
||||||
def exception(*)
|
def exception(*)
|
||||||
|
@ -80,8 +81,6 @@ module Timeout
|
||||||
end
|
end
|
||||||
}
|
}
|
||||||
return yield(sec)
|
return yield(sec)
|
||||||
rescue klass => e
|
|
||||||
e.backtrace
|
|
||||||
ensure
|
ensure
|
||||||
if y
|
if y
|
||||||
y.kill
|
y.kill
|
||||||
|
@ -89,7 +88,15 @@ module Timeout
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
bt = klass ? bl.call(klass) : catch((klass = ExitException).new, &bl)
|
if klass
|
||||||
|
begin
|
||||||
|
bl.call(klass)
|
||||||
|
rescue klass => e
|
||||||
|
bt = e.backtrace
|
||||||
|
end
|
||||||
|
else
|
||||||
|
bt = ExitException.catch(&bl)
|
||||||
|
end
|
||||||
rej = /\A#{Regexp.quote(__FILE__)}:#{__LINE__-4}\z/o
|
rej = /\A#{Regexp.quote(__FILE__)}:#{__LINE__-4}\z/o
|
||||||
bt.reject! {|m| rej =~ m}
|
bt.reject! {|m| rej =~ m}
|
||||||
level = -caller(CALLER_OFFSET).size
|
level = -caller(CALLER_OFFSET).size
|
||||||
|
|
|
@ -67,4 +67,12 @@ class TestTimeout < Test::Unit::TestCase
|
||||||
assert_equal(:ok, timeout(100, err) {:ok})
|
assert_equal(:ok, timeout(100, err) {:ok})
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_exit_exception
|
||||||
|
assert_raise_with_message(Timeout::ExitException, "boon") do
|
||||||
|
Timeout.timeout(10, Timeout::ExitException) do
|
||||||
|
raise Timeout::ExitException, "boon"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Add table
Reference in a new issue