GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — gh-143650: Fix importlib race condition on import failure by gpshead · Pull Request #143651 · python/cpython

Conversation

@gpshead

@gpshead gpshead commented Jan 10, 2026 •
edited by bedevere-app Bot
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member

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.

  • Issue: importlib race condition allows stale module ref to escape on failure #143650

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@gpshead
gpshead requested review from brettcannon, ericsnowcurrently, kumaraditya303, ncoghlan and warsaw as code owners January 10, 2026 11:03
@bedevere-app bedevere-app Bot mentioned this pull request Jan 10, 2026
importlib race condition allows stale module ref to escape on failure #143650
Closed
@bedevere-app bedevere-app Bot added the awaiting core review label Jan 10, 2026
@gpshead @claude
Fix race condition in import system with failing imports
6393e97
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 <[email protected]>
@gpshead
gpshead force-pushed the fix-import-race-condition branch from 78fed07 to 6393e97 Compare January 10, 2026 11:10
@gpshead
shorten comment
0a682cd
picnixz
picnixz reviewed Jan 10, 2026
View reviewed changes
Comment thread Python/import.c Resolved

Uh oh!

There was an error while loading. Please reload this page.

@gpshead gpshead added needs backport to 3.13 only security fixes needs backport to 3.14 bugs and security fixes 🔨 test-with-buildbots Test PR w/ buildbots; report in status section labels Jan 10, 2026
@bedevere-bot

bedevere-bot commented Jan 10, 2026

Copy link
Copy Markdown

🤖 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.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@bedevere-bot bedevere-bot removed the 🔨 test-with-buildbots Test PR w/ buildbots; report in status section label Jan 10, 2026
@gpshead @claude
Address review: handle import_get_module errors in race condition fix
842c160
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 <[email protected]>
@gpshead
gpshead force-pushed the fix-import-race-condition branch from 45c02dc to 842c160 Compare January 10, 2026 21:14
@gpshead

gpshead commented Jan 10, 2026

Copy link
Copy Markdown
Member Author

the few buildbot failures in the earlier round all looked like network issues. not rerunning those.

gpshead reacted with laugh emoji
All reactions
  • 😄 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@gpshead
gpshead requested review from picnixz and pitrou January 11, 2026 18:24
picnixz
picnixz reviewed Jan 11, 2026
View reviewed changes
Comment thread Python/import.c Resolved

Uh oh!

There was an error while loading. Please reload this page.

