Skip to content

Commit

Permalink
Make cache and values fully thread-safe
Browse files Browse the repository at this point in the history
Not locking the default initialization can lead to race-conditions.

Note: not sure if I should use one or two mutexes as I am not familiar with this code enough to make the judgment.

ref: ruby-concurrency/concurrent-ruby#970
  • Loading branch information
mensfeld authored Nov 21, 2022
1 parent e95386b commit c832ae8
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions lib/puppet/settings.rb
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,19 @@ def initialize
@configuration_file = nil

# And keep a per-environment cache
@cache = Concurrent::Hash.new { |hash, key| hash[key] = Concurrent::Hash.new }
@values = Concurrent::Hash.new { |hash, key| hash[key] = Concurrent::Hash.new }
@mutex = Mutex.new
@cache = Concurrent::Hash.new do |hash, key|
@mutex.synchronize do
break hash[key] if hash.key?(key)
hash[key] = Concurrent::Hash.new
end
end
@values = Concurrent::Hash.new do |hash, key|
@mutex.synchronize do
break hash[key] if hash.key?(key)
hash[key] = Concurrent::Hash.new
end
end

# The list of sections we've used.
@used = []
Expand Down

0 comments on commit c832ae8

Please sign in to comment.