Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion lib/chef-cli/chef_server_api_multi.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,10 @@ def streaming_request(*args, &block)
end

def client_for_thread
Thread.current[:chef_server_api_multi] ||= Chef::ServerAPI.new(@url, @opts)
# Use thread_variable_* methods instead of Thread.current[] to avoid
# fiber-local variable issues and provide better thread isolation
Thread.current.thread_variable_get(:chef_server_api_multi) ||
Thread.current.thread_variable_set(:chef_server_api_multi, Chef::ServerAPI.new(@url, @opts))
end

end
Expand Down
6 changes: 3 additions & 3 deletions spec/unit/chef_server_api_multi_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
before do
# clean out thread local storage or else `chef_server_api` instance double
# will get re-used across test examples and rspec will complain:
Thread.current[:chef_server_api_multi] = nil
Thread.current.thread_variable_set(:chef_server_api_multi, nil)
allow(Chef::ServerAPI).to receive(:new).with(url, expected_server_api_opts).and_return(chef_server_api)
end

Expand All @@ -57,8 +57,8 @@
end

it "creates a thread-local Chef::ServerAPI object for requests" do
server_api_multi.client_for_thread # force `||=` to run
expect(server_api_multi.client_for_thread).to eq(Thread.current[:chef_server_api_multi])
server_api_multi.client_for_thread # force lazy initialization to run
expect(server_api_multi.client_for_thread).to eq(Thread.current.thread_variable_get(:chef_server_api_multi))
end

describe "when keepalives are disabled" do
Expand Down
Loading