timeout 0.4.0 → 0.6.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 19bdc6e927cfc023688f6d4e08a55b1d483257bd450db6e203e1965158bb7ed4
4
- data.tar.gz: 742db38b61ab633ca1baef82a73338b0e2d9e4a0d10bdf3908fc8ed87d04f4c2
3
+ metadata.gz: 31442005d41eeaddb46916ff83e27dc0198a3a0509c463d47d1fd4a9c7e0ec6d
4
+ data.tar.gz: bf8dff95c8356a47b513568f3c6890d2c1a9a74a6a364f5fa38d5e8f4b8e3c87
5
5
  SHA512:
6
- metadata.gz: 6836956b4e59cebd6a14d0a9d3857c700a1bd65a566ff1d41be3657631b3e33a443910d4123dae1b81eaa81198896442f5c0f2a32e3b7182ad2144fdc39cbcfc
7
- data.tar.gz: a7a4a1a176dcbf09297133476914684db3ae8c77c37d510af6d6200279f17fa0b1829c6b2ad19fa29b6be1c62f9b60aba6e362c47affcd71ec36ce934e8a391a
6
+ metadata.gz: 3630c0c2b33659c650a9f4af7d5983dd303d174431c7d7c137292e4c702ac95afaab7434b4061bd6b013edc10343fa141e0969ac4188f671cbc84a14583c9986
7
+ data.tar.gz: b835d20514bda974bd0f5cd6473213bd429a9887503802dc2882ab25ae00be502f5df84642f305eb348866e33c9357130b985012d936ed48eebfea800458853c
data/.document ADDED
@@ -0,0 +1,3 @@
1
+ LICENSE.txt
2
+ README.md
3
+ lib/
@@ -4,10 +4,10 @@ Redistribution and use in source and binary forms, with or without
4
4
  modification, are permitted provided that the following conditions
5
5
  are met:
6
6
  1. Redistributions of source code must retain the above copyright
7
- notice, this list of conditions and the following disclaimer.
7
+ notice, this list of conditions and the following disclaimer.
8
8
  2. Redistributions in binary form must reproduce the above copyright
9
- notice, this list of conditions and the following disclaimer in the
10
- documentation and/or other materials provided with the distribution.
9
+ notice, this list of conditions and the following disclaimer in the
10
+ documentation and/or other materials provided with the distribution.
11
11
 
12
12
  THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
13
13
  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
data/COPYING ADDED
@@ -0,0 +1,56 @@
1
+ Ruby is copyrighted free software by Yukihiro Matsumoto <matz@netlab.jp>.
2
+ You can redistribute it and/or modify it under either the terms of the
3
+ 2-clause BSDL (see the file BSDL), or the conditions below:
4
+
5
+ 1. You may make and give away verbatim copies of the source form of the
6
+ software without restriction, provided that you duplicate all of the
7
+ original copyright notices and associated disclaimers.
8
+
9
+ 2. You may modify your copy of the software in any way, provided that
10
+ you do at least ONE of the following:
11
+
12
+ a. place your modifications in the Public Domain or otherwise
13
+ make them Freely Available, such as by posting said
14
+ modifications to Usenet or an equivalent medium, or by allowing
15
+ the author to include your modifications in the software.
16
+
17
+ b. use the modified software only within your corporation or
18
+ organization.
19
+
20
+ c. give non-standard binaries non-standard names, with
21
+ instructions on where to get the original software distribution.
22
+
23
+ d. make other distribution arrangements with the author.
24
+
25
+ 3. You may distribute the software in object code or binary form,
26
+ provided that you do at least ONE of the following:
27
+
28
+ a. distribute the binaries and library files of the software,
29
+ together with instructions (in the manual page or equivalent)
30
+ on where to get the original distribution.
31
+
32
+ b. accompany the distribution with the machine-readable source of
33
+ the software.
34
+
35
+ c. give non-standard binaries non-standard names, with
36
+ instructions on where to get the original software distribution.
37
+
38
+ d. make other distribution arrangements with the author.
39
+
40
+ 4. You may modify and include the part of the software into any other
41
+ software (possibly commercial). But some files in the distribution
42
+ are not written by the author, so that they are not under these terms.
43
+
44
+ For the list of those files and their copying conditions, see the
45
+ file LEGAL.
46
+
47
+ 5. The scripts and library files supplied as input to or produced as
48
+ output from the software do not automatically fall under the
49
+ copyright of the software, but belong to whomever generated them,
50
+ and may be sold commercially, and may be aggregated with this
51
+ software.
52
+
53
+ 6. THIS SOFTWARE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR
54
+ IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
55
+ WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
56
+ PURPOSE.
data/README.md CHANGED
@@ -3,10 +3,6 @@
3
3
  Timeout provides a way to auto-terminate a potentially long-running
