From ac628de36bc1951769c2e95edb6e15a7d6de1d9a Mon Sep 17 00:00:00 2001 From: Paul Tarjan Date: Tue, 29 Jan 2019 22:29:05 -0800 Subject: [PATCH] make `require 'timecop'` a bit more threadsafe Every few hundred runs of our application we are seeing something like: ``` 22:27:36 NameError: uninitialized constant #::Timecop 22:27:36 /build/vendor/bundle/ruby/2.4.0/gems/timecop-0.9.1/lib/timecop/time_extensions.rb:7:in `mock_time' 22:27:36 /build/vendor/bundle/ruby/2.4.0/gems/timecop-0.9.1/lib/timecop/time_extensions.rb:14:in `now_with_mock_time' ... ``` I think this is because we are running multiple ruby `Thread`s, and one thread is doing `require 'timecop'` and is interrupted after monkeypatching `Time` but before creating the `TimeCop` constant. We should do the monkeypatching after the constant is available. --- lib/timecop/timecop.rb | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/timecop/timecop.rb b/lib/timecop/timecop.rb index afd9f807..9106616b 100644 --- a/lib/timecop/timecop.rb +++ b/lib/timecop/timecop.rb @@ -1,5 +1,4 @@ require 'singleton' -require File.join(File.dirname(__FILE__), "time_extensions") require File.join(File.dirname(__FILE__), "time_stack_item") # Timecop @@ -235,3 +234,6 @@ def initialize end end end + +# This must be done after TimeCop is available +require File.join(File.dirname(__FILE__), "time_extensions")