cpython/Lib/test/test_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
..
builtin GH-97850: Remove all uses and definitions of load_module() from importlib (#142205) 2025-12-10 15:35:51 -08:00
extension GH-97850: Remove all uses and definitions of load_module() from importlib (#142205) 2025-12-10 15:35:51 -08: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_ GH-65961: Stop setting __cached__ on modules (GH-142165) 2025-12-11 11:44:46 -08:00
metadata gh-143658: importlib.metadata: Use str.translate to improve performance of importlib.metadata.Prepared.normalized (#143660) 2026-01-13 08:54:15 +02:00
namespace_pkgs gh-93334: Fix homonym edge case in PathFinder.find_spec() (GH-98100) 2025-09-05 15:41:01 -07:00
partial bpo-43517: Fix false positive in detection of circular imports (#24895) 2021-03-20 20:07:44 +01:00
resources gh-138044: Remove deprecated parameter alias for importlib.resources.files (#138059) 2025-10-04 14:53:43 +01:00
source gh-144278: Enable overriding sys.implementation's name and cache_tag when building sysmodule.c (GH-144293) 2026-02-10 00:01:17 +00:00
__init__.py
__main__.py
abc.py GH-97850: Remove all uses and definitions of load_module() from importlib (#142205) 2025-12-10 15:35:51 -08:00
test_abc.py gh-144278: Enable overriding sys.implementation's name and cache_tag when building sysmodule.c (GH-144293) 2026-02-10 00:01:17 +00:00
test_api.py gh-144278: Enable overriding sys.implementation's name and cache_tag when building sysmodule.c (GH-144293) 2026-02-10 00:01:17 +00:00
test_lazy.py GH-139686: Revert "gh-139686: Make reloading a lazy module no-op (GH-139857)" (#143584) 2026-01-08 22:08:03 +00:00
test_locks.py gh-134322: Fix repr(threading.RLock) (#134389) 2025-05-22 16:46:57 +00:00
test_namespace_pkgs.py gh-93334: Fix homonym edge case in PathFinder.find_spec() (GH-98100) 2025-09-05 15:41:01 -07:00
test_pkg_import.py gh-144278: Enable overriding sys.implementation's name and cache_tag when building sysmodule.c (GH-144293) 2026-02-10 00:01:17 +00:00
test_spec.py gh-144278: Enable overriding sys.implementation's name and cache_tag when building sysmodule.c (GH-144293) 2026-02-10 00:01:17 +00:00
test_threaded_import.py gh-143650: Fix importlib race condition on import failure (GH-143651) 2026-02-10 14:08:33 +01:00
test_util.py gh-144278: Enable overriding sys.implementation's name and cache_tag when building sysmodule.c (GH-144293) 2026-02-10 00:01:17 +00: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-144278: Enable overriding sys.implementation's name and cache_tag when building sysmodule.c (GH-144293) 2026-02-10 00:01:17 +00:00