Skip to content

Commit 07e5d86

Browse files
committed
Keep a private reference to Process.clock_gettime
`timeout 0.3.0` broke our test suite because we have some tests that stubs `Process.clock_gettime` making it return a value in the past, causing `Timeout` to trigger almost immediately. I beleive it wasn't a problem before because it was relying on `Process.sleep`.
1 parent 01554f1 commit 07e5d86

File tree

1 file changed

+5
-2
lines changed

1 file changed

+5
-2
lines changed

lib/timeout.rb

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ class Request
6262

6363
def initialize(thread, timeout, exception_class, message)
6464
@thread = thread
65-
@deadline = Process.clock_gettime(Process::CLOCK_MONOTONIC) + timeout
65+
@deadline = GET_TIME.call(Process::CLOCK_MONOTONIC) + timeout
6666
@exception_class = exception_class
6767
@message = message
6868

@@ -109,7 +109,7 @@ def self.create_timeout_thread
109109

110110
now = 0.0
111111
QUEUE_MUTEX.synchronize do
112-
while (now = Process.clock_gettime(Process::CLOCK_MONOTONIC)) < closest_deadline and QUEUE.empty?
112+
while (now = GET_TIME.call(Process::CLOCK_MONOTONIC)) < closest_deadline and QUEUE.empty?
113113
CONDVAR.wait(QUEUE_MUTEX, closest_deadline - now)
114114
end
115115
end
@@ -136,6 +136,9 @@ def self.ensure_timeout_thread_created
136136
end
137137
# :startdoc:
138138

139+
GET_TIME = Process.method(:clock_gettime)
140+
private_constant :GET_TIME
141+
139142
# Perform an operation in a block, raising an error if it takes longer than
140143
# +sec+ seconds to complete.
141144
#

0 commit comments

Comments
 (0)