From f145618d9fa272629092ff91a5c6fc61dc143c80 Mon Sep 17 00:00:00 2001 From: Joseph Teichman Date: Mon, 23 Dec 2024 16:25:30 -0500 Subject: [PATCH 1/3] RUBY-3604 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 From ab636b9ae1f721f6e3f97e31228bfc38824a54de Mon Sep 17 00:00:00 2001 From: Joseph Teichman Date: Thu, 26 Dec 2024 11:41:57 -0500 Subject: [PATCH 2/3] removing unused external attribute --- lib/mongo/auth/credential_cache.rb | 4 ---- 1 file changed, 4 deletions(-) diff --git a/lib/mongo/auth/credential_cache.rb b/lib/mongo/auth/credential_cache.rb index 32c2fd640b..d6727b0444 100644 --- a/lib/mongo/auth/credential_cache.rb +++ b/lib/mongo/auth/credential_cache.rb @@ -24,10 +24,6 @@ module Auth module CredentialCache MUTEX = Mutex.new - class << self - attr_reader :store - end - module_function def get(key) MUTEX.synchronize do @store ||= {} From eb079e9646c9bf935c1e5c2acf87a078bf151350 Mon Sep 17 00:00:00 2001 From: Dmitry Rybakov Date: Mon, 20 Jan 2025 15:21:31 +0100 Subject: [PATCH 3/3] Return store accessor --- lib/mongo/auth/credential_cache.rb | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/lib/mongo/auth/credential_cache.rb b/lib/mongo/auth/credential_cache.rb index d6727b0444..c9888ce3dd 100644 --- a/lib/mongo/auth/credential_cache.rb +++ b/lib/mongo/auth/credential_cache.rb @@ -22,6 +22,10 @@ module Auth # # @api private module CredentialCache + class << self + attr_reader :store + end + MUTEX = Mutex.new module_function def get(key)