4
4
  operation if it hasn't finished in a fixed amount of time.
5
5
 
6
- Previous versions didn't use a module for namespacing, however
7
- #timeout is provided for backwards compatibility. You
8
- should prefer Timeout.timeout instead.
9
-
10
6
  ## Installation
11
7
 
12
8
  Add this line to your application's Gemfile:
@@ -27,7 +23,7 @@ Or install it yourself as:
27
23
 
28
24
  ```ruby
29
25
  require 'timeout'
30
- status = Timeout::timeout(5) {
26
+ status = Timeout.timeout(5) {
31
27
  # Something that should be interrupted if it takes more than 5 seconds...
32
28
  }
33
29
  ```
data/lib/timeout.rb CHANGED
@@ -4,7 +4,7 @@
4
4
  # == Synopsis
5
5
  #
6
6
  # require 'timeout'
7
- # status = Timeout::timeout(5) {
7
+ # status = Timeout.timeout(5) {
8
8
  # # Something that should be interrupted if it takes more than 5 seconds...
9
9
  # }
10
10
  #
@@ -13,28 +13,25 @@
13
13
  # Timeout provides a way to auto-terminate a potentially long-running
14
14
  # operation if it hasn't finished in a fixed amount of time.
15
15
  #
16
- # Previous versions didn't use a module for namespacing, however
17
- # #timeout is provided for backwards compatibility. You
18
- # should prefer Timeout.timeout instead.
19
- #
20
16
  # == Copyright
21
17
  #
22
18
  # Copyright:: (C) 2000 Network Applied Communication Laboratory, Inc.
23
19
  # Copyright:: (C) 2000 Information-technology Promotion Agency, Japan
24
20
 
25
21
  module Timeout
26
- VERSION = "0.4.0"
22
+ # The version
23
+ VERSION = "0.6.1"
27
24
 
28
- # Internal error raised to when a timeout is triggered.
25
+ # Internal exception raised to when a timeout is triggered.
29
26
  class ExitException < Exception
30
- def exception(*)
27
+ def exception(*) # :nodoc:
31
28
  self
32
29
  end
33
30
  end
34
31
 
35
32
  # Raised by Timeout.timeout when the block times out.
36
33
  class Error < RuntimeError
37
- def self.handle_timeout(message)
34
+ def self.handle_timeout(message) # :nodoc:
38
35
  exc = ExitException.new(message)
39
36
 
40
37
  begin
@@ -47,12 +44,101 @@ module Timeout
47
44
  end
48
45
 
49
46
  # :stopdoc:
