Skip to content

Commit

Permalink
Use fiber-local for global_hook_disabled_requests (#907)
Browse files Browse the repository at this point in the history
Previously the finalizer being registered failed on Ruby trunk because
finalizers are passed an argument and the lambda didn't accept one.

I think it's simpler here to just use a fiber-local variable and have
our array cleaned up automatically when the thread exits without need of
a finalizer.
  • Loading branch information
jhawthorn committed Nov 19, 2021
1 parent 6a90f8a commit 98cc00b
Showing 1 changed file with 2 additions and 11 deletions.
13 changes: 2 additions & 11 deletions lib/vcr/library_hooks/webmock.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ class LibraryHooks
module WebMock
extend self

@global_hook_disabled_requests = {}

def with_global_hook_disabled(request)
global_hook_disabled_requests << request

Expand All @@ -25,19 +23,12 @@ def with_global_hook_disabled(request)
end

def global_hook_disabled?(request)
requests = @global_hook_disabled_requests[Thread.current.object_id]
requests = Thread.current[:_vcr_webmock_disabled_requests]
requests && requests.include?(request)
end

def global_hook_disabled_requests
requests = @global_hook_disabled_requests[Thread.current.object_id]
return requests if requests

ObjectSpace.define_finalizer(Thread.current, lambda {
@global_hook_disabled_requests.delete(Thread.current.object_id)
})

@global_hook_disabled_requests[Thread.current.object_id] = []
Thread.current[:_vcr_webmock_disabled_requests] ||= []
end

# @private
Expand Down

0 comments on commit 98cc00b

Please sign in to comment.