René's URL Explorer Experiment


Title: gh-149816: Fix race in subtype_getweakref in free-threading build by Abhi210 · Pull Request #150247 · python/cpython · GitHub

Open Graph Title: gh-149816: Fix race in subtype_getweakref in free-threading build by Abhi210 · Pull Request #150247 · python/cpython

X Title: gh-149816: Fix race in subtype_getweakref in free-threading build by Abhi210 · Pull Request #150247 · python/cpython

Description: Fixes gh-149816 This PR resolves finding 61 from the weakref race tracking issue: "Racy weakref head load before incref in Objects/typeobject.c". Description In free-threaded builds, subtype_getweakref() had a race condition where the weakref list head (*weaklistptr) was loaded and immediately passed to Py_NewRef(). If another thread concurrently triggered weakref_dealloc(), the weakref could be freed before the incref, leading to a use-after-free (UAF). This PR fixes the issue by: Wrapping the pointer load and incref sequence inside the existing LOCK_WEAKREFS(obj) / UNLOCK_WEAKREFS(obj) critical section. Replacing Py_NewRef() with _Py_TryIncref() to safely handle weakrefs whose refcount has reached zero during the race window. Returning Py_NewRef(Py_None) when the list is empty or the weakref is no longer live, preserving the existing API behavior. Testing Added regression tests in Lib/test/test_free_threading/test_weakref.py that: Exercise obj.__weakref__ concurrently from reader and mutator threads synchronized with threading.Barrier. Verify correct return values (None versus weakref.ref). Check refcount stability across repeated accesses to detect leaks or incorrect incref/decref behavior. AI Tool Disclosure: Claude Code was used to help understand the relevant code paths and to improve the clarity and documentation of the proposed tests. It was not used to modify Objects/typeobject.c; all code changes and final decisions were made by me.

Open Graph Description: Fixes gh-149816 This PR resolves finding 61 from the weakref race tracking issue: "Racy weakref head load before incref in Objects/typeobject.c". Description In free-threaded builds, sub...

X Description: Fixes gh-149816 This PR resolves finding 61 from the weakref race tracking issue: "Racy weakref head load before incref in Objects/typeobject.c". Description In free-threaded bui...

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

X: @github

direct link

Domain: github.com

route-pattern/:user_id/:repository/pull/:id/files(.:format)
route-controllerpull_requests
route-actionfiles
fetch-noncev2:eff22287-27d3-6453-f223-f0b407c6ab50
current-catalog-service-hashae870bc5e265a340912cde392f23dad3671a0a881730ffdadd82f2f57d81641b
request-id82DE:103B4D:E8B3F:13DF51:6A5259EE
html-safe-noncef36c674cad2543c579c0277b7808232e88e1129a0dc00735e7bdb8f4812329c4
visitor-payloadeyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4MkRFOjEwM0I0RDpFOEIzRjoxM0RGNTE6NkE1MjU5RUUiLCJ2aXNpdG9yX2lkIjoiMzAzMDI5Njk0NjA2Nzc5MDMxOCIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9
visitor-hmac045f5e7d7fa2e29fab5d3d6bac0ab56d5f92515334a6c362d285697d29169848
hovercard-subject-tagpull_request:3730318350
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/150247/files
twitter:imagehttps://avatars.githubusercontent.com/u/27881020?s=400&v=4
twitter:cardsummary_large_image
og:imagehttps://avatars.githubusercontent.com/u/27881020?s=400&v=4
og:image:altFixes gh-149816 This PR resolves finding 61 from the weakref race tracking issue: "Racy weakref head load before incref in Objects/typeobject.c". Description In free-threaded builds, sub...
og:site_nameGitHub
og:typeobject
hostnamegithub.com
expected-hostnamegithub.com
Noneb9a586c06a05a7a86fc7e3f4dbd03e42f6869085879aa184aa6369456dbd50fb
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
release7aed05249554b889eb33d002851a973eebcc7e91
ui-targetfull
theme-color#1e2327
color-schemelight dark

Links:

