Skip to content

Commit b0ccd0e

Browse files
RUBY-3604 Fix multithread auth race condition (#2912)
Co-authored-by: Dmitry Rybakov <[email protected]>
1 parent 0e91a40 commit b0ccd0e

File tree

1 file changed

+13
-6
lines changed

1 file changed

+13
-6
lines changed

Diff for: lib/mongo/auth/credential_cache.rb

+13-6
Original file line numberDiff line numberDiff line change
@@ -22,19 +22,24 @@ module Auth
2222
#
2323
# @api private
2424
module CredentialCache
25-
2625
class << self
2726
attr_reader :store
2827
end
2928

29+
MUTEX = Mutex.new
30+
3031
module_function def get(key)
31-
@store ||= {}
32-
@store[key]
32+
MUTEX.synchronize do
33+
@store ||= {}
34+
@store[key]
35+
end
3336
end
3437

3538
module_function def set(key, value)
36-
@store ||= {}
37-
@store[key] = value
39+
MUTEX.synchronize do
40+
@store ||= {}
41+
@store[key] = value
42+
end
3843
end
3944

4045
module_function def cache(key)
@@ -47,7 +52,9 @@ class << self
4752
end
4853

4954
module_function def clear
50-
@store = {}
55+
MUTEX.synchronize do
56+
@store = {}
57+
end
5158
end
5259
end
5360
end

0 commit comments

Comments
 (0)