Skip to content

Commit

Permalink
RUBY-3604 Fix multithread auth race condition (#2912)
Browse files Browse the repository at this point in the history
Co-authored-by: Dmitry Rybakov <dmitry.rybakov@mongodb.com>
  • Loading branch information
jteich and comandeo-mongo authored Jan 21, 2025
1 parent 0e91a40 commit b0ccd0e
Showing 1 changed file with 13 additions and 6 deletions.
19 changes: 13 additions & 6 deletions lib/mongo/auth/credential_cache.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,24 @@ module Auth
#
# @api private
module CredentialCache

class << self
attr_reader :store
end

MUTEX = Mutex.new

module_function def get(key)
@store ||= {}
@store[key]
MUTEX.synchronize do
@store ||= {}
@store[key]
end
end

module_function def set(key, value)
@store ||= {}
@store[key] = value
MUTEX.synchronize do
@store ||= {}
@store[key] = value
end
end

module_function def cache(key)
Expand All @@ -47,7 +52,9 @@ class << self
end

module_function def clear
@store = {}
MUTEX.synchronize do
@store = {}
end
end
end
end
Expand Down

0 comments on commit b0ccd0e

Please sign in to comment.