Skip to content

Commit

Permalink
make require 'timecop' a bit more threadsafe
Browse files Browse the repository at this point in the history
Every few hundred runs of our application we are seeing something like:
```
22:27:36 NameError: uninitialized constant #<Class:Time>::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.
  • Loading branch information
ptarjan committed Jan 30, 2019
1 parent 71cbbd3 commit ac628de
Showing 1 changed file with 3 additions and 1 deletion.
4 changes: 3 additions & 1 deletion 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
Expand Down Expand Up @@ -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")

0 comments on commit ac628de

Please sign in to comment.