[3.6] bpo-31070: Fix a race condition in importlib _get_module_lock(). (GH-3033). (#3038)

(cherry picked from commit 9b0d1d647e)
This commit is contained in:
Serhiy Storchaka 2017-08-09 14:56:13 +03:00 committed by GitHub
parent 33460fa7e0
commit f3b891718e
3 changed files with 1495 additions and 1480 deletions

View file

@ -172,8 +172,18 @@ def _get_module_lock(name):
lock = _DummyModuleLock(name) lock = _DummyModuleLock(name)
else: else:
lock = _ModuleLock(name) lock = _ModuleLock(name)
def cb(_):
def cb(ref, name=name):
_imp.acquire_lock()
try:
# bpo-31070: Check if another thread created a new lock
# after the previous lock was destroyed
# but before the weakref callback was called.
if _module_locks.get(name) is ref:
del _module_locks[name] del _module_locks[name]
finally:
_imp.release_lock()
_module_locks[name] = _weakref.ref(lock, cb) _module_locks[name] = _weakref.ref(lock, cb)
finally: finally:
_imp.release_lock() _imp.release_lock()

View file

@ -0,0 +1 @@
Fix a race condition in importlib _get_module_lock().

File diff suppressed because it is too large Load diff