Skip to content

Commit 628e78d

Browse files
committed
merge revision(s) 44517,44518,44519,44523: [Backport ruby#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 ruby#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 ruby#9380] * lib/timeout.rb (Timeout#timeout): initialize ExitException with message for the fallback case. git-svn-id: svn+ssh://svn.ruby-lang.org/ruby/branches/ruby_2_1@44841 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
1 parent a933f15 commit 628e78d

4 files changed

Lines changed: 78 additions & 10 deletions

File tree

ChangeLog

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,26 @@
1+
Wed Feb 5 21:12:02 2014 Nobuyoshi Nakada <nobu@ruby-lang.org>
2+
3+
* lib/timeout.rb (Timeout::ExitException.catch): pass arguments
4+
for new instance.
5+
6+
* lib/timeout.rb (Timeout::ExitException#exception): fallback to
7+
Timeout::Error if couldn't throw. [ruby-dev:47872] [Bug #9380]
8+
9+
* lib/timeout.rb (Timeout#timeout): initialize ExitException with
10+
message for the fallback case.
11+
12+
Wed Feb 5 21:12:02 2014 Nobuyoshi Nakada <nobu@ruby-lang.org>
13+
14+
* lib/timeout.rb (Timeout#timeout): should not rescue ordinarily
15+
raised ExitException, which should not be thrown.
16+
17+
* lib/timeout.rb (Timeout::ExitException.catch): set @thread only if
18+
it ought to be caught.
19+
20+
* lib/timeout.rb (Timeout#timeout): when a custom exception is given,
21+
no instance is needed to be caught, so defer creating new instance
22+
until it is raised. [ruby-core:59511] [Bug #9354]
23+
124
Wed Feb 5 17:55:28 2014 Aman Gupta <ruby@tmm1.net>
225

326
* array.c (ary_add_hash): Fix consistency issue between Array#uniq and

lib/timeout.rb

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,25 @@ module Timeout
2626
class Error < RuntimeError
2727
end
2828
class ExitException < ::Exception # :nodoc:
29-
attr_reader :klass, :thread
29+
attr_reader :thread
3030

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

3738
def exception(*)
38-
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
3948
self
4049
end
4150
end
@@ -67,7 +76,7 @@ def timeout(sec, klass = nil) #:yield: +sec+
6776
return yield(sec) if sec == nil or sec.zero?
6877
message = "execution expired"
6978
e = Error
70-
bt = catch((klass||ExitException).new) do |exception|
79+
bl = proc do |exception|
7180
begin
7281
x = Thread.current
7382
y = Thread.start {
@@ -80,15 +89,22 @@ def timeout(sec, klass = nil) #:yield: +sec+
8089
end
8190
}
8291
return yield(sec)
83-
rescue (klass||ExitException) => e
84-
e.backtrace
8592
ensure
8693
if y
8794
y.kill
8895
y.join # make sure y is dead.
8996
end
9097
end
9198
end
99+
if klass
100+
begin
101+
bl.call(klass)
102+
rescue klass => e
103+
bt = e.backtrace
104+
end
105+
else
106+
bt = ExitException.catch(message, &bl)
107+
end
92108
rej = /\A#{Regexp.quote(__FILE__)}:#{__LINE__-4}\z/o
93109
bt.reject! {|m| rej =~ m}
94110
level = -caller(CALLER_OFFSET).size

test/test_timeout.rb

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,4 +57,33 @@ def test_rescue_exit
5757
end
5858
assert_raise_with_message(exc, /execution expired/) {raise e if e}
5959
end
60+
61+
def test_custom_exception
62+
bug9354 = '[ruby-core:59511] [Bug #9354]'
63+
err = Class.new(StandardError) do
64+
def initialize(msg) super end
65+
end
66+
assert_nothing_raised(ArgumentError, bug9354) do
67+
assert_equal(:ok, timeout(100, err) {:ok})
68+
end
69+
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
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
6089
end

version.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#define RUBY_VERSION "2.1.1"
22
#define RUBY_RELEASE_DATE "2014-02-05"
3-
#define RUBY_PATCHLEVEL 18
3+
#define RUBY_PATCHLEVEL 19
44

55
#define RUBY_RELEASE_YEAR 2014
66
#define RUBY_RELEASE_MONTH 2

0 commit comments

Comments
 (0)