cpython/Lib/test/test_importlib
Gregory P. Smith c117ef1a1c
[3.13] gh-143650: Fix importlib race condition on import failure (GH-143651) (#144697)
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>

cherry picked from ac8b5b6890
2026-02-11 06:37:25 +00:00
..
builtin gh-105407: Remove unused imports in tests (#105408) 2023-06-06 22:50:43 +02:00
extension [3.13] gh-71339: Use new assertion methods in test_import and test_importlib (GH-129052) (#129123) 2025-04-08 12:43:04 +02:00
frozen [3.13] gh-71339: Use new assertion methods in test_import and test_importlib (GH-129052) (#129123) 2025-04-08 12:43:04 +02:00
import_ [3.13] gh-134100: Fix use-after-free in PyImport_ImportModuleLevelObject (GH-134117) (#134172) 2025-05-18 20:38:28 +05:30
metadata [3.13] gh-120910: Fix issue resolving relative paths outside site-packages. (GH-120911) (#120917) 2024-06-23 17:30:08 +00:00
namespace_pkgs
partial bpo-43517: Fix false positive in detection of circular imports (#24895) 2021-03-20 20:07:44 +01:00
resources [3.13] gh-123085: _compile_importlib: Avoid copying sources before compilation (GH-124131) (#137914) 2025-08-18 13:55:44 +00:00
source [3.13] gh-71339: Use new assertion methods in test_import and test_importlib (GH-129052) (#129123) 2025-04-08 12:43:04 +02:00
__init__.py
__main__.py
abc.py
test_abc.py [3.13] gh-71339: Use new assertion methods in test_import and test_importlib (GH-129052) (#129123) 2025-04-08 12:43:04 +02:00
test_api.py [3.13] gh-71339: Use new assertion methods in test_import and test_importlib (GH-129052) (#129123) 2025-04-08 12:43:04 +02:00
test_lazy.py [3.13] gh-71339: Use new assertion methods in test_import and test_importlib (GH-129052) (#129123) 2025-04-08 12:43:04 +02:00
test_locks.py gh-109974: Fix more threading lock_tests race conditions (#110089) 2023-09-29 12:21:18 +00:00
test_namespace_pkgs.py [3.13] gh-71339: Use new assertion methods in test_import and test_importlib (GH-129052) (#129123) 2025-04-08 12:43:04 +02:00
test_pkg_import.py [3.13] gh-71339: Use new assertion methods in test_import and test_importlib (GH-129052) (#129123) 2025-04-08 12:43:04 +02:00
test_spec.py [3.13] gh-71339: Use new assertion methods in test_import and test_importlib (GH-129052) (#129123) 2025-04-08 12:43:04 +02:00
test_threaded_import.py [3.13] gh-143650: Fix importlib race condition on import failure (GH-143651) (#144697) 2026-02-11 06:37:25 +00:00
test_util.py [3.13] gh-141930: Use the regular IO stack to write .pyc files for a better error message on failure (GH-141931) (#142023) 2025-12-01 23:23:07 +01:00
test_windows.py gh-112535: Add test on _Py_ThreadId() (#112709) 2023-12-04 22:40:06 +00:00
threaded_import_hangers.py bpo-19696: Move threaded_import_hangers (GH-14655) 2019-07-12 14:22:05 -07:00
util.py [3.13] gh-71339: Use new assertion methods in test_import and test_importlib (GH-129052) (#129123) 2025-04-08 12:43:04 +02:00