Skip to content

Conversation

@gpshead
Copy link
Member

@gpshead gpshead commented Feb 11, 2026

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 ac8b5b6

Co-Authored-By: Claude Opus 4.5 noreply@anthropic.com

Fixes #143650

…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 <noreply@anthropic.com>

cherry picked from ac8b5b6
@gpshead gpshead self-assigned this Feb 11, 2026
@gpshead gpshead enabled auto-merge (squash) February 11, 2026 06:15
@gpshead gpshead merged commit c117ef1 into python:3.13 Feb 11, 2026
42 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant