timeout 0.5.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 +129 -53
  3. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 64020467581a9c1fe3f3b8dac1276315e0653c8029e12e031ba61dffe379629d
4
- data.tar.gz: 13473fd4bcfa02a9406c1e4b8c949c696634def65c3fdbd03bfe35670adf3194
3
+ metadata.gz: 31442005d41eeaddb46916ff83e27dc0198a3a0509c463d47d1fd4a9c7e0ec6d
4
+ data.tar.gz: bf8dff95c8356a47b513568f3c6890d2c1a9a74a6a364f5fa38d5e8f4b8e3c87
5
5
  SHA512:
6
- metadata.gz: 79b97c0bc72c9417b5185928947f629733b027fc907213293bb90441bca438afe3ccaf9e549811dfb9da4c5796c6a413eb31d4d687bb84868e718a0e6bb4bbaf
7
- data.tar.gz: 49ea49ecd7e9b73834e5410188d955be6c256f0f5ea349aa816187bb57df32e403bb8f87f80fea11dd42c1fc789c7ac17de4b6f5942ce17513a18d7fa0b35834
6
+ metadata.gz: 3630c0c2b33659c650a9f4af7d5983dd303d174431c7d7c137292e4c702ac95afaab7434b4061bd6b013edc10343fa141e0969ac4188f671cbc84a14583c9986
7
+ data.tar.gz: b835d20514bda974bd0f5cd6473213bd429a9887503802dc2882ab25ae00be502f5df84642f305eb348866e33c9357130b985012d936ed48eebfea800458853c
data/lib/timeout.rb CHANGED
@@ -20,9 +20,9 @@
20
20
 
21
21
  module Timeout
22
22
  # The version
23
- VERSION = "0.5.0"
23
+ VERSION = "0.6.1"
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
@@ -54,8 +54,6 @@ module Timeout
54
54
  private_constant :GET_TIME
55
55
 
56
56
  class State
57
- attr_reader :condvar, :queue, :queue_mutex # shared with Timeout.timeout()
58
-
59
57
  def initialize
60
58
  @condvar = ConditionVariable.new
61
59
  @queue = Queue.new
@@ -83,36 +81,40 @@ module Timeout
83
81
  end
84
82
 
85
83
  def create_timeout_thread
86
- watcher = Thread.new do
87
- requests = []
88
- while true
89
- until @queue.empty? and !requests.empty? # wait to have at least one request
90
- req = @queue.pop
91
- requests << req unless req.done?
92
- end
93
- closest_deadline = requests.min_by(&:deadline).deadline
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
94
95
 
95
- now = 0.0
96
- @queue_mutex.synchronize do
97
- while (now = GET_TIME.call(Process::CLOCK_MONOTONIC)) < closest_deadline and @queue.empty?
98
- @condvar.wait(@queue_mutex, closest_deadline - now)
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
99
101
  end
100
- end
101
102
 
102
- requests.each do |req|
103
- req.interrupt if req.expired?(now)
103
+ requests.each do |req|
104
+ req.interrupt if req.expired?(now)
105
+ end
106
+ requests.reject!(&:done?)
104
107
  end
105
- requests.reject!(&:done?)
106
108
  end
107
- end
108
109
 
109
- if !watcher.group.enclosed? && (!defined?(Ractor.main?) || Ractor.main?)
110
- ThreadGroup::Default.add(watcher)
111
- end
110
+ if !watcher.group.enclosed? && (!defined?(Ractor.main?) || Ractor.main?)
111
+ ThreadGroup::Default.add(watcher)
112
+ end
112
113
 
113
- watcher.name = "Timeout stdlib thread"
114
- watcher.thread_variable_set(:"\0__detached_thread__", true)
115
- watcher
114
+ watcher.name = "Timeout stdlib thread"
115
+ watcher.thread_variable_set(:"\0__detached_thread__", true)
116
+ watcher
117
+ end
116
118
  end
117
119
 
118
120
  def ensure_timeout_thread_created
@@ -121,13 +123,20 @@ module Timeout
121
123
  # In that case, just return and let the main thread create the Timeout thread.
122
124
  return if @timeout_thread_mutex.owned?
123
125
 
124
- @timeout_thread_mutex.synchronize do
126
+ Sync.synchronize @timeout_thread_mutex do
125
127
  unless @timeout_thread&.alive?
126
128
  @timeout_thread = create_timeout_thread
127
129
  end
128
130
  end
