Skip to content

Commit 3cd50cc

Browse files
author
A. Jesse Jiryu Davis
committed
Ensure ThreadIdent's lock is always released; improve comment PYTHON-509
1 parent a82d6be commit 3cd50cc

File tree

1 file changed

+9
-5
lines changed

1 file changed

+9
-5
lines changed

pymongo/thread_util.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -73,13 +73,17 @@ class ThreadVigil(object):
7373
pass
7474

7575
def _make_vigil(self):
76-
# Assigning to a threadlocal isn't thread-safe in Python <= 2.7.0
76+
# Threadlocals in Python <= 2.7.0 have race conditions when setting
77+
# attributes and possibly when getting them, too, leading to weakref
78+
# callbacks not getting called later.
7779
self._lock.acquire()
78-
vigil = getattr(self._local, 'vigil', None)
79-
if not vigil:
80-
self._local.vigil = vigil = ThreadIdent.ThreadVigil()
80+
try:
81+
vigil = getattr(self._local, 'vigil', None)
82+
if not vigil:
83+
self._local.vigil = vigil = ThreadIdent.ThreadVigil()
84+
finally:
85+
self._lock.release()
8186

82-
self._lock.release()
8387
return vigil
8488

8589
def get(self):

0 commit comments

Comments
 (0)