50
- CONDVAR = ConditionVariable.new
51
- QUEUE = Queue.new
52
- QUEUE_MUTEX = Mutex.new
53
- TIMEOUT_THREAD_MUTEX = Mutex.new
54
- @timeout_thread = nil
55
- private_constant :CONDVAR, :QUEUE, :QUEUE_MUTEX, :TIMEOUT_THREAD_MUTEX
47
+
48
+ # We keep a private reference so that time mocking libraries won't break Timeout.
49
+ GET_TIME = Process.method(:clock_gettime)
50
+ if defined?(Ractor.make_shareable)
51
+ # Ractor.make_shareable(Method) only works on Ruby 4+
52
+ Ractor.make_shareable(GET_TIME) rescue nil
53
+ end
54
+ private_constant :GET_TIME
55
+
56
+ class State
57
+ def initialize
58
+ @condvar = ConditionVariable.new
59
+ @queue = Queue.new
60
+ @queue_mutex = Mutex.new
61
+
62
+ @timeout_thread = nil
63
+ @timeout_thread_mutex = Mutex.new
64
+ end
65
+
66
+ if defined?(Ractor.store_if_absent) && defined?(Ractor.shareable?) && Ractor.shareable?(GET_TIME)
67
+ # Ractor support if
68
+ # 1. Ractor.store_if_absent is available
69
+ # 2. Method object can be shareable (4.0~)
70
+ def self.instance
71
+ Ractor.store_if_absent :timeout_gem_state do
72
+ State.new
73
+ end
74
+ end
75
+ else
76
+ GLOBAL_STATE = State.new
77
+
78
+ def self.instance
79
+ GLOBAL_STATE
80
+ end
81
+ end
82
+
83
+ def create_timeout_thread
84
+ # Threads unexpectedly inherit the interrupt mask: https://github.com/ruby/timeout/issues/41
85
+ # So reset the interrupt mask to the default one for the timeout thread
86
+ Thread.handle_interrupt(Object => :immediate) do
87
+ watcher = Thread.new do
88
+ requests = []
89
+ while true
90
+ until @queue.empty? and !requests.empty? # wait to have at least one request
91
+ req = @queue.pop
92
+ requests << req unless req.done?
93
+ end
94
+ closest_deadline = requests.min_by(&:deadline).deadline
95
+
96
+ now = 0.0
97
+ @queue_mutex.synchronize do
98
+ while (now = GET_TIME.call(Process::CLOCK_MONOTONIC)) < closest_deadline and @queue.empty?
99
+ @condvar.wait(@queue_mutex, closest_deadline - now)
100
+ end
101
+ end
102
+
103
+ requests.each do |req|
104
+ req.interrupt if req.expired?(now)
105
+ end
106
+ requests.reject!(&:done?)
107
+ end
108
+ end
109
+
110
+ if !watcher.group.enclosed? && (!defined?(Ractor.main?) || Ractor.main?)
111
+ ThreadGroup::Default.add(watcher)
112
+ end
113
+
114
+ watcher.name = "Timeout stdlib thread"
115
+ watcher.thread_variable_set(:"\0__detached_thread__", true)
116
+ watcher
117
+ end
118
+ end
119
+
120
+ def ensure_timeout_thread_created
121
+ unless @timeout_thread&.alive?
122
+ # If the Mutex is already owned we are in a signal handler.
123
+ # In that case, just return and let the main thread create the Timeout thread.
124
+ return if @timeout_thread_mutex.owned?
125
+
126
+ Sync.synchronize @timeout_thread_mutex do
127
+ unless @timeout_thread&.alive?
128
+ @timeout_thread = create_timeout_thread
129
+ end
130
+ end
131
+ end
132
+ end
133
+
134
+ def add_request(request)
135
+ Sync.synchronize @queue_mutex do
136
+ @queue << request
137
+ @condvar.signal
138
+ end
139
+ end
140
+ end
141
+ private_constant :State
56
142
 
57
143
  class Request
58
144
  attr_reader :deadline
@@ -67,6 +153,7 @@ module Timeout
67
153
  @done = false # protected by @mutex
68
154
  end
69
155
 
156
+ # Only called by the timeout thread, so does not need Sync.synchronize
70
157
  def done?
71
158
  @mutex.synchronize do
72
159
  @done
@@ -77,6 +164,7 @@ module Timeout
77
164
  now >= @deadline
78
165
  end
79
166
 
167
+ # Only called by the timeout thread, so does not need Sync.synchronize
80
168
  def interrupt
81
169
  @mutex.synchronize do
82
170
  unless @done
@@ -87,105 +175,128 @@ module Timeout
87
175
  end
88
176
 
89
177
  def finished
90
- @mutex.synchronize do
178
+ Sync.synchronize @mutex do
91
179
  @done = true
92
180
  end
