René's URL Explorer Experiment


Title: gh-160397: Don't take the warnings lock when emitting a warning by nascheme · Pull Request #160401 · python/cpython · GitHub

Open Graph Title: gh-160397: Don't take the warnings lock when emitting a warning by nascheme · Pull Request #160401 · python/cpython

X Title: gh-160397: Don't take the warnings lock when emitting a warning by nascheme · Pull Request #160401 · python/cpython

Description: C implementation of warnings held the per-interpreter warnings lock for the whole of warn_explicit(). That includes building the warning message (str() of the message, calling the category) and displaying it (showwarning(), linecache, writing to sys.stderr). These run arbitrary code and can block indefinitely, e.g. on a full stderr pipe, stalling every other thread that emits a warning or changes the filters. It also widens the window for problems when forking or finalizing while the lock is held (see gh-158195). With this change, emitting a warning doesn't take the lock at all. The lock is only used by the Python-level code that changes the filters (filterwarnings(), catch_warnings, etc.), as before. On the free-threaded build, the state that needs consistency is protected by smaller operations instead: The filter search uses the existing critical section on the filters list. The __warningregistry__ and onceregistry checks and updates use a critical section on the registry dict. Recording is a check-and-set, so concurrent threads emitting the same warning show it only once. filters_version is read once per warning and passed through the search and the registry update. If the filters changed in the meantime, the result is not recorded, so a registry is never stamped with a version that doesn't match its entries. Since a critical section can be suspended if the dict operations run code that blocks (e.g. a __del__), the stamp is checked again after recording and the registry is invalidated if it changed. Key hashes are computed before entering the critical section. The cached defaultaction, onceregistry and filters objects in the interpreter state are replaced atomically and the old value is released with _PyObject_XDecRefDelayed(), since other threads may be using it without holding a reference. On the default build, the critical sections are no-ops, so emitting a warning has no locking. The registry operations are single C calls that don't release the GIL for normal keys, so this keeps the behaviour of the 3.14 locking in practice, while removing it from the slow paths. The pure-Python implementation (_py_warnings) is unchanged. It already released the lock before displaying the warning, and it needs a reentrant lock since finalizers and signal handlers can emit warnings from inside the locked section. A new test checks that showwarning() runs without the lock held, so another thread can change the filters meanwhile (this deadlocked before). A backport should be done only with great care: it changes how concurrent warnings interleave. Likely we need to test a bunch of 3rd party libraries and ensure this wouldn't break them. The safe thing to do is keep this as a 3.16 change. Issue: gh-160397

Open Graph Description: C implementation of warnings held the per-interpreter warnings lock for the whole of warn_explicit(). That includes building the warning message (str() of the message, calling the category) and di...

X Description: C implementation of warnings held the per-interpreter warnings lock for the whole of warn_explicit(). That includes building the warning message (str() of the message, calling the category) and di...

Opengraph URL: https://github.com/python/cpython/pull/160401

X: @github

direct link

Domain: github.com

react-profiling0
react-import-mapreact,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322
route-pattern/:user_id/:repository/pull/:id/files(.:format)
route-controllerpull_requests
route-actionfiles
fetch-noncev2:3668f159-d116-5168-1b65-867e0b626458
current-catalog-service-hashae870bc5e265a340912cde392f23dad3671a0a881730ffdadd82f2f57d81641b
request-idCDAC:2A1EC8:85BFD8:A83BD5:6B0215BB
html-safe-nonce04fb3904f585d8d53b78f21dc2185c87f514369174a69a91ac7098b057b1998a
visitor-payloadeyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJDREFDOjJBMUVDODo4NUJGRDg6QTgzQkQ1OjZCMDIxNUJCIiwidmlzaXRvcl9pZCI6IjI5NzUxNjg4Mzc1MTY5MjQzNDciLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ==
visitor-hmac965b5adafafddef0909dca1ca32a4c8e88cea662b4d75161ac8ac9758b63172f
hovercard-subject-tagpull_request:4975570678
github-keyboard-shortcutsrepository,pull-request-list,pull-request-conversation,pull-request-files-changed,copilot
google-site-verificationApib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I
octolytics-urlhttps://collector.github.com/github/collect
analytics-location///pull_requests/show/files
fb:app_id1401488693436528
apple-itunes-appapp-id=1477376905, app-argument=https://github.com/python/cpython/pull/160401/files
twitter:imagehttps://avatars.githubusercontent.com/u/690853?s=400&v=4
twitter:cardsummary_large_image
og:imagehttps://avatars.githubusercontent.com/u/690853?s=400&v=4
og:image:altC implementation of warnings held the per-interpreter warnings lock for the whole of warn_explicit(). That includes building the warning message (str() of the message, calling the category) and di...
og:site_nameGitHub
og:typeobject
hostnamegithub.com
expected-hostnamegithub.com
None87e774ee7c6b3bee0490394e4d3e57ec9dbc956f74b87ea685dc8996c1676456
turbo-cache-controlno-preview
diff-viewunified
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 full-width
disable-turbotrue
browser-stats-urlhttps://api.github.com/_private/browser/stats
browser-errors-urlhttps://api.github.com/_private/browser/errors
release423dbb54c5e7bfecc838eb387c1435eb3d0e760d
ui-targetfull
theme-color#1e2327
color-schemelight dark

Links:

Skip to contenthttps://github.com/python/cpython/pull/160401/files#start-of-content
https://github.com/
Sign inhttps://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fpython%2Fcpython%2Fpull%2F160401%2Ffiles
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%2Fpull%2F160401%2Ffiles
Sign uphttps://github.com/signup?ref_cta=Sign+up&ref_loc=header+logged+out&ref_page=%2F%3Cuser-name%3E%2F%3Crepo-name%3E%2Fpull_requests%2Fshow%2Ffiles&source=header-repo&source_repo=python%2Fcpython
Reloadhttps://github.com/python/cpython/pull/160401/files
Reloadhttps://github.com/python/cpython/pull/160401/files
Reloadhttps://github.com/python/cpython/pull/160401/files
pythonhttps://github.com/python
cpythonhttps://github.com/python/cpython
Codehttps://github.com/python/cpython
Issues5k+ (5k+)https://github.com/python/cpython/issues
Pull requests2.4k (2.4k)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/insights
Sign up for GitHub https://github.com/signup?return_to=%2Fpython%2Fcpython%2Fissues%2Fnew%2Fchoose
terms of servicehttps://docs.github.com/terms
privacy statementhttps://docs.github.com/privacy
Sign inhttps://github.com/login?return_to=%2Fpython%2Fcpython%2Fissues%2Fnew%2Fchoose
kumaraditya303https://github.com/kumaraditya303
python:mainhttps://github.com/python/cpython/tree/main
nascheme:gh-160397-warnings-critical-sectionshttps://github.com/nascheme/cpython/tree/gh-160397-warnings-critical-sections
Conversation 3 https://github.com/python/cpython/pull/160401
Commits 11 https://github.com/python/cpython/pull/160401/commits
Checks 58 https://github.com/python/cpython/pull/160401/checks
Files changed https://github.com/python/cpython/pull/160401/files
Please reload this pagehttps://github.com/python/cpython/pull/160401/files
gh-160397: Don't take the warnings lock when emitting a warning https://github.com/python/cpython/pull/160401/files#top
Show all changes 11 commits https://github.com/python/cpython/pull/160401/files
9d3dc19 gh-160397: Don't take the warnings lock when emitting a warning nascheme Oct 22, 2026 https://github.com/python/cpython/pull/160401/commits/9d3dc193f0bd6c974a90e241670be55bcb90acae
5d0326b gh-160397: Don't leak warnings filters from ThreadTests.setUp() nascheme Oct 23, 2026 https://github.com/python/cpython/pull/160401/commits/5d0326b418a961f8d9caeaabfe1dbb5043ddb9bb
e562f72 Merge remote-tracking branch 'origin/main' into gh-160397-warnings-cr… nascheme Nov 3, 2026 https://github.com/python/cpython/pull/160401/commits/e562f7213d6acbecd861e4f669e3bc464434a5bd
34ace5c Rewrite some code comments. nascheme Nov 3, 2026 https://github.com/python/cpython/pull/160401/commits/34ace5c00ba80d7e3271e53a9765ca1255260817
d31ab6a Merge remote-tracking branch 'origin/main' into gh-160397-warnings-cr… nascheme Nov 3, 2026 https://github.com/python/cpython/pull/160401/commits/d31ab6ab4b9c41bfd4beaefac39280e406f6ae2c
2d78e63 Use _Py_XGetRef() for warnings state. nascheme Nov 9, 2026 https://github.com/python/cpython/pull/160401/commits/2d78e639c15f828fff13c6173126f1bb4a8decbd
f82442a Merge remote-tracking branch 'origin/main' into gh-160397-warnings-cr… nascheme Nov 9, 2026 https://github.com/python/cpython/pull/160401/commits/f82442aace8ed462196af12bb2381626568eb571
9a5b491 Read warnings filter version under the registry lock nascheme Nov 10, 2026 https://github.com/python/cpython/pull/160401/commits/9a5b4912f329a1bb7a70f514ad9b5e138b839d41
4fca4bf Merge remote-tracking branch 'origin/main' into gh-160397-warnings-cr… nascheme Nov 10, 2026 https://github.com/python/cpython/pull/160401/commits/4fca4bf35d806993d43c33bbdff163a7a3be6745
eff9ca5 Address review: drop unneeded changes in _warnings.c nascheme Nov 11, 2026 https://github.com/python/cpython/pull/160401/commits/eff9ca53a5f93ef77a14611c2c2f62b1c846bc2d
b01e1f6 Merge remote-tracking branch 'origin/main' into gh-160397-warnings-cr… nascheme Nov 11, 2026 https://github.com/python/cpython/pull/160401/commits/b01e1f656d8d286ee303e9dc7c5094c9a52c0844
Clear filters https://github.com/python/cpython/pull/160401/files
Please reload this pagehttps://github.com/python/cpython/pull/160401/files
Please reload this pagehttps://github.com/python/cpython/pull/160401/files
pycore_interp_structs.h https://github.com/python/cpython/pull/160401/files#diff-6075b6142803c1eccc213aa150704fd639443c65311d5999f69a5ea77cb76756
__init__.py https://github.com/python/cpython/pull/160401/files#diff-21be9e4809f5cd7dcd4665eb0054b4bf9d924c2c9ce0a010e85dba405e94d9e9
2026-10-22-21-19-51.gh-issue-160397.E-RwgUgm.rst https://github.com/python/cpython/pull/160401/files#diff-d680325d4e7c10b393e7a9488332444f01431cdc7884ecf65745fe36f1034f55
_warnings.c https://github.com/python/cpython/pull/160401/files#diff-868cb9c3ec7f49459c652319e480e03ee132976b45fbedacb94958f8e0a9ea58
Include/internal/pycore_interp_structs.hhttps://github.com/python/cpython/pull/160401/files#diff-6075b6142803c1eccc213aa150704fd639443c65311d5999f69a5ea77cb76756
View file https://github.com/nascheme/cpython/blob/b01e1f656d8d286ee303e9dc7c5094c9a52c0844/Include/internal/pycore_interp_structs.h
Open in desktop https://desktop.github.com
https://github.co/hiddenchars
https://github.com/python/cpython/pull/160401/{{ revealButtonHref }}
https://github.com/python/cpython/pull/160401/files#diff-6075b6142803c1eccc213aa150704fd639443c65311d5999f69a5ea77cb76756
https://github.com/python/cpython/pull/160401/files#diff-6075b6142803c1eccc213aa150704fd639443c65311d5999f69a5ea77cb76756
Lib/test/test_warnings/__init__.pyhttps://github.com/python/cpython/pull/160401/files#diff-21be9e4809f5cd7dcd4665eb0054b4bf9d924c2c9ce0a010e85dba405e94d9e9
View file https://github.com/nascheme/cpython/blob/b01e1f656d8d286ee303e9dc7c5094c9a52c0844/Lib/test/test_warnings/__init__.py
Open in desktop https://desktop.github.com
https://github.co/hiddenchars
https://github.com/python/cpython/pull/160401/{{ revealButtonHref }}
https://github.com/python/cpython/pull/160401/files#diff-21be9e4809f5cd7dcd4665eb0054b4bf9d924c2c9ce0a010e85dba405e94d9e9
https://github.com/python/cpython/pull/160401/files#diff-21be9e4809f5cd7dcd4665eb0054b4bf9d924c2c9ce0a010e85dba405e94d9e9
https://github.com/python/cpython/pull/160401/files#diff-21be9e4809f5cd7dcd4665eb0054b4bf9d924c2c9ce0a010e85dba405e94d9e9
https://github.com/python/cpython/pull/160401/files#diff-21be9e4809f5cd7dcd4665eb0054b4bf9d924c2c9ce0a010e85dba405e94d9e9
Misc/NEWS.d/next/Library/2026-10-22-21-19-51.gh-issue-160397.E-RwgUgm.rsthttps://github.com/python/cpython/pull/160401/files#diff-d680325d4e7c10b393e7a9488332444f01431cdc7884ecf65745fe36f1034f55
View file https://github.com/nascheme/cpython/blob/b01e1f656d8d286ee303e9dc7c5094c9a52c0844/Misc/NEWS.d/next/Library/2026-10-22-21-19-51.gh-issue-160397.E-RwgUgm.rst
Open in desktop https://desktop.github.com
https://github.co/hiddenchars
https://github.com/python/cpython/pull/160401/{{ revealButtonHref }}
Please reload this pagehttps://github.com/python/cpython/pull/160401/files
Please reload this pagehttps://github.com/python/cpython/pull/160401/files
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.