Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix race in ThreadSafeSingleton #322

Merged
merged 1 commit into from Nov 10, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 10 additions & 6 deletions src/dependency_injector/providers.pyx
Expand Up @@ -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):
Expand Down