]> git.ipfire.org Git - thirdparty/Python/cpython.git/commit
[3.14] gh-143650: Fix importlib race condition on import failure (GH-143651) (#144662)
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Wed, 11 Feb 2026 06:06:51 +0000 (07:06 +0100)
committerGitHub <noreply@github.com>
Wed, 11 Feb 2026 06:06:51 +0000 (06:06 +0000)
commit7d074702ebf2f456baee9696e73e176bf7fa5146
tree43b19d0e9b6d3ac65f783676e7d29d45cb1f56e2
parent815f21bda9566c54f15e8faac40291cda08a839d
[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 ac8b5b6890006ee7254ea878866cb486ff835ecb)

Co-authored-by: Gregory P. Smith <68491+gpshead@users.noreply.github.com>
Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
Lib/importlib/_bootstrap.py
Lib/test/test_importlib/test_threaded_import.py
Misc/NEWS.d/next/Core_and_Builtins/2026-01-10-10-58-36.gh-issue-143650.k8mR4x.rst [new file with mode: 0644]
Python/import.c