cpython/Lib/importlib
Gregory P. Smith ac8b5b6890
gh-143650: Fix importlib race condition on import failure (GH-143651)
Fix a race condition where a thread could receive a partially-initialized
module when another thread's import fails. The race occurs when:

1. Thread 1 starts importing, adds module to sys.modules
2. Thread 2 sees the module in sys.modules via the fast path
3. Thread 1's import fails, removes module from sys.modules
4. Thread 2 returns a stale module reference not in sys.modules

The fix adds verification after the "skip lock" optimization in both Python
and C code paths to check if the module is still in sys.modules. If the
module was removed (due to import failure), we retry the import so the
caller receives the actual exception from the import failure rather than
a stale module reference.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-02-10 14:08:33 +01:00
..
metadata gh-143658: Use str.lower and replace to further improve performance of importlib.metadata.Prepared.normalized (#144083) 2026-02-06 19:38:58 +02:00
resources gh-138044: Remove deprecated parameter alias for importlib.resources.files (#138059) 2025-10-04 14:53:43 +01:00
__init__.py GH-139686: Revert "gh-139686: Make reloading a lazy module no-op (GH-139857)" (#143584) 2026-01-08 22:08:03 +00:00
_abc.py GH-97850: Remove all uses and definitions of load_module() from importlib (#142205) 2025-12-10 15:35:51 -08:00
_bootstrap.py gh-143650: Fix importlib race condition on import failure (GH-143651) 2026-02-10 14:08:33 +01:00
_bootstrap_external.py gh-141081: Revert "Add a .gitignore file to __pycache__ folders (#141162)" (#142758) 2025-12-15 21:27:06 +02:00
abc.py GH-97850: Remove all uses and definitions of load_module() from importlib (#142205) 2025-12-10 15:35:51 -08:00
machinery.py GH-119668: expose importlib.machinery.NamespacePath (#119669) 2025-11-01 00:39:48 +00:00
readers.py bpo-46118: Move importlib.resources to its own package. (#30176) 2021-12-30 21:00:48 -05:00
simple.py bpo-46118: Move importlib.resources to its own package. (#30176) 2021-12-30 21:00:48 -05:00
util.py gh-122188: Move magic number to its own file (#122243) 2024-07-30 15:31:05 -04:00