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 — importlib race condition allows stale module ref to escape on failure · Issue #143650 · python/cpython

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

New issue
Copy link
New issue
Copy link
Closed
Closed
importlib race condition allows stale module ref to escape on failure#143650
Copy link
Assignees
gpshead
Labels
interpreter-core(Objects, Python, Grammar, and Parser dirs)(Objects, Python, Grammar, and Parser dirs)stdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytopic-importlibtype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error

Description

@gpshead
gpshead
opened on Jan 10, 2026
Issue body actions

When a module import fails, other threads can receive a stale module reference that is no longer in sys.modules.

Race condition

  1. Thread 1 starts importing module X, adds it to sys.modules
  2. Thread 2 checks sys.modules, finds module X, takes the "skip lock" fast path
  3. Thread 1's import fails, removes module X from sys.modules
  4. Thread 2 returns the stale module reference (not in sys.modules)

Affected code paths

  • Lib/importlib/_bootstrap.py:_find_and_load() lines 1268-1282
  • Python/import.c:PyImport_GetModule()
  • Python/import.c:PyImport_ImportModuleLevelObject()

All three have an optimization that skips locking when a module appears already loaded, but none verify the module is still valid after checking _initializing.

Reproducer

import sys
import threading
import tempfile
import os

with tempfile.TemporaryDirectory() as tmpdir:
    sys.path.insert(0, tmpdir)
    
    # Module that partially initializes then fails
    with open(os.path.join(tmpdir, "failing_mod.py"), "w") as f:
        f.write("import time; time.sleep(0.05); raise RuntimeError('fail')")
    
    results = []
    def do_import(delay):
        import time; time.sleep(delay)
        try:
            mod = __import__("failing_mod")
            if "failing_mod" not in sys.modules:
                results.append("RACE: got module not in sys.modules")
        except RuntimeError:
            results.append("ok")
    
    t1 = threading.Thread(target=do_import, args=(0,))
    t2 = threading.Thread(target=do_import, args=(0.01,))
    t1.start(); t2.start(); t1.join(); t2.join()
    print(results)  # May show "RACE" on affected builds

Linked PRs

  • gh-143650: Fix importlib race condition on import failure #143651
  • [3.14] gh-143650: Fix importlib race condition on import failure (GH-143651) #144662
  • [3.13] gh-143650: Fix importlib race condition on import failure (GH-143651) #144697

Activity

  1. gpshead
    self-assigned this
    on Jan 10, 2026
  2. StanFromIreland
    added
    type-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
    interpreter-core(Objects, Python, Grammar, and Parser dirs)(Objects, Python, Grammar, and Parser dirs)
    stdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directory
    topic-importlib
    on Jan 10, 2026
  3. gpshead
    marked No protection: import numpy in two different threads can lead to race-condition #91238 as a duplicate of this issue on Jan 11, 2026
  4. encukou
    added a commit that references this issue on Feb 10, 2026

    gh-143650: Fix importlib race condition on import failure (GH-143651)

    ac8b5b6
  5. miss-islington
    added a commit that references this issue on Feb 10, 2026

    pythongh-143650: Fix importlib race condition on import failure (pyth…

    6cc1f0c
  6. gpshead
    added 3 commits that reference this issue on Feb 11, 2026

    [3.14] gh-143650: Fix importlib race condition on import failure (GH-…

    7d07470

    pythongh-143650: Fix importlib race condition on import failure (pyth…

    d6e953f

    [3.13] gh-143650: Fix importlib race condition on import failure (GH-…

    c117ef1
  7. encukou
    closed this as completedon Feb 11, 2026
  8. thunder-coding
    added a commit that references this issue on Feb 15, 2026

    pythongh-143650: Fix importlib race condition on import failure (pyth…

    30ed3bd
  9. brijkapadia
    added a commit that references this issue on Feb 28, 2026

    pythongh-143650: Fix importlib race condition on import failure (pyth…

    6aa0be4
  10. ljfp
    added a commit that references this issue on Apr 25, 2026

    pythongh-143650: Fix importlib race condition on import failure (pyth…

    40af15d
  11. Pyosch
    added a commit that references this issue on Jun 17, 2026

    fix(startup): remove background preloader daemon to stop import race

    4cc0a51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

  • @gpshead
    gpshead

Labels

interpreter-core(Objects, Python, Grammar, and Parser dirs)(Objects, Python, Grammar, and Parser dirs)stdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytopic-importlibtype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

    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.