diff options
author | Serhiy Storchaka <storchaka@gmail.com> | 2017-08-09 14:56:13 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-08-09 14:56:13 +0300 |
commit | f3b891718e104b6e7018b58bbcd86421a2837fb8 (patch) | |
tree | f041e864d8114ea2662d269af36fa61d52fd71be /Lib/importlib | |
parent | ttk: fix LabeledScale and OptionMenu destroy() method (#3025) (#3030) (diff) | |
download | cpython-f3b891718e104b6e7018b58bbcd86421a2837fb8.tar.gz cpython-f3b891718e104b6e7018b58bbcd86421a2837fb8.tar.bz2 cpython-f3b891718e104b6e7018b58bbcd86421a2837fb8.zip |
[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.py | 14 |
1 files changed, 12 insertions, 2 deletions
diff --git a/Lib/importlib/_bootstrap.py b/Lib/importlib/_bootstrap.py index 92cf14f9504..4cf8aec1b2a 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() |