Fix race in ThreadSafeSingleton (#322)

This commit is contained in:
Dmitry Rassoshenko 2020-11-10 22:57:10 +03:00 committed by GitHub
parent 4120afd1c3
commit fb0d99c1c9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -2178,15 +2178,19 @@ cdef class ThreadSafeSingleton(BaseSingleton):
:rtype: None
"""
self.__storage = None
with self.__storage_lock:
self.__storage = None
cpdef object _provide(self, tuple args, dict kwargs):
"""Return single instance."""
with self.__storage_lock:
if self.__storage is None:
self.__storage = __factory_call(self.__instantiator,
args, kwargs)
return self.__storage
storage = self.__storage
if storage is None:
with self.__storage_lock:
if self.__storage is None:
self.__storage = __factory_call(self.__instantiator,
args, kwargs)
storage = self.__storage
return storage
cdef class DelegatedThreadSafeSingleton(ThreadSafeSingleton):