diff options
author | naruse <naruse@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2014-02-05 12:43:19 +0000 |
---|---|---|
committer | naruse <naruse@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2014-02-05 12:43:19 +0000 |
commit | c00e288936dbcfcaa3b9eaadbd14ab94f778389e (patch) | |
tree | 03ed3d6676ce0500d7e58244ab560a54cea796e8 /lib | |
parent | c0bdb2511d898cb875e75dc1e7f774112e5341f9 (diff) |
merge revision(s) 44517,44518,44519,44523: [Backport #9354]
* lib/timeout.rb (Timeout#timeout): when a custom exception is given,
no instance is needed to be caught, so defer creating new instance
until it is raised. [ruby-core:59511] [Bug #9354]
* 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::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 #9380]
* lib/timeout.rb (Timeout#timeout): initialize ExitException with
message for the fallback case.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_2_1@44841 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'lib')
-rw-r--r-- | lib/timeout.rb | 34 |
1 files changed, 25 insertions, 9 deletions
diff --git a/lib/timeout.rb b/lib/timeout.rb index ad951d2ffa..d805dce2a3 100644 --- a/lib/timeout.rb +++ b/lib/timeout.rb @@ -26,16 +26,25 @@ module Timeout class Error < RuntimeError end class ExitException < ::Exception # :nodoc: - attr_reader :klass, :thread + attr_reader :thread - def initialize(*) - super - @thread = Thread.current - freeze + def self.catch(*args) + exc = new(*args) + exc.instance_variable_set(:@thread, Thread.current) + exc.freeze + ::Kernel.catch(exc) {yield exc} end def exception(*) - throw(self, caller) if self.thread == Thread.current + if self.thread == Thread.current + bt = caller + begin + throw(self, bt) + rescue ArgumentError => e + raise unless e.message.start_with?("uncaught throw") + raise Error, message, backtrace + end + end self end end @@ -67,7 +76,7 @@ module Timeout return yield(sec) if sec == nil or sec.zero? message = "execution expired" e = Error - bt = catch((klass||ExitException).new) do |exception| + bl = proc do |exception| begin x = Thread.current y = Thread.start { @@ -80,8 +89,6 @@ module Timeout end } return yield(sec) - rescue (klass||ExitException) => e - e.backtrace ensure if y y.kill @@ -89,6 +96,15 @@ module Timeout end end end + if klass + begin + bl.call(klass) + rescue klass => e + bt = e.backtrace + end + else + bt = ExitException.catch(message, &bl) + end rej = /\A#{Regexp.quote(__FILE__)}:#{__LINE__-4}\z/o bt.reject! {|m| rej =~ m} level = -caller(CALLER_OFFSET).size |