129
131
  end
130
132
  end
133
+
134
+ def add_request(request)
135
+ Sync.synchronize @queue_mutex do
136
+ @queue << request
137
+ @condvar.signal
138
+ end
139
+ end
131
140
  end
132
141
  private_constant :State
133
142
 
@@ -144,6 +153,7 @@ module Timeout
144
153
  @done = false # protected by @mutex
145
154
  end
146
155
 
156
+ # Only called by the timeout thread, so does not need Sync.synchronize
147
157
  def done?
148
158
  @mutex.synchronize do
149
159
  @done
@@ -154,6 +164,7 @@ module Timeout
154
164
  now >= @deadline
155
165
  end
156
166
 
167
+ # Only called by the timeout thread, so does not need Sync.synchronize
157
168
  def interrupt
158
169
  @mutex.synchronize do
159
170
  unless @done
@@ -164,16 +175,36 @@ module Timeout
164
175
  end
165
176
 
166
177
  def finished
167
- @mutex.synchronize do
178
+ Sync.synchronize @mutex do
168
179
  @done = true
169
180
  end
170
181
  end
171
182
  end
172
183
  private_constant :Request
173
184
 
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
200
+ end
201
+ end
202
+ end
203
+ private_constant :Sync
204
+
174
205
  # :startdoc:
175
206
 
176
- # 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
177
208
  # +sec+ seconds to complete.
178
209
  #
179
210
  # +sec+:: Number of seconds to wait for the block to terminate. Any non-negative number
@@ -181,24 +212,69 @@ module Timeout
181
212
  # value of 0 or +nil+ will execute the block without any timeout.
182
213
  # Any negative number will raise an ArgumentError.
183
214
  # +klass+:: Exception Class to raise if the block fails to terminate
184
- # in +sec+ seconds. Omitting will use the default, Timeout::Error
215
+ # in +sec+ seconds. Omitting will use the default, Timeout::Error.
185
216
  # +message+:: Error message to raise with Exception Class.
186
- # Omitting will use the default, "execution expired"
217
+ # Omitting will use the default, <tt>"execution expired"</tt>.
187
218
  #
188
219
  # Returns the result of the block *if* the block completed before
189
- # +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+.
221
+ #
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.
190
228
  #
191
- # The exception thrown to terminate the given block cannot be rescued inside
192
- # the block unless +klass+ is given explicitly. However, the block can use
193
- # ensure to prevent the handling of the exception. For that reason, this
194
- # method cannot be relied on to enforce timeouts for untrusted blocks.
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.
195
232
  #
196
233
  # If a scheduler is defined, it will be used to handle the timeout by invoking
197
- # Scheduler#timeout_after.
234
+ # Fiber::Scheduler#timeout_after.
198
235
  #
199
236
  # Note that this is both a method of module Timeout, so you can <tt>include
200
237
  # Timeout</tt> into your classes so they have a #timeout method, as well as
201
238
  # a module method, so you can call it directly as Timeout.timeout().
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.
202
278
  def self.timeout(sec, klass = nil, message = nil, &block) #:yield: +sec+
203
279
  return yield(sec) if sec == nil or sec.zero?
204
280
  raise ArgumentError, "Timeout sec must be a non-negative number" if 0 > sec
@@ -206,22 +282,21 @@ module Timeout
206
282
  message ||= "execution expired"
207
283
 
208
284
  if Fiber.respond_to?(:current_scheduler) && (scheduler = Fiber.current_scheduler)&.respond_to?(:timeout_after)
209
- return scheduler.timeout_after(sec, klass || Error, message, &block)
210
- end
211
-
212
- state = State.instance
213
- state.ensure_timeout_thread_created
214
-
215
- perform = Proc.new do |exc|
216
- request = Request.new(Thread.current, sec, exc, message)
217
- state.queue_mutex.synchronize do
218
- state.queue << request
219
- state.condvar.signal
285
+ perform = Proc.new do |exc|
286
+ scheduler.timeout_after(sec, exc, message, &block)
220
287
  end
221
- begin
222
- return yield(sec)
223
- ensure
224
- 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
225
300
  end
226
301
  end
227
302
 
@@ -232,6 +307,7 @@ module Timeout
232
307
  end
233
308
  end
234
309
 
310
+ # See Timeout.timeout
235
311
  private def timeout(*args, &block)
236
312
  Timeout.timeout(*args, &block)
237
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.5.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-08 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: