summaryrefslogtreecommitdiffstats
path: root/Lib/importlib
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2017-08-09 11:29:12 (GMT)
committerGitHub <noreply@github.com>2017-08-09 11:29:12 (GMT)
commit9b0d1d647e3d2ec9d299e5c9f49b02fbbb810a5a (patch)
tree1c66e3ae8c16009b6fadadf9911b039bd7d2c307 /Lib/importlib
parent88eee44a91c991dbbdf284fa220e2928b5de105c (diff)
downloadcpython-9b0d1d647e3d2ec9d299e5c9f49b02fbbb810a5a.zip
cpython-9b0d1d647e3d2ec9d299e5c9f49b02fbbb810a5a.tar.gz
cpython-9b0d1d647e3d2ec9d299e5c9f49b02fbbb810a5a.tar.bz2
bpo-31070: Fix a race condition in importlib _get_module_lock(). (#3033)
Diffstat (limited to 'Lib/importlib')
-rw-r--r--Lib/importlib/_bootstrap.py14
1 files changed, 12 insertions, 2 deletions
diff --git a/Lib/importlib/_bootstrap.py b/Lib/importlib/_bootstrap.py
index 493c272..fbd9392 100644
--- a/Lib/importlib/_bootstrap.py
+++ b/Lib/importlib/_bootstrap.py
@@ -172,8 +172,18 @@ def _get_module_lock(name):
lock = _DummyModuleLock(name)
else:
lock = _ModuleLock(name)
- def cb(_):
- del _module_locks[name]
+
+ 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]
+ finally:
+ _imp.release_lock()
+
_module_locks[name] = _weakref.ref(lock, cb)
finally:
_imp.release_lock()