Ensure ThreadIdent's lock is always released; improve comment PYTHON-509

This commit is contained in:
A. Jesse Jiryu Davis 2013-04-24 22:08:38 -04:00
parent a82d6bea2d
commit 3cd50cceed

View File

@ -73,13 +73,17 @@ class ThreadIdent(Ident):
pass
def _make_vigil(self):
# Assigning to a threadlocal isn't thread-safe in Python <= 2.7.0
# Threadlocals in Python <= 2.7.0 have race conditions when setting
# attributes and possibly when getting them, too, leading to weakref
# callbacks not getting called later.
self._lock.acquire()
vigil = getattr(self._local, 'vigil', None)
if not vigil:
self._local.vigil = vigil = ThreadIdent.ThreadVigil()
try:
vigil = getattr(self._local, 'vigil', None)
if not vigil:
self._local.vigil = vigil = ThreadIdent.ThreadVigil()
finally:
self._lock.release()
self._lock.release()
return vigil
def get(self):