Need to check again for appropriate synchronised lock inside of meta lock to avoid race condition.

This commit is contained in:
Graham Dumpleton
2013-08-31 10:43:56 +10:00
parent b520db984f
commit def06e83a1

View File

@@ -41,10 +41,9 @@ def synchronized(wrapped, instance, args, kwargs):
context = instance
name = '_synchronized_instance_lock'
try:
lock = getattr(context, name)
lock = getattr(context, name, None)
except AttributeError:
if lock is None:
# There is no existing lock defined for the context we
# are dealing with so we need to create one. This needs
# to be done in a way to guarantee there is only one
@@ -61,6 +60,14 @@ def synchronized(wrapped, instance, args, kwargs):
'_synchronized_meta_lock', threading.RLock())
with meta_lock:
# We need to check again for whether the lock we want
# exists in case two threads were trying to create it
# at the same time and were competing to create the
# meta lock.
lock = getattr(context, name, None)
if lock is None:
lock = threading.Lock()
setattr(context, name, lock)