summaryrefslogtreecommitdiffstats
path: root/Lib/importlib
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2017-08-09 11:56:13 (GMT)
committerGitHub <noreply@github.com>2017-08-09 11:56:13 (GMT)
commitf3b891718e104b6e7018b58bbcd86421a2837fb8 (patch)
treef041e864d8114ea2662d269af36fa61d52fd71be /Lib/importlib
parent33460fa7e0bd126bee739a66e1228665dc22e70f (diff)
downloadcpython-f3b891718e104b6e7018b58bbcd86421a2837fb8.zip
cpython-f3b891718e104b6e7018b58bbcd86421a2837fb8.tar.gz
cpython-f3b891718e104b6e7018b58bbcd86421a2837fb8.tar.bz2
[3.6] bpo-31070: Fix a race condition in importlib _get_module_lock(). (GH-3033). (#3038)
(cherry picked from commit 9b0d1d647e3d2ec9d299e5c9f49b02fbbb810a5a)
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 92cf14f..4cf8aec 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()