From c4e58444f9f1dd284a0f38dc6bf9b0158306723e Mon Sep 17 00:00:00 2001 From: Andrii Grynenko Date: Thu, 12 Sep 2024 23:57:57 -0700 Subject: [PATCH] Fix data race in SingletonHolder::hasLiveInstance Summary: hasLiveInstance() accesses instance_weak_ which is not safe to access concurrently with createInstance(). In all other places hasLiveInstance() is already called while holding the vault state lock or after switching state to shutdown (which prevents concurrent creation). livingSingletonCount() should be performing similar synchronization as well. Reviewed By: dsesh Differential Revision: D62614961 fbshipit-source-id: 7552e46fc12fb4e6e02c5094afb1d4a4e0b7804c --- folly/Singleton.h | 1 + 1 file changed, 1 insertion(+) diff --git a/folly/Singleton.h b/folly/Singleton.h index 3aae996bec9..27b17edd4d2 100644 --- a/folly/Singleton.h +++ b/folly/Singleton.h @@ -497,6 +497,7 @@ class SingletonVault { bool eagerInitComplete() const; size_t livingSingletonCount() const { + auto state = state_.rlock(); auto singletons = singletons_.rlock(); size_t ret = 0;