Skip to content

Commit 9b0d1d6

Browse files
bpo-31070: Fix a race condition in importlib _get_module_lock(). (#3033)
1 parent 88eee44 commit 9b0d1d6

File tree

3 files changed

+1491
-1475
lines changed

3 files changed

+1491
-1475
lines changed

Lib/importlib/_bootstrap.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -172,8 +172,18 @@ def _get_module_lock(name):
172172
lock = _DummyModuleLock(name)
173173
else:
174174
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+
177187
_module_locks[name] = _weakref.ref(lock, cb)
178188
finally:
179189
_imp.release_lock()
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix a race condition in importlib _get_module_lock().

0 commit comments

Comments
 (0)