Skip to content

Commit 4e6aa1f

Browse files
authored
Merge pull request #9158 from joshcooper/threadsafe_initialization
Make cache and values fully thread-safe
2 parents 13ce54d + f890191 commit 4e6aa1f

File tree

1 file changed

+16
-2
lines changed

1 file changed

+16
-2
lines changed

lib/puppet/settings.rb

+16-2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
require 'forwardable'
66
require 'fileutils'
77
require 'concurrent'
8+
require_relative 'concurrent/lock'
89

910
# The class for handling configuration files.
1011
class Puppet::Settings
@@ -146,8 +147,21 @@ def initialize
146147
@configuration_file = nil
147148

148149
# And keep a per-environment cache
149-
@cache = Concurrent::Hash.new { |hash, key| hash[key] = Concurrent::Hash.new }
150-
@values = Concurrent::Hash.new { |hash, key| hash[key] = Concurrent::Hash.new }
150+
# We can't use Concurrent::Map because we want to preserve insertion order
151+
@cache_lock = Puppet::Concurrent::Lock.new
152+
@cache = Concurrent::Hash.new do |hash, key|
153+
@cache_lock.synchronize do
154+
break hash[key] if hash.key?(key)
155+
hash[key] = Concurrent::Hash.new
156+
end
157+
end
158+
@values_lock = Puppet::Concurrent::Lock.new
159+
@values = Concurrent::Hash.new do |hash, key|
160+
@values_lock.synchronize do
161+
break hash[key] if hash.key?(key)
162+
hash[key] = Concurrent::Hash.new
163+
end
164+
end
151165

152166
# The list of sections we've used.
153167
@used = []

0 commit comments

Comments
 (0)