Skip to content

Commit 76fd124

Browse files
committed
Fix another race condition
1 parent d5bdf6a commit 76fd124

2 files changed

Lines changed: 8 additions & 7 deletions

File tree

Lib/importlib/_bootstrap.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1301,7 +1301,6 @@ def _sanity_check(name, package, level):
13011301

13021302

13031303
_ERR_MSG_PREFIX = 'No module named '
1304-
_ERR_MSG = _ERR_MSG_PREFIX + '{!r}'
13051304

13061305
def _find_and_load_unlocked(name, import_):
13071306
path = None
@@ -1310,16 +1309,19 @@ def _find_and_load_unlocked(name, import_):
13101309
if parent:
13111310
if parent not in sys.modules:
13121311
_call_with_frames_removed(import_, parent)
1313-
# Crazy side-effects!
1314-
if name in sys.modules:
1315-
return sys.modules[name]
13161312
parent_module = sys.modules[parent]
13171313
try:
13181314
path = parent_module.__path__
13191315
except AttributeError:
13201316
msg = f'{_ERR_MSG_PREFIX}{name!r}; {parent!r} is not a package'
13211317
raise ModuleNotFoundError(msg, name=name) from None
13221318
parent_spec = parent_module.__spec__
1319+
if getattr(parent_spec, '_initializing', False):
1320+
_call_with_frames_removed(import_, parent)
1321+
# Crazy side-effects!
1322+
module = sys.modules.get(name)
1323+
if module is not None:
1324+
return module
13231325
child = name.rpartition('.')[2]
13241326
spec = _find_spec(name, path)
13251327
if spec is None:
Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
1-
Fix a race condition where importing setuptools or other packages that modify
2-
:data:`sys.meta_path` could cause concurrent imports to fail with a
3-
:exc:`ModuleNotFoundError`.
1+
Fix two race conditions involving concurrent imports that could lead to
2+
spurious failures with :exc:`ModuleNotFoundError`.

0 commit comments

Comments
 (0)