@gpshead
remove extra decref, oops! (should've rerun buildbots afterall)
102845b
@gpshead gpshead added 🔨 test-with-buildbots Test PR w/ buildbots; report in status section 🔨 test-with-refleak-buildbots Test PR w/ refleak buildbots; report in status section labels Jan 11, 2026
@bedevere-bot

bedevere-bot commented Jan 11, 2026

Copy link
Copy Markdown

🤖 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.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@bedevere-bot

bedevere-bot commented Jan 11, 2026

Copy link
Copy Markdown

🤖 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.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@bedevere-bot bedevere-bot removed 🔨 test-with-buildbots Test PR w/ buildbots; report in status section 🔨 test-with-refleak-buildbots Test PR w/ refleak buildbots; report in status section labels Jan 11, 2026
@gpshead
gpshead requested a review from picnixz January 12, 2026 07:48
@encukou
Merge branch 'main' into fix-import-race-condition
179d7bf
@encukou encukou added the 🔨 test-with-buildbots Test PR w/ buildbots; report in status section label Feb 4, 2026
@bedevere-bot

bedevere-bot commented Feb 4, 2026

Copy link
Copy Markdown

🤖 New build scheduled with the buildbot fleet by @encukou for commit 179d7bf 🤖

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.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@bedevere-bot bedevere-bot removed the 🔨 test-with-buildbots Test PR w/ buildbots; report in status section label Feb 4, 2026
@encukou
encukou merged commit ac8b5b6 into python:main Feb 10, 2026
118 of 120 checks passed

Uh oh!

There was an error while loading. Please reload this page.

@miss-islington-app

miss-islington-app Bot commented Feb 10, 2026

Copy link
Copy Markdown

Thanks @gpshead for the PR, and @encukou for merging it 🌮🎉.. I'm working now to backport this PR to: 3.13, 3.14.
🐍🍒⛏🤖

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@bedevere-app bedevere-app Bot removed the awaiting core review label Feb 10, 2026
miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Feb 10, 2026
@gpshead @claude @miss-islington
pythongh-143650: Fix importlib race condition on import failure (pyth…
6cc1f0c
…onGH-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 ac8b5b6)

Co-authored-by: Gregory P. Smith <[email protected]>
Co-Authored-By: Claude Opus 4.5 <[email protected]>
@miss-islington-app

miss-islington-app Bot commented Feb 10, 2026

Copy link
Copy Markdown

Sorry, @gpshead and @encukou, I could not cleanly backport this to 3.13 due to a conflict.
Please backport using cherry_picker on command line.

cherry_picker ac8b5b6890006ee7254ea878866cb486ff835ecb 3.13
All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@miss-islington-app miss-islington-app Bot assigned encukou Feb 10, 2026
@bedevere-app

bedevere-app Bot commented Feb 10, 2026

Copy link
Copy Markdown

GH-144662 is a backport of this pull request to the 3.14 branch.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@bedevere-app bedevere-app Bot removed the needs backport to 3.14 bugs and security fixes label Feb 10, 2026
gpshead added a commit that referenced this pull request Feb 11, 2026
@miss-islington @gpshead @claude
[3.14] gh-143650: Fix importlib race condition on import failure (GH-…
7d07470
…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 ac8b5b6)

Co-authored-by: Gregory P. Smith <[email protected]>
Co-authored-by: Claude Opus 4.5 <[email protected]>
gpshead added a commit to gpshead/cpython that referenced this pull request Feb 11, 2026
@gpshead
pythongh-143650: Fix importlib race condition on import failure (pyth…
d6e953f
…onGH-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 <[email protected]>

cherry picked from ac8b5b6
@bedevere-app

bedevere-app Bot commented Feb 11, 2026

Copy link
Copy Markdown

GH-144697 is a backport of this pull request to the 3.13 branch.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@bedevere-app bedevere-app Bot removed the needs backport to 3.13 only security fixes label Feb 11, 2026
gpshead added a commit that referenced this pull request Feb 11, 2026
@gpshead
[3.13] gh-143650: Fix importlib race condition on import failure (GH-…
c117ef1
…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 <[email protected]>

cherry picked from ac8b5b6
thunder-coding pushed a commit to thunder-coding/cpython that referenced this pull request Feb 15, 2026
@gpshead @claude @thunder-coding
pythongh-143650: Fix importlib race condition on import failure (pyth…
30ed3bd
…onGH-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 <[email protected]>
brijkapadia pushed a commit to brijkapadia/cpython that referenced this pull request Feb 28, 2026
@gpshead @claude @brijkapadia
pythongh-143650: Fix importlib race condition on import failure (pyth…
6aa0be4
…onGH-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 <[email protected]>
ljfp pushed a commit to ljfp/cpython that referenced this pull request Apr 25, 2026
@gpshead @claude @ljfp
pythongh-143650: Fix importlib race condition on import failure (pyth…
40af15d
…onGH-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 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@brettcannon brettcannon Awaiting requested review from brettcannon brettcannon is a code owner
@ericsnowcurrently ericsnowcurrently Awaiting requested review from ericsnowcurrently ericsnowcurrently is a code owner
@ncoghlan ncoghlan Awaiting requested review from ncoghlan ncoghlan is a code owner
@warsaw warsaw Awaiting requested review from warsaw warsaw is a code owner
@kumaraditya303 kumaraditya303 Awaiting requested review from kumaraditya303 kumaraditya303 is a code owner
@pitrou pitrou Awaiting requested review from pitrou
@picnixz picnixz Awaiting requested review from picnixz

Uh oh!

There was an error while loading. Please reload this page.

Assignees

@encukou encukou

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

4 participants

@gpshead @bedevere-bot @picnixz @encukou
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.