timeout 0.4.4 → 0.6.0

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 +176 -66
  3. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 2e546a82029e45c714f45f0a47cbcb3e747ba7faee26569c1c23126aa15af62f
4
- data.tar.gz: 3a3b43a30e17b257eb10efbaf542cfa1cba3f2382900c5d84d6fc8ffd48fc053
3
+ metadata.gz: 405263117245d6a9c981a56bc90cad374d50547078dbcfef1cb3fecca78bcece
4
+ data.tar.gz: 8eaeb608b52c8f273342746ff88f7e7b9458325e4a30e9f5605d62c749022ec0
5
5
  SHA512:
6
- metadata.gz: aed390b63eb9cbbf06ad8077e107fc8524400102b63bd02615336ecf051e86c35d5258df1e148246c0675c22c84dfc4a652f9b1dffeef79515c011ffec31eb9b
7
- data.tar.gz: e53db10269c082ff01290139ab63783b1e33e4f2cf29a33ef7741af766256187f54270a16c9f1da84060a4824b5bb723d1822043513a4f7292947588d11f5f99
6
+ metadata.gz: 1bf761aa173a75b2545088a1304eb8e8c2bc2df0b3223af738fd2c10f2170e4b140644aa9dc036835149ff1ca2cecf866cd782560b9e3856dc63491f6b357657
7
+ data.tar.gz: da2ec36df76ea4dc31c532a704d3ae102eac3dc60f4091e9d7dd072144353a1a38aa818ed15d36fb8c8a675b82a9ffa9eae85475e11346faafe48f6318602af8
data/lib/timeout.rb CHANGED
@@ -20,9 +20,9 @@
20
20
 
21
21
  module Timeout
22
22
  # The version
23
- VERSION = "0.4.4"
23
+ VERSION = "0.6.0"
24
24
 
25
- # Internal error raised to when a timeout is triggered.
25
+ # Internal exception raised to when a timeout is triggered.
26
26
  class ExitException < Exception
27
27
  def exception(*) # :nodoc:
28
28
  self
@@ -44,12 +44,101 @@ module Timeout
44
44
  end
45
45
 
46
46
  # :stopdoc:
47
- CONDVAR = ConditionVariable.new
48
- QUEUE = Queue.new
49
- QUEUE_MUTEX = Mutex.new
50
- TIMEOUT_THREAD_MUTEX = Mutex.new
51
- @timeout_thread = nil
52
- 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
53
142
 
54
143
  class Request
55
144
  attr_reader :deadline
@@ -64,6 +153,7 @@ module Timeout
64
153
  @done = false # protected by @mutex
65
154
  end
66
155
 
156
+ # Only called by the timeout thread, so does not need Sync.synchronize
67
157
  def done?
68
158
  @mutex.synchronize do
69
159
  @done
@@ -74,6 +164,7 @@ module Timeout
74
164
  now >= @deadline
75
165
  end
76
166
 
167
+ # Only called by the timeout thread, so does not need Sync.synchronize
77
168
  def interrupt
78
169
  @mutex.synchronize do
79
170
  unless @done
@@ -84,64 +175,36 @@ module Timeout
84
175
  end
85
176
 
86
177
  def finished
87
- @mutex.synchronize do
178
+ Sync.synchronize @mutex do
88
179
  @done = true
89
180
  end
90
181
  end
91
182
  end
92
183
  private_constant :Request
93
184
 
94
- def self.create_timeout_thread
95
- watcher = Thread.new do
96
- requests = []
97
- while true
98
- until QUEUE.empty? and !requests.empty? # wait to have at least one request
99
- req = QUEUE.pop
100
- requests << req unless req.done?
101
- end
102
- closest_deadline = requests.min_by(&:deadline).deadline
103
-
104
- now = 0.0
105
- QUEUE_MUTEX.synchronize do
106
- while (now = GET_TIME.call(Process::CLOCK_MONOTONIC)) < closest_deadline and QUEUE.empty?
107
- CONDVAR.wait(QUEUE_MUTEX, closest_deadline - now)
108
- end
109
- end
110
-
111
- requests.each do |req|
112
- req.interrupt if req.expired?(now)
113
- end
114
- requests.reject!(&:done?)
115
- end
116
- end
117
- ThreadGroup::Default.add(watcher) unless watcher.group.enclosed?
118
- watcher.name = "Timeout stdlib thread"
119
- watcher.thread_variable_set(:"\0__detached_thread__", true)
120
- watcher
121
- end
122
- private_class_method :create_timeout_thread
123
-
124
- def self.ensure_timeout_thread_created
125
- unless @timeout_thread and @timeout_thread.alive?
126
- # If the Mutex is already owned we are in a signal handler.
127
- # In that case, just return and let the main thread create the @timeout_thread.
128
- return if TIMEOUT_THREAD_MUTEX.owned?
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
210
  # +sec+:: Number of seconds to wait for the block to terminate. Any non-negative number
@@ -154,12 +217,18 @@ module Timeout
154
217
  # Omitting will use the default, "execution expired"
155
218
  #
156
219
  # Returns the result of the block *if* the block completed before
157
- # +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+.
158
221
  #
159
- # The exception thrown to terminate the given block cannot be rescued inside
160
- # the block unless +klass+ is given explicitly. However, the block can use
161
- # ensure to prevent the handling of the exception. For that reason, this
162
- # 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 `rescue Exception`.
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.
163
232
  #
164
233
  # If a scheduler is defined, it will be used to handle the timeout by invoking
165
234
  # Scheduler#timeout_after.
@@ -167,7 +236,46 @@ module Timeout
167
236
  # Note that this is both a method of module Timeout, so you can <tt>include
168
237
  # Timeout</tt> into your classes so they have a #timeout method, as well as
169
238
  # a module method, so you can call it directly as Timeout.timeout().
170
- 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 it 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 interrupt 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 +Thread.handle_interrupt(Timeout::ExitException => ...)+
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+
171
279
  return yield(sec) if sec == nil or sec.zero?
172
280
  raise ArgumentError, "Timeout sec must be a non-negative number" if 0 > sec
173
281
 
@@ -177,13 +285,12 @@ module Timeout
177
285
  return scheduler.timeout_after(sec, klass || Error, message, &block)
178
286
  end
179
287
 
180
- Timeout.ensure_timeout_thread_created
288
+ state = State.instance
289
+ state.ensure_timeout_thread_created
290
+
181
291
  perform = Proc.new do |exc|
182
292
  request = Request.new(Thread.current, sec, exc, message)
183
- QUEUE_MUTEX.synchronize do
184
- QUEUE << request
185
- CONDVAR.signal
186
- end
293
+ state.add_request(request)
187
294
  begin
188
295
  return yield(sec)
189
296
  ensure
@@ -197,5 +304,8 @@ module Timeout
197
304
  Error.handle_timeout(message, &perform)
198
305
  end
199
306
  end
200
- module_function :timeout
307
+
308
+ private def timeout(*args, &block)
309
+ Timeout.timeout(*args, &block)
310
+ end
201
311
  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.4.4
4
+ version: 0.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yukihiro Matsumoto
8
8
  bindir: bin
9
9
  cert_chain: []
10
- date: 2025-10-29 00:00:00.000000000 Z
10
+ date: 2025-12-17 00:00:00.000000000 Z
11
11
  dependencies: []
12
12
  description: Auto-terminate potentially long-running operations in Ruby.
13
13
  email: