René's URL Explorer Experiment


Title: importlib race condition allows stale module ref to escape on failure · Issue #143650 · python/cpython · GitHub

Open Graph Title: importlib race condition allows stale module ref to escape on failure · Issue #143650 · python/cpython

X Title: importlib race condition allows stale module ref to escape on failure · Issue #143650 · python/cpython

Description: When a module import fails, other threads can receive a stale module reference that is no longer in sys.modules. Race condition Thread 1 starts importing module X, adds it to sys.modules Thread 2 checks sys.modules, finds module X, takes...

Open Graph Description: When a module import fails, other threads can receive a stale module reference that is no longer in sys.modules. Race condition Thread 1 starts importing module X, adds it to sys.modules Thread 2 c...

X Description: When a module import fails, other threads can receive a stale module reference that is no longer in sys.modules. Race condition Thread 1 starts importing module X, adds it to sys.modules Thread 2 c...

Opengraph URL: https://github.com/python/cpython/issues/143650

X: @github

direct link

Domain: github.com


Hey, it has json ld scripts:
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"importlib race condition allows stale module ref to escape on failure","articleBody":"When a module import fails, other threads can receive a stale module reference that is no longer in `sys.modules`.\n\n## Race condition\n\n1. Thread 1 starts importing module X, adds it to `sys.modules`\n2. Thread 2 checks `sys.modules`, finds module X, takes the \"skip lock\" fast path\n3. Thread 1's import fails, removes module X from `sys.modules`\n4. Thread 2 returns the stale module reference (not in `sys.modules`)\n\n## Affected code paths\n\n- `Lib/importlib/_bootstrap.py:_find_and_load()` lines 1268-1282\n- `Python/import.c:PyImport_GetModule()` \n- `Python/import.c:PyImport_ImportModuleLevelObject()`\n\nAll three have an optimization that skips locking when a module appears already loaded, but none verify the module is still valid after checking `_initializing`.\n\n## Reproducer\n\n```python\nimport sys\nimport threading\nimport tempfile\nimport os\n\nwith tempfile.TemporaryDirectory() as tmpdir:\n    sys.path.insert(0, tmpdir)\n    \n    # Module that partially initializes then fails\n    with open(os.path.join(tmpdir, \"failing_mod.py\"), \"w\") as f:\n        f.write(\"import time; time.sleep(0.05); raise RuntimeError('fail')\")\n    \n    results = []\n    def do_import(delay):\n        import time; time.sleep(delay)\n        try:\n            mod = __import__(\"failing_mod\")\n            if \"failing_mod\" not in sys.modules:\n                results.append(\"RACE: got module not in sys.modules\")\n        except RuntimeError:\n            results.append(\"ok\")\n    \n    t1 = threading.Thread(target=do_import, args=(0,))\n    t2 = threading.Thread(target=do_import, args=(0.01,))\n    t1.start(); t2.start(); t1.join(); t2.join()\n    print(results)  # May show \"RACE\" on affected builds\n```\n\n\u003c!-- gh-linked-prs -->\n### Linked PRs\n* gh-143651\n* gh-144662\n* gh-144697\n\u003c!-- /gh-linked-prs -->\n","author":{"url":"https://github.com/gpshead","@type":"Person","name":"gpshead"},"datePublished":"2026-01-10T10:57:32Z","url":"https://github.com/python/cpython/issues/143650"}

react-profiling0
react-import-mapreact,react-dom,react-dom/client,react-dom/profiling,react-is,react-reconciler,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@d5750093facf
route-pattern/_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format)
route-controllervoltron_issues_fragments
route-actionissue_layout
fetch-noncev2:a2e7e6b6-4fcf-7061-a106-b6ff0e51c194
current-catalog-service-hash81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114
request-idBA2E:31D44E:5E9F8A:8105F2:6AED2541
html-safe-nonceec8987746778b17b15dd1c8f265517a11d9ab602f8750cd3de06aea8cc1d0df9
visitor-payloadeyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJCQTJFOjMxRDQ0RTo1RTlGOEE6ODEwNUYyOjZBRUQyNTQxIiwidmlzaXRvcl9pZCI6IjMwNzE1NjA0NzA3MTIxMDAxNjEiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ==
visitor-hmac44c3e97a481c25f5b228261717c4363475915a42c494b57e2128562b104c0d32
hovercard-subject-tagissue:3799517091
github-keyboard-shortcutsrepository,issues,copilot
google-site-verificationApib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I
octolytics-urlhttps://collector.github.com/github/collect
analytics-location///voltron/issues_fragments/issue_layout
fb:app_id1401488693436528
apple-itunes-appapp-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/python/cpython/143650/issue_layout
twitter:imagehttps://opengraph.githubassets.com/2a76e4639f1f8b8ba203836a4d5379f79050483989badfae7a8e7b673ed19406/python/cpython/issues/143650
twitter:cardsummary_large_image
og:imagehttps://opengraph.githubassets.com/2a76e4639f1f8b8ba203836a4d5379f79050483989badfae7a8e7b673ed19406/python/cpython/issues/143650
og:image:altWhen a module import fails, other threads can receive a stale module reference that is no longer in sys.modules. Race condition Thread 1 starts importing module X, adds it to sys.modules Thread 2 c...
og:image:width1200
og:image:height600
og:site_nameGitHub
og:typeobject
og:author:usernamegpshead
hostnamegithub.com
expected-hostnamegithub.com
None9d95365dda0576efe9b71f45807595e396bd0ca9286fba5367a05048e6ddf8f2
turbo-cache-controlno-preview
go-importgithub.com/python/cpython git https://github.com/python/cpython.git
octolytics-dimension-user_id1525981
octolytics-dimension-user_loginpython
octolytics-dimension-repository_id81598961
octolytics-dimension-repository_nwopython/cpython
octolytics-dimension-repository_publictrue
octolytics-dimension-repository_is_forkfalse
octolytics-dimension-repository_network_root_id81598961
octolytics-dimension-repository_network_root_nwopython/cpython
turbo-body-classeslogged-out env-production page-responsive
disable-turbofalse
browser-stats-urlhttps://api.github.com/_private/browser/stats
browser-errors-urlhttps://api.github.com/_private/browser/errors
release98b41fa4b5635ceb45a4c54320fced73817ff290
ui-targetfull
theme-color#1e2327
color-schemelight dark
react-app-nameissues

Links:

