cpython/Lib/test/test_importlib
Miss Islington (bot) 7d074702eb
[3.14] gh-143650: Fix importlib race condition on import failure (GH-143651) (#144662)
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.
(cherry picked from commit ac8b5b6890)

Co-authored-by: Gregory P. Smith <68491+gpshead@users.noreply.github.com>
Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
2026-02-11 06:06:51 +00:00
..
builtin gh-105407: Remove unused imports in tests (#105408) 2023-06-06 22:50:43 +02:00
extension gh-131152: Remove unused imports from tests (part 2) (#131154) 2025-03-13 10:57:40 +01:00
frozen gh-71339: Use new assertion methods in test_import and test_importlib (GH-129052) 2025-01-21 11:24:19 +02:00
import_ [3.14] gh-134100: Fix use-after-free in PyImport_ImportModuleLevelObject (GH-134117) (#134171) 2025-05-18 20:38:40 +05:30
metadata gh-120910: Fix issue resolving relative paths outside site-packages. (#120911) 2024-06-23 13:06:07 -04:00
namespace_pkgs
partial bpo-43517: Fix false positive in detection of circular imports (#24895) 2021-03-20 20:07:44 +01:00
resources gh-131152: Remove unused imports from tests (part 2) (#131154) 2025-03-13 10:57:40 +01:00
source gh-71339: Use new assertion methods in test_import and test_importlib (GH-129052) 2025-01-21 11:24:19 +02:00
__init__.py
__main__.py
abc.py
test_abc.py [3.14] GH-137426: Remove code deprecation of importlib.abc.ResourceLoader (GH-137567) (#137654) 2025-08-12 15:47:28 +03:00
test_api.py gh-71339: Use new assertion methods in test_import and test_importlib (GH-129052) 2025-01-21 11:24:19 +02:00
test_lazy.py gh-71339: Use new assertion methods in test_import and test_importlib (GH-129052) 2025-01-21 11:24:19 +02:00
test_locks.py [3.14] gh-134322: Fix repr(threading.RLock) (GH-134389) (#134528) 2025-05-22 17:12:21 +00:00
test_namespace_pkgs.py gh-71339: Use new assertion methods in test_import and test_importlib (GH-129052) 2025-01-21 11:24:19 +02:00
test_pkg_import.py gh-71339: Use new assertion methods in test_import and test_importlib (GH-129052) 2025-01-21 11:24:19 +02:00
test_spec.py gh-71339: Use new assertion methods in test_import and test_importlib (GH-129052) 2025-01-21 11:24:19 +02:00
test_threaded_import.py [3.14] gh-143650: Fix importlib race condition on import failure (GH-143651) (#144662) 2026-02-11 06:06:51 +00:00
test_util.py [3.14] gh-141930: Use the regular IO stack to write .pyc files for a better error message on failure (GH-141931) (#142021) 2025-12-01 18:43:50 +02:00
test_windows.py gh-121604: fix warnings in test_importlib.test_abc and test_importlib.test_windows (GH-128904) 2025-01-16 13:29:16 -08:00
threaded_import_hangers.py
util.py gh-116303: Skip test module dependent tests if test modules are unavailable (#117341) 2024-04-03 15:11:36 +02:00