93
181
  end
94
182
  end
95
183
  private_constant :Request
96
184
 
97
- def self.create_timeout_thread
98
- watcher = Thread.new do
99
- requests = []
100
- while true
101
- until QUEUE.empty? and !requests.empty? # wait to have at least one request
102
- req = QUEUE.pop
103
- requests << req unless req.done?
104
- end
105
- closest_deadline = requests.min_by(&:deadline).deadline
106
-
107
- now = 0.0
108
- QUEUE_MUTEX.synchronize do
109
- while (now = GET_TIME.call(Process::CLOCK_MONOTONIC)) < closest_deadline and QUEUE.empty?
110
- CONDVAR.wait(QUEUE_MUTEX, closest_deadline - now)
111
- end
112
- end
113
-
114
- requests.each do |req|
115
- req.interrupt if req.expired?(now)
116
- end
117
- requests.reject!(&:done?)
118
- end
119
- end
120
- ThreadGroup::Default.add(watcher) unless watcher.group.enclosed?
121
- watcher.name = "Timeout stdlib thread"
122
- watcher.thread_variable_set(:"\0__detached_thread__", true)
123
- watcher
124
- end
125
- private_class_method :create_timeout_thread
126
-
127
- def self.ensure_timeout_thread_created
128
- unless @timeout_thread and @timeout_thread.alive?
129
- TIMEOUT_THREAD_MUTEX.synchronize do
130
- unless @timeout_thread and @timeout_thread.alive?
131
- @timeout_thread = create_timeout_thread
132
- end
185
+ module Sync
186
+ # Calls mutex.synchronize(&block) but if that fails on CRuby due to being in a trap handler,
187
+ # run mutex.synchronize(&block) in a separate Thread instead.
188
+ def self.synchronize(mutex, &block)
189
+ begin
190
+ mutex.synchronize(&block)
191
+ rescue ThreadError => e
192
+ raise e unless e.message == "can't be called from trap context"
193
+ # Workaround CRuby issue https://bugs.ruby-lang.org/issues/19473
194
+ # which raises on Mutex#synchronize in trap handler.
195
+ # It's expensive to create a Thread just for this,
196
+ # but better than failing.
197
+ Thread.new {
198
+ mutex.synchronize(&block)
199
+ }.join
133
200
  end
134
201
  end
135
202
  end
136
-
137
- # We keep a private reference so that time mocking libraries won't break
138
- # Timeout.
139
- GET_TIME = Process.method(:clock_gettime)
140
- private_constant :GET_TIME
203
+ private_constant :Sync
141
204
 
142
205
  # :startdoc:
143
206
 
144
- # Perform an operation in a block, raising an error if it takes longer than
207
+ # Perform an operation in a block, raising an exception if it takes longer than
145
208
  # +sec+ seconds to complete.
146
209
  #
147
- # +sec+:: Number of seconds to wait for the block to terminate. Any number
148
- # may be used, including Floats to specify fractional seconds. A
210
+ # +sec+:: Number of seconds to wait for the block to terminate. Any non-negative number
211
+ # or nil may be used, including Floats to specify fractional seconds. A
149
212
  # value of 0 or +nil+ will execute the block without any timeout.
213
+ # Any negative number will raise an ArgumentError.
150
214
  # +klass+:: Exception Class to raise if the block fails to terminate
151
- # in +sec+ seconds. Omitting will use the default, Timeout::Error
215
+ # in +sec+ seconds. Omitting will use the default, Timeout::Error.
152
216
  # +message+:: Error message to raise with Exception Class.
153
- # Omitting will use the default, "execution expired"
217
+ # Omitting will use the default, <tt>"execution expired"</tt>.
154
218
  #
155
219
  # Returns the result of the block *if* the block completed before
156
- # +sec+ seconds, otherwise throws an exception, based on the value of +klass+.
220
+ # +sec+ seconds, otherwise raises an exception, based on the value of +klass+.
157
221
  #
