-
-
Notifications
You must be signed in to change notification settings - Fork 33.9k
gh-143650: Fix importlib race condition on import failure #143651
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
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. Changes: - Lib/importlib/_bootstrap.py: Add check after fast path in _find_and_load() - Python/import.c: Add checks in PyImport_GetModule() and PyImport_ImportModuleLevelObject() - Add regression test test_import_failure_race_condition() Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
78fed07 to
6393e97
Compare
|
🤖 New build scheduled with the buildbot fleet by @gpshead for commit 0a682cd 🤖 Results will be shown at: https://buildbot.python.org/all/#/grid?branch=refs%2Fpull%2F143651%2Fmerge If you want to schedule another build, you need to add the 🔨 test-with-buildbots label again. |
Add error checking inside the `if (mod_check != mod)` block to properly handle the case where import_get_module itself fails with an exception. Also refactor PyImport_GetModule to use an error label for cleanup. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
45c02dc to
842c160
Compare
|
the few buildbot failures in the earlier round all looked like network issues. not rerunning those. |
|
🤖 New build scheduled with the buildbot fleet by @gpshead for commit 102845b 🤖 Results will be shown at: https://buildbot.python.org/all/#/grid?branch=refs%2Fpull%2F143651%2Fmerge If you want to schedule another build, you need to add the 🔨 test-with-buildbots label again. |
|
🤖 New build scheduled with the buildbot fleet by @gpshead for commit 102845b 🤖 Results will be shown at: https://buildbot.python.org/all/#/grid?branch=refs%2Fpull%2F143651%2Fmerge If you want to schedule another build, you need to add the 🔨 test-with-refleak-buildbots label again. |
Fix race condition where a thread could receive a stale module reference when another thread's import fails. Adds verification after the "skip lock" fast path in both Python and C code paths to check if the module is still in sys.modules before returning it.