Skip to contenthttps://github.com/python/cpython/issues/143650#start-of-content
https://github.com/
Sign inhttps://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fpython%2Fcpython%2Fissues%2F143650
GitHubRepository hosting, code review, and collaborationhttps://github.com/platform
CopilotAI coding agent for tasks, projects, and automated workhttps://github.com/features/copilot
ActionsCI/CD and automationhttps://github.com/features/actions
Code QualityQuality analysis, fixes, and enforcementhttps://github.com/features/code-quality
CodespacesCloud development environmentshttps://github.com/features/codespaces
Code SecurityVulnerability detection and remediationhttps://github.com/security/advanced-security/code-security
Secret ProtectionSecret scanning and leak preventionhttps://github.com/security/advanced-security/secret-protection
All Featureshttps://github.com/features
Documentationhttps://docs.github.com/en
Traininghttps://learn.github.com/skills
Certificationshttps://learn.github.com/credentials
Build with Copilothttps://github.com/features/ai
Customize Copilothttps://docs.github.com/en/copilot/how-tos/copilot-on-github/customize-copilot/customize-copilot-overview
Integrationshttps://github.com/marketplace
Trending reposhttps://github.com/trending
GitHub Sponsorshttps://github.com/open-source/sponsors
Developer articleshttps://github.com/resources/articles
ChangelogYour source for the latest features, improvements, and fixes across GitHub products, all in one place.https://github.blog/changelog/
Enterpriseshttps://github.com/enterprise
Teamhttps://github.com/team
Startupshttps://github.com/enterprise/startups
Nonprofitshttps://github.com/solutions/industry/nonprofits
Healthcarehttps://github.com/solutions/industry/healthcare
Financial serviceshttps://github.com/solutions/industry/financial-services
Manufacturinghttps://github.com/solutions/industry/manufacturing
Governmenthttps://github.com/solutions/industry/government
See all industrieshttps://github.com/solutions/industry
Why GitHubhttps://github.com/why-github
Customer Storieshttps://github.com/customer-stories
Business insightshttps://github.com/solutions/executive-insights
Trust Centerhttps://github.com/trust-center
GitHub Advanced SecurityEnterprise-grade code securityhttps://github.com/security/advanced-security
Copilot for businessEnterprise-grade AI featureshttps://github.com/features/copilot/copilot-business
Premium SupportEnterprise-grade 24/7 supporthttps://github.com/enterprise/premium-support
Documentationhttps://docs.github.com/en
Resource centerhttps://github.com/resources
Bloghttps://github.blog/
Events & webinarshttps://github.com/resources/events
Community Forumhttps://github.com/orgs/community/discussions
Customer Storieshttps://github.com/customer-stories
Integrationshttps://github.com/marketplace
Customer supporthttps://help.github.com/
Traininghttps://learn.github.com/skills
GitHub Statushttps://www.githubstatus.com/
Abouthttps://github.com/about
Partnershttps://github.com/partners
Changeloghttps://github.blog/changelog/
GitHub UniverseThe event for developers, by developers — talks, workshops, and what’s next for GitHub.https://githubuniverse.com/
Pricinghttps://github.com/pricing
Sign inhttps://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fpython%2Fcpython%2Fissues%2F143650
Sign uphttps://github.com/signup?ref_cta=Sign+up&ref_loc=header+logged+out&ref_page=%2F%3Cuser-name%3E%2F%3Crepo-name%3E%2Fvoltron%2Fissues_fragments%2Fissue_layout&source=header-repo&source_repo=python%2Fcpython
Reloadhttps://github.com/python/cpython/issues/143650
Reloadhttps://github.com/python/cpython/issues/143650
Reloadhttps://github.com/python/cpython/issues/143650
pythonhttps://github.com/python
cpythonhttps://github.com/python/cpython
Codehttps://github.com/python/cpython
Issues5k+ (5k+)https://github.com/python/cpython/issues
Pull requests2.3k (2.3k)https://github.com/python/cpython/pulls
Actionshttps://github.com/python/cpython/actions
Projectshttps://github.com/python/cpython/projects
Security and qualityhttps://github.com/python/cpython/security
Insightshttps://github.com/python/cpython/pulse
https://github.com/python/cpython
New issuehttps://github.com/login?return_to=https://github.com/python/cpython/issues/143650
New issuehttps://github.com/login?return_to=https://github.com/python/cpython/issues/143650
importlib race condition allows stale module ref to escape on failurehttps://github.com/python/cpython/issues/143650#top
https://github.com/gpshead
interpreter-core(Objects, Python, Grammar, and Parser dirs)https://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22interpreter-core%22
stdlibStandard Library Python modules in the Lib/ directoryhttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22stdlib%22
topic-importlibhttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22topic-importlib%22
type-bugAn unexpected behavior, bug, or errorhttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22type-bug%22
https://github.com/gpshead
gpsheadhttps://github.com/gpshead
on Jan 10, 2026https://github.com/python/cpython/issues/143650#issue-3799517091
gh-143650: Fix importlib race condition on import failure #143651https://github.com/python/cpython/pull/143651
[3.14] gh-143650: Fix importlib race condition on import failure (GH-143651) #144662https://github.com/python/cpython/pull/144662
[3.13] gh-143650: Fix importlib race condition on import failure (GH-143651) #144697https://github.com/python/cpython/pull/144697
gpsheadhttps://github.com/gpshead
on Jan 10, 2026https://github.com/python/cpython/issues/143650#event-21962038656
StanFromIrelandhttps://github.com/StanFromIreland
type-bugAn unexpected behavior, bug, or errorhttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22type-bug%22
interpreter-core(Objects, Python, Grammar, and Parser dirs)https://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22interpreter-core%22
stdlibStandard Library Python modules in the Lib/ directoryhttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22stdlib%22
topic-importlibhttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22topic-importlib%22
on Jan 10, 2026https://github.com/python/cpython/issues/143650#event-21962266887
gpsheadhttps://github.com/gpshead
No protection: import numpy in two different threads can lead to race-condition #91238https://github.com/python/cpython/issues/91238
on Jan 11, 2026https://github.com/python/cpython/issues/143650#event-21973687725
encukouhttps://github.com/encukou
on Feb 10, 2026https://github.com/python/cpython/issues/143650#event-22673743737
gh-143650: Fix importlib race condition on import failure (GH-143651)https://github.com/python/cpython/commit/ac8b5b6890006ee7254ea878866cb486ff835ecb
gh-143650https://github.com/python/cpython/issues/143650
GH-143651https://github.com/python/cpython/pull/143651
ac8b5b6https://github.com/python/cpython/commit/ac8b5b6890006ee7254ea878866cb486ff835ecb
miss-islingtonhttps://github.com/miss-islington
on Feb 10, 2026https://github.com/python/cpython/issues/143650#event-22673747489
pythongh-143650: Fix importlib race condition on import failure (pyth…https://github.com/miss-islington/cpython/commit/6cc1f0cb179820a3d9bc5f0ad5e9e17c6e5d9cfc
pythongh-143650https://github.com/python/cpython/issues/143650
pyth…https://github.com/python/cpython/pull/143651
6cc1f0chttps://github.com/miss-islington/cpython/commit/6cc1f0cb179820a3d9bc5f0ad5e9e17c6e5d9cfc
gpsheadhttps://github.com/gpshead
on Feb 11, 2026https://github.com/python/cpython/issues/143650#event-22694304880
[3.14] gh-143650: Fix importlib race condition on import failure (GH-…https://github.com/python/cpython/commit/7d074702ebf2f456baee9696e73e176bf7fa5146
gh-143650https://github.com/python/cpython/issues/143650
GH-…https://github.com/python/cpython/pull/143651
7d07470https://github.com/python/cpython/commit/7d074702ebf2f456baee9696e73e176bf7fa5146
pythongh-143650: Fix importlib race condition on import failure (pyth…https://github.com/gpshead/cpython/commit/d6e953fb68c7dd212d25fffb14ea2e32d4fa0907
pythongh-143650https://github.com/python/cpython/issues/143650
pyth…https://github.com/python/cpython/pull/143651
d6e953fhttps://github.com/gpshead/cpython/commit/d6e953fb68c7dd212d25fffb14ea2e32d4fa0907
[3.13] gh-143650: Fix importlib race condition on import failure (GH-…https://github.com/python/cpython/commit/c117ef1a1c3940a22caa9c95f426e03547f48160
gh-143650https://github.com/python/cpython/issues/143650
GH-…https://github.com/python/cpython/pull/143651
c117ef1https://github.com/python/cpython/commit/c117ef1a1c3940a22caa9c95f426e03547f48160
encukouhttps://github.com/encukou
completedhttps://github.com/python/cpython/issues?q=is%3Aissue%20state%3Aclosed%20archived%3Afalse%20reason%3Acompleted
on Feb 11, 2026https://github.com/python/cpython/issues/143650#event-22702063842
thunder-codinghttps://github.com/thunder-coding
on Feb 15, 2026https://github.com/python/cpython/issues/143650#event-22797352588
pythongh-143650: Fix importlib race condition on import failure (pyth…https://github.com/thunder-coding/cpython/commit/30ed3bd41c10a8c75e3e47897a53d75e17ea0239
pythongh-143650https://github.com/python/cpython/issues/143650
pyth…https://github.com/python/cpython/pull/143651
30ed3bdhttps://github.com/thunder-coding/cpython/commit/30ed3bd41c10a8c75e3e47897a53d75e17ea0239
brijkapadiahttps://github.com/brijkapadia
on Feb 28, 2026https://github.com/python/cpython/issues/143650#event-23156606170
pythongh-143650: Fix importlib race condition on import failure (pyth…https://github.com/brijkapadia/cpython/commit/6aa0be49f4820a9c667b83ed6ab7b0b289614846
pythongh-143650https://github.com/python/cpython/issues/143650
pyth…https://github.com/python/cpython/pull/143651
6aa0be4https://github.com/brijkapadia/cpython/commit/6aa0be49f4820a9c667b83ed6ab7b0b289614846
ljfphttps://github.com/ljfp
on Apr 25, 2026https://github.com/python/cpython/issues/143650#event-24868314245
pythongh-143650: Fix importlib race condition on import failure (pyth…https://github.com/ljfp/cpython/commit/40af15d70297e82b8bf107b5bacfbcd2bb81aeb4
pythongh-143650https://github.com/python/cpython/issues/143650
pyth…https://github.com/python/cpython/pull/143651
40af15dhttps://github.com/ljfp/cpython/commit/40af15d70297e82b8bf107b5bacfbcd2bb81aeb4
Pyoschhttps://github.com/Pyosch
on Jun 17, 2026https://github.com/python/cpython/issues/143650#event-26866686969
fix(startup): remove background preloader daemon to stop import racehttps://github.com/Pyosch/vise-d/commit/4cc0a51b909fd02af38306db3869b6207e8c7545
4cc0a51https://github.com/Pyosch/vise-d/commit/4cc0a51b909fd02af38306db3869b6207e8c7545
Sign up for freehttps://github.com/signup?return_to=https://github.com/python/cpython/issues/143650
Sign in to commenthttps://github.com/login?return_to=https://github.com/python/cpython/issues/143650
gpsheadhttps://github.com/gpshead
interpreter-core(Objects, Python, Grammar, and Parser dirs)https://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22interpreter-core%22
stdlibStandard Library Python modules in the Lib/ directoryhttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22stdlib%22
topic-importlibhttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22topic-importlib%22
type-bugAn unexpected behavior, bug, or errorhttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22type-bug%22
https://github.com
Termshttps://docs.github.com/site-policy/github-terms/github-terms-of-service
Privacyhttps://docs.github.com/site-policy/privacy-policies/github-privacy-statement
Securityhttps://github.com/security
Statushttps://www.githubstatus.com/
Communityhttps://github.community/
Docshttps://docs.github.com/
Contacthttps://support.github.com?tags=dotcom-footer

Viewport: width=device-width


URLs of crawlers that visited me.