158
- # The exception thrown to terminate the given block cannot be rescued inside
159
- # the block unless +klass+ is given explicitly. However, the block can use
160
- # ensure to prevent the handling of the exception. For that reason, this
161
- # method cannot be relied on to enforce timeouts for untrusted blocks.
222
+ # The exception raised to terminate the given block is the given +klass+, or
223
+ # Timeout::ExitException if +klass+ is not given. The reason for that behavior
224
+ # is that Timeout::Error inherits from RuntimeError and might be caught unexpectedly by +rescue+.
225
+ # Timeout::ExitException inherits from Exception so it will only be rescued by <tt>rescue Exception</tt>.
226
+ # Note that the Timeout::ExitException is translated to a Timeout::Error once it reaches the Timeout.timeout call,
227
+ # so outside that call it will be a Timeout::Error.
228
+ #
229
+ # In general, be aware that the code block may rescue the exception, and in such a case not respect the timeout.
230
+ # Also, the block can use +ensure+ to prevent the handling of the exception.
231
+ # For those reasons, this method cannot be relied on to enforce timeouts for untrusted blocks.
162
232
  #
163
233
  # If a scheduler is defined, it will be used to handle the timeout by invoking
164
- # Scheduler#timeout_after.
234
+ # Fiber::Scheduler#timeout_after.
165
235
  #
166
236
  # Note that this is both a method of module Timeout, so you can <tt>include
167
237
  # Timeout</tt> into your classes so they have a #timeout method, as well as
168
238
  # a module method, so you can call it directly as Timeout.timeout().
169
- def timeout(sec, klass = nil, message = nil, &block) #:yield: +sec+
239
+ #
240
+ # ==== Ensuring the exception does not fire inside ensure blocks
241
+ #
242
+ # When using Timeout.timeout, it can be desirable to ensure the timeout exception does not fire inside an +ensure+ block.
243
+ # The simplest and best way to do so is to put the Timeout.timeout call inside the body of the +begin+/+ensure+/+end+:
244
+ #
245
+ # begin
246
+ # Timeout.timeout(sec) { some_long_operation }
247
+ # ensure
248
+ # cleanup # safe, cannot be interrupted by timeout
249
+ # end
250
+ #
251
+ # If that is not feasible, e.g. if there are +ensure+ blocks inside +some_long_operation+,
252
+ # they need to not be interrupted by timeout, and it's not possible to move these ensure blocks outside,
253
+ # one can use Thread.handle_interrupt to delay the timeout exception like so:
254
+ #
255
+ # Thread.handle_interrupt(Timeout::Error => :never) {
256
+ # Timeout.timeout(sec, Timeout::Error) do
257
+ # setup # timeout cannot happen here, no matter how long it takes
258
+ # Thread.handle_interrupt(Timeout::Error => :immediate) {
259
+ # some_long_operation # timeout can happen here
260
+ # }
261
+ # ensure
262
+ # cleanup # timeout cannot happen here, no matter how long it takes
263
+ # end
264
+ # }
265
+ #
266
+ # An important thing to note is the need to pass an exception +klass+ to Timeout.timeout,
267
+ # otherwise it does not work. Specifically, using <tt>Thread.handle_interrupt(Timeout::ExitException => ...)</tt>
268
+ # is unsupported and causes subtle errors like raising the wrong exception outside the block, do not use that.
269
+ #
270
+ # Note that Thread.handle_interrupt is somewhat dangerous because if setup or cleanup hangs
271
+ # then the current thread will hang too and the timeout will never fire.
272
+ # Also note the block might run for longer than +sec+ seconds:
273
+ # e.g. +some_long_operation+ executes for +sec+ seconds + whatever time cleanup takes.
274
+ #
275
+ # If you want the timeout to only happen on blocking operations, one can use +:on_blocking+
276
+ # instead of +:immediate+. However, that means if the block uses no blocking operations after +sec+ seconds,
277
+ # the block will not be interrupted.
278
+ def self.timeout(sec, klass = nil, message = nil, &block) #:yield: +sec+
170
279
  return yield(sec) if sec == nil or sec.zero?
280
+ raise ArgumentError, "Timeout sec must be a non-negative number" if 0 > sec
171
281
 
