[前][次][番号順一覧][スレッド一覧]

ruby-changes:72108

From: Jean <ko1@a...>
Date: Thu, 9 Jun 2022 18:58:59 +0900 (JST)
Subject: [ruby-changes:72108] 4e21b19a61 (master): [ruby/timeout] Keep a private reference to `Process.clock_gettime`

https://git.ruby-lang.org/ruby.git/commit/?id=4e21b19a61

From 4e21b19a61aadd785df1d731d79265fef16fb7c8 Mon Sep 17 00:00:00 2001
From: Jean Boussier <jean.boussier@g...>
Date: Wed, 8 Jun 2022 15:44:32 +0200
Subject: [ruby/timeout] 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`.

https://github.com/ruby/timeout/commit/e5911a303e
---
 lib/timeout.rb | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)

diff --git a/lib/timeout.rb b/lib/timeout.rb
index aa9bcaf100..95dd1490ae 100644
--- a/lib/timeout.rb
+++ b/lib/timeout.rb
@@ -62,7 +62,7 @@ module Timeout https://github.com/ruby/ruby/blob/trunk/lib/timeout.rb#L62
 
     def initialize(thread, timeout, exception_class, message)
       @thread = thread
-      @deadline = Process.clock_gettime(Process::CLOCK_MONOTONIC) + timeout
+      @deadline = GET_TIME.call(Process::CLOCK_MONOTONIC) + timeout
       @exception_class = exception_class
       @message = message
 
@@ -109,7 +109,7 @@ module Timeout https://github.com/ruby/ruby/blob/trunk/lib/timeout.rb#L109
 
         now = 0.0
         QUEUE_MUTEX.synchronize do
-          while (now = Process.clock_gettime(Process::CLOCK_MONOTONIC)) < closest_deadline and QUEUE.empty?
+          while (now = GET_TIME.call(Process::CLOCK_MONOTONIC)) < closest_deadline and QUEUE.empty?
             CONDVAR.wait(QUEUE_MUTEX, closest_deadline - now)
           end
         end
@@ -134,6 +134,12 @@ module Timeout https://github.com/ruby/ruby/blob/trunk/lib/timeout.rb#L134
       end
     end
   end
+
+  # We keep a private reference so that time mocking libraries won't break
+  # Timeout.
+  GET_TIME = Process.method(:clock_gettime)
+  private_constant :GET_TIME
+
   # :startdoc:
 
   # Perform an operation in a block, raising an error if it takes longer than
-- 
cgit v1.2.1


--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]