Skip to contenthttps://github.com/python/cpython/pull/150247/files#start-of-content
https://github.com/
Sign in https://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fpython%2Fcpython%2Fpull%2F150247%2Ffiles
GitHub CopilotWrite better code with AIhttps://github.com/features/copilot
GitHub Copilot appDirect agents from issue to mergehttps://github.com/features/ai/github-app
MCP RegistryNewIntegrate external toolshttps://github.com/mcp
ActionsAutomate any workflowhttps://github.com/features/actions
CodespacesInstant dev environmentshttps://github.com/features/codespaces
IssuesPlan and track workhttps://github.com/features/issues
Code ReviewManage code changeshttps://github.com/features/code-review
GitHub Advanced SecurityFind and fix vulnerabilitieshttps://github.com/security/advanced-security
Code securitySecure your code as you buildhttps://github.com/security/advanced-security/code-security
Secret protectionStop leaks before they starthttps://github.com/security/advanced-security/secret-protection
Why GitHubhttps://github.com/why-github
Documentationhttps://docs.github.com
Bloghttps://github.blog
Changeloghttps://github.blog/changelog
Marketplacehttps://github.com/marketplace
View all featureshttps://github.com/features
Enterpriseshttps://github.com/enterprise
Small and medium teamshttps://github.com/team
Startupshttps://github.com/enterprise/startups
Nonprofitshttps://github.com/solutions/industry/nonprofits
App Modernizationhttps://github.com/solutions/use-case/app-modernization
DevSecOpshttps://github.com/solutions/use-case/devsecops
DevOpshttps://github.com/solutions/use-case/devops
CI/CDhttps://github.com/solutions/use-case/ci-cd
View all use caseshttps://github.com/solutions/use-case
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
View all industrieshttps://github.com/solutions/industry
View all solutionshttps://github.com/solutions
AIhttps://github.com/resources/articles?topic=ai
Software Developmenthttps://github.com/resources/articles?topic=software-development
DevOpshttps://github.com/resources/articles?topic=devops
Securityhttps://github.com/resources/articles?topic=security
View all topicshttps://github.com/resources/articles
Customer storieshttps://github.com/customer-stories
Events & webinarshttps://github.com/resources/events
Ebooks & reportshttps://github.com/resources/whitepapers
Business insightshttps://github.com/solutions/executive-insights
GitHub Skillshttps://skills.github.com
Documentationhttps://docs.github.com
Customer supporthttps://support.github.com
Community forumhttps://github.com/orgs/community/discussions
Trust centerhttps://github.com/trust-center
Partnershttps://github.com/partners
View all resourceshttps://github.com/resources
GitHub SponsorsFund open source developershttps://github.com/open-source/sponsors
Security Labhttps://securitylab.github.com
Maintainer Communityhttps://maintainers.github.com
Acceleratorhttps://github.com/open-source/accelerator
GitHub Starshttps://stars.github.com
Archive Programhttps://archiveprogram.github.com
Topicshttps://github.com/topics
Trendinghttps://github.com/trending
Collectionshttps://github.com/collections
Enterprise platformAI-powered developer platformhttps://github.com/enterprise
GitHub Advanced SecurityEnterprise-grade security featureshttps://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
Pricinghttps://github.com/pricing
Search syntax tipshttps://docs.github.com/search-github/github-code-search/understanding-github-code-search-syntax
documentationhttps://docs.github.com/search-github/github-code-search/understanding-github-code-search-syntax
Sign in https://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fpython%2Fcpython%2Fpull%2F150247%2Ffiles
Sign up https://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/150247/files
Reloadhttps://github.com/python/cpython/pull/150247/files
Reloadhttps://github.com/python/cpython/pull/150247/files
Please reload this pagehttps://github.com/python/cpython/pull/150247/files
python https://github.com/python
cpythonhttps://github.com/python/cpython
Please reload this pagehttps://github.com/python/cpython/pull/150247/files
Notifications https://github.com/login?return_to=%2Fpython%2Fcpython
Fork 35k https://github.com/login?return_to=%2Fpython%2Fcpython
Star 73.7k https://github.com/login?return_to=%2Fpython%2Fcpython
Code https://github.com/python/cpython
Issues 5k+ https://github.com/python/cpython/issues
Pull requests 2.3k https://github.com/python/cpython/pulls
Actions https://github.com/python/cpython/actions
Projects https://github.com/python/cpython/projects
Security and quality 0 https://github.com/python/cpython/security
Insights https://github.com/python/cpython/pulse
Code https://github.com/python/cpython
Issues https://github.com/python/cpython/issues
Pull requests https://github.com/python/cpython/pulls
Actions https://github.com/python/cpython/actions
Projects https://github.com/python/cpython/projects
Security and quality https://github.com/python/cpython/security
Insights https://github.com/python/cpython/pulse
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
Abhi210https://github.com/Abhi210
python:mainhttps://github.com/python/cpython/tree/main
Abhi210:gh-149816-61https://github.com/Abhi210/cpython/tree/gh-149816-61
Conversation 2 https://github.com/python/cpython/pull/150247
Commits 1 https://github.com/python/cpython/pull/150247/commits
Checks 50 https://github.com/python/cpython/pull/150247/checks
Files changed 3 https://github.com/python/cpython/pull/150247/files
gh-149816: Fix race in subtype_getweakref in free-threading build https://github.com/python/cpython/pull/150247/files#top
Show all changes 1 commit https://github.com/python/cpython/pull/150247/files
7caab50 gh-149816: Fix race in subtype_getweakref in free-threading build Abhi210 May 22, 2026 https://github.com/python/cpython/pull/150247/commits/7caab507d1fa1fb794d579522e8e4e385b1c4de8
Clear filters https://github.com/python/cpython/pull/150247/files
Please reload this pagehttps://github.com/python/cpython/pull/150247/files
Please reload this pagehttps://github.com/python/cpython/pull/150247/files
test_weakref.py https://github.com/python/cpython/pull/150247/files#diff-dedac49c7822124c3b58b1b19e85075a2daf405447e77d4eb8092b47ba705a64
2026-05-22-17-10-29.gh-issue-149816.u1-iqT.rst https://github.com/python/cpython/pull/150247/files#diff-dc915291212875107e75c54c95f416ed9075d7edf2056295d7a68fe88ec1cafc
typeobject.c https://github.com/python/cpython/pull/150247/files#diff-1decebeef15f4e0b0ce106c665751ec55068d4d1d1825847925ad4f528b5b872
https://github.com/python/cpython/blob/main/.github/CODEOWNERS#L629
Lib/test/test_free_threading/test_weakref.pyhttps://github.com/python/cpython/pull/150247/files#diff-dedac49c7822124c3b58b1b19e85075a2daf405447e77d4eb8092b47ba705a64
View file https://github.com/python/cpython/blob/7caab507d1fa1fb794d579522e8e4e385b1c4de8/Lib/test/test_free_threading/test_weakref.py
Open in desktop https://desktop.github.com
https://github.co/hiddenchars
https://github.com/python/cpython/pull/150247/{{ revealButtonHref }}
Misc/NEWS.d/next/Core_and_Builtins/2026-05-22-17-10-29.gh-issue-149816.u1-iqT.rsthttps://github.com/python/cpython/pull/150247/files#diff-dc915291212875107e75c54c95f416ed9075d7edf2056295d7a68fe88ec1cafc
View file https://github.com/python/cpython/blob/7caab507d1fa1fb794d579522e8e4e385b1c4de8/Misc/NEWS.d/next/Core_and_Builtins/2026-05-22-17-10-29.gh-issue-149816.u1-iqT.rst
Open in desktop https://desktop.github.com
https://github.co/hiddenchars
https://github.com/python/cpython/pull/150247/{{ revealButtonHref }}
https://github.com/python/cpython/blob/main/.github/CODEOWNERS#L220
Objects/typeobject.chttps://github.com/python/cpython/pull/150247/files#diff-1decebeef15f4e0b0ce106c665751ec55068d4d1d1825847925ad4f528b5b872
View file https://github.com/python/cpython/blob/7caab507d1fa1fb794d579522e8e4e385b1c4de8/Objects/typeobject.c
Open in desktop https://desktop.github.com
https://github.co/hiddenchars
https://github.com/python/cpython/pull/150247/{{ revealButtonHref }}
https://github.com/python/cpython/pull/150247/files#diff-1decebeef15f4e0b0ce106c665751ec55068d4d1d1825847925ad4f528b5b872
https://github.com/python/cpython/pull/150247/files#diff-1decebeef15f4e0b0ce106c665751ec55068d4d1d1825847925ad4f528b5b872
Please reload this pagehttps://github.com/python/cpython/pull/150247/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.