From a689c0cbe61cf89ea966518b18a8ce3a1af4f0c0 Mon Sep 17 00:00:00 2001 From: Joseph Teichman Date: Mon, 23 Dec 2024 16:25:30 -0500 Subject: [PATCH] Fix multithread auth race condition --- lib/mongo/auth/credential_cache.rb | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/lib/mongo/auth/credential_cache.rb b/lib/mongo/auth/credential_cache.rb index 16d7962775..32c2fd640b 100644 --- a/lib/mongo/auth/credential_cache.rb +++ b/lib/mongo/auth/credential_cache.rb @@ -22,19 +22,24 @@ module Auth # # @api private module CredentialCache + MUTEX = Mutex.new class << self attr_reader :store end 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) @@ -47,7 +52,9 @@ class << self end module_function def clear - @store = {} + MUTEX.synchronize do + @store = {} + end end end end