[3.6] bpo-31070: Fix a race condition in importlib _get_module_lock()… · python/cpython@f3b8917 (original) (raw)

File tree

3 files changed

lines changed

3 files changed

lines changed

Original file line number Diff line number Diff line change
@@ -172,8 +172,18 @@ def _get_module_lock(name):
172 172 lock = _DummyModuleLock(name)
173 173 else:
174 174 lock = _ModuleLock(name)
175 -def cb(_):
176 -del _module_locks[name]
175 +
176 +def cb(ref, name=name):
177 +_imp.acquire_lock()
178 +try:
179 +# bpo-31070: Check if another thread created a new lock
180 +# after the previous lock was destroyed
181 +# but before the weakref callback was called.
182 +if _module_locks.get(name) is ref:
183 +del _module_locks[name]
184 +finally:
185 +_imp.release_lock()
186 +
177 187 _module_locks[name] = _weakref.ref(lock, cb)
178 188 finally:
179 189 _imp.release_lock()
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1 +Fix a race condition in importlib _get_module_lock().