Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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

Closed
Assignees
gpshead
Labels
interpreter-core(Objects, Python, Grammar, and Parser dirs)stdlibStandard Library Python modules in the Lib/ directorytopic-importlibtype-bugAn unexpected behavior, bug, or error
@gpshead

Description

@gpshead

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

Race condition

  1. Thread 1 starts importing module X, adds it tosys.modules
  2. Thread 2 checkssys.modules, finds module X, takes the "skip lock" fast path
  3. Thread 1's import fails, removes module X fromsys.modules
  4. Thread 2 returns the stale module reference (not insys.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

importsysimportthreadingimporttempfileimportoswithtempfile.TemporaryDirectory()astmpdir:sys.path.insert(0,tmpdir)# Module that partially initializes then failswithopen(os.path.join(tmpdir,"failing_mod.py"),"w")asf:f.write("import time; time.sleep(0.05); raise RuntimeError('fail')")results= []defdo_import(delay):importtime;time.sleep(delay)try:mod=__import__("failing_mod")if"failing_mod"notinsys.modules:results.append("RACE: got module not in sys.modules")exceptRuntimeError: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

Metadata

Metadata

Assignees

Labels

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

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions


    [8]ページ先頭

    ©2009-2026 Movatter.jp