172
282
  message ||= "execution expired"
173
283
 
174
284
  if Fiber.respond_to?(:current_scheduler) && (scheduler = Fiber.current_scheduler)&.respond_to?(:timeout_after)
175
- return scheduler.timeout_after(sec, klass || Error, message, &block)
176
- end
177
-
178
- Timeout.ensure_timeout_thread_created
179
- perform = Proc.new do |exc|
180
- request = Request.new(Thread.current, sec, exc, message)
181
- QUEUE_MUTEX.synchronize do
182
- QUEUE << request
183
- CONDVAR.signal
285
+ perform = Proc.new do |exc|
286
+ scheduler.timeout_after(sec, exc, message, &block)
184
287
  end
185
- begin
186
- return yield(sec)
187
- ensure
188
- request.finished
288
+ else
289
+ state = State.instance
290
+ state.ensure_timeout_thread_created
291
+
292
+ perform = Proc.new do |exc|
293
+ request = Request.new(Thread.current, sec, exc, message)
294
+ state.add_request(request)
295
+ begin
296
+ return yield(sec)
297
+ ensure
298
+ request.finished
299
+ end
189
300
  end
190
301
  end
191
302
 
@@ -195,5 +306,9 @@ module Timeout
195
306
  Error.handle_timeout(message, &perform)
196
307
  end
197
308
  end
198
- module_function :timeout
309
+
310
+ # See Timeout.timeout
311
+ private def timeout(*args, &block)
312
+ Timeout.timeout(*args, &block)
313
+ end
199
314
  end
data/timeout.gemspec CHANGED
@@ -20,6 +20,9 @@ Gem::Specification.new do |spec|
20
20
 
21
21
  spec.metadata["homepage_uri"] = spec.homepage
22
22
  spec.metadata["source_code_uri"] = spec.homepage
23
+ spec.metadata["changelog_uri"] = spec.homepage + "/releases"
24
+
25
+ spec.required_ruby_version = '>= 2.6.0'
23
26
 
24
27
  spec.files = Dir.chdir(__dir__) do
25
28
  `git ls-files -z`.split("\x0").reject do |f|
metadata CHANGED
@@ -1,14 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: timeout
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.6.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yukihiro Matsumoto
8
- autorequire:
9
8
  bindir: bin
10
9
  cert_chain: []
11
- date: 2023-06-23 00:00:00.000000000 Z
10
+ date: 2026-03-09 00:00:00.000000000 Z
12
11
  dependencies: []
13
12
  description: Auto-terminate potentially long-running operations in Ruby.
14
13
  email:
@@ -17,8 +16,10 @@ executables: []
17
16
  extensions: []
18
17
  extra_rdoc_files: []
19
18
  files:
19
+ - ".document"
20
+ - BSDL
21
+ - COPYING
20
22
  - Gemfile
21
- - LICENSE.txt
22
23
  - README.md
23
24
  - lib/timeout.rb
24
25
  - timeout.gemspec
@@ -29,7 +30,7 @@ licenses:
29
30
  metadata:
30
31
  homepage_uri: https://github.com/ruby/timeout
31
32
  source_code_uri: https://github.com/ruby/timeout
32
- post_install_message:
33
+ changelog_uri: https://github.com/ruby/timeout/releases
33
34
  rdoc_options: []
34
35
  require_paths:
35
36
  - lib
@@ -37,15 +38,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
37
38
  requirements:
38
39
  - - ">="
39
40
  - !ruby/object:Gem::Version
40
- version: '0'
41
+ version: 2.6.0
41
42
  required_rubygems_version: !ruby/object:Gem::Requirement
42
43
  requirements:
43
44
  - - ">="
44
45
  - !ruby/object:Gem::Version
45
46
  version: '0'
46
47
  requirements: []
47
- rubygems_version: 3.5.0.dev
48
- signing_key:
48
+ rubygems_version: 3.6.9
49
49
  specification_version: 4
50
50
  summary: Auto-terminate potentially long-running operations in Ruby.
51
51
  test_files: []