timeout 0.6.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.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/timeout.rb +30 -27
  3. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 405263117245d6a9c981a56bc90cad374d50547078dbcfef1cb3fecca78bcece
4
- data.tar.gz: 8eaeb608b52c8f273342746ff88f7e7b9458325e4a30e9f5605d62c749022ec0
3
+ metadata.gz: 31442005d41eeaddb46916ff83e27dc0198a3a0509c463d47d1fd4a9c7e0ec6d
4
+ data.tar.gz: bf8dff95c8356a47b513568f3c6890d2c1a9a74a6a364f5fa38d5e8f4b8e3c87
5
5
  SHA512:
6
- metadata.gz: 1bf761aa173a75b2545088a1304eb8e8c2bc2df0b3223af738fd2c10f2170e4b140644aa9dc036835149ff1ca2cecf866cd782560b9e3856dc63491f6b357657
7
- data.tar.gz: da2ec36df76ea4dc31c532a704d3ae102eac3dc60f4091e9d7dd072144353a1a38aa818ed15d36fb8c8a675b82a9ffa9eae85475e11346faafe48f6318602af8
6
+ metadata.gz: 3630c0c2b33659c650a9f4af7d5983dd303d174431c7d7c137292e4c702ac95afaab7434b4061bd6b013edc10343fa141e0969ac4188f671cbc84a14583c9986
7
+ data.tar.gz: b835d20514bda974bd0f5cd6473213bd429a9887503802dc2882ab25ae00be502f5df84642f305eb348866e33c9357130b985012d936ed48eebfea800458853c
data/lib/timeout.rb CHANGED
@@ -20,7 +20,7 @@
20
20
 
21
21
  module Timeout
22
22
  # The version
23
- VERSION = "0.6.0"
23
+ VERSION = "0.6.1"
24
24
 
25
25
  # Internal exception raised to when a timeout is triggered.
26
26
  class ExitException < Exception
@@ -212,17 +212,17 @@ module Timeout
212
212
  # value of 0 or +nil+ will execute the block without any timeout.
213
213
  # Any negative number will raise an ArgumentError.
214
214
  # +klass+:: Exception Class to raise if the block fails to terminate
215
- # in +sec+ seconds. Omitting will use the default, Timeout::Error
215
+ # in +sec+ seconds. Omitting will use the default, Timeout::Error.
216
216
  # +message+:: Error message to raise with Exception Class.
217
- # Omitting will use the default, "execution expired"
217
+ # Omitting will use the default, <tt>"execution expired"</tt>.
218
218
  #
219
219
  # Returns the result of the block *if* the block completed before
220
220
  # +sec+ seconds, otherwise raises an exception, based on the value of +klass+.
221
221
  #
222
222
  # The exception raised to terminate the given block is the given +klass+, or
223
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 `rescue Exception`.
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
226
  # Note that the Timeout::ExitException is translated to a Timeout::Error once it reaches the Timeout.timeout call,
227
227
  # so outside that call it will be a Timeout::Error.
228
228
  #
@@ -231,7 +231,7 @@ module Timeout
231
231
  # For those reasons, this method cannot be relied on to enforce timeouts for untrusted blocks.
232
232
  #
233
233
  # If a scheduler is defined, it will be used to handle the timeout by invoking
234
- # Scheduler#timeout_after.
234
+ # Fiber::Scheduler#timeout_after.
235
235
  #
236
236
  # Note that this is both a method of module Timeout, so you can <tt>include
237
237
  # Timeout</tt> into your classes so they have a #timeout method, as well as
@@ -239,13 +239,13 @@ module Timeout
239
239
  #
240
240
  # ==== Ensuring the exception does not fire inside ensure blocks
241
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 it to put the Timeout.timeout call inside the body of the begin/ensure/end:
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
244
  #
245
245
  # begin
246
246
  # Timeout.timeout(sec) { some_long_operation }
247
247
  # ensure
248
- # cleanup # safe, cannot be interrupt by timeout
248
+ # cleanup # safe, cannot be interrupted by timeout
249
249
  # end
250
250
  #
251
251
  # If that is not feasible, e.g. if there are +ensure+ blocks inside +some_long_operation+,
@@ -263,17 +263,17 @@ module Timeout
263
263
  # end
264
264
  # }
265
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 +Thread.handle_interrupt(Timeout::ExitException => ...)+
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
268
  # is unsupported and causes subtle errors like raising the wrong exception outside the block, do not use that.
269
269
  #
270
270
  # Note that Thread.handle_interrupt is somewhat dangerous because if setup or cleanup hangs
271
271
  # then the current thread will hang too and the timeout will never fire.
272
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.
273
+ # e.g. +some_long_operation+ executes for +sec+ seconds + whatever time cleanup takes.
274
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,
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
277
  # the block will not be interrupted.
278
278
  def self.timeout(sec, klass = nil, message = nil, &block) #:yield: +sec+
279
279
  return yield(sec) if sec == nil or sec.zero?
@@ -282,19 +282,21 @@ module Timeout
282
282
  message ||= "execution expired"
283
283
 
284
284
  if Fiber.respond_to?(:current_scheduler) && (scheduler = Fiber.current_scheduler)&.respond_to?(:timeout_after)
285
- return scheduler.timeout_after(sec, klass || Error, message, &block)
286
- end
287
-
288
- state = State.instance
289
- state.ensure_timeout_thread_created
290
-
291
- perform = Proc.new do |exc|
292
- request = Request.new(Thread.current, sec, exc, message)
293
- state.add_request(request)
294
- begin
295
- return yield(sec)
296
- ensure
297
- request.finished
285
+ perform = Proc.new do |exc|
286
+ scheduler.timeout_after(sec, exc, message, &block)
287
+ end
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
298
300
  end
299
301
  end
300
302
 
@@ -305,6 +307,7 @@ module Timeout
305
307
  end
306
308
  end
307
309
 
310
+ # See Timeout.timeout
308
311
  private def timeout(*args, &block)
309
312
  Timeout.timeout(*args, &block)
310
313
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: timeout
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.0
4
+ version: 0.6.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yukihiro Matsumoto
8
8
  bindir: bin
9
9
  cert_chain: []
10
- date: 2025-12-17 00:00:00.000000000 Z
10
+ date: 2026-03-09 00:00:00.000000000 Z
11
11
  dependencies: []
12
12
  description: Auto-terminate potentially long-running operations in Ruby.
13
13
  email: