René's URL Explorer Experiment


Title: gh-151722: Fix data race on frozendict reads during construction in free-threading build by tonghuaroot · Pull Request #151724 · python/cpython · GitHub

Open Graph Title: gh-151722: Fix data race on frozendict reads during construction in free-threading build by tonghuaroot · Pull Request #151724 · python/cpython

X Title: gh-151722: Fix data race on frozendict reads during construction in free-threading build by tonghuaroot · Pull Request #151724 · python/cpython

Description: In the free-threading build, len(fd), repr(fd) and hash(fd) on a frozendict (PEP 814) read the object's internal state without synchronization. That is safe for a finished frozendict, but not while one is being constructed from a user mapping: the half-built object is already GC-tracked and reachable from other threads (the generic frozendict_new → dict_merge path runs user keys()/__getitem__, which can leak it via gc.get_objects()), so a reader races the construction-time inserts and resizes. ThreadSanitizer reports a data race, and a reader can observe the frozendict's length and contents change, which contradicts its immutability. What frozendict_length, the frozendict_repr/copy_lock_held empty-guards, and the frozendict_hash active-entry factor now read ma_used through GET_USED() (FT_ATOMIC_LOAD_SSIZE_RELAXED), matching dict_length and set_len. frozendict_repr now runs the _PyDict_Next entry-table walk (anydict_repr_impl) inside Py_BEGIN_CRITICAL_SECTION, exactly like dict_repr and like set_repr (the tp_repr of set/frozenset). frozendict_hash is split into a frozendict_hash_lock_held helper (the entry-table walk) wrapped in Py_BEGIN_CRITICAL_SECTION, with the cached ma_hash short-circuit left lock-free (atomic load), mirroring frozenset_hash/frozenset_hash_impl. Why this matches the existing conventions These are not new design choices; they mirror what dictobject.c and setobject.c already do: dict_repr wraps anydict_repr_impl in Py_BEGIN_CRITICAL_SECTION(self). set_repr wraps set_repr_lock_held the same way, and it is the tp_repr for both PySet_Type and PyFrozenSet_Type — so a frozen type already uses a critical-sectioned repr. The critical section is effective here because the construction side already holds Py_BEGIN_CRITICAL_SECTION(a) on the target in the generic dict_merge branch, and inserts run under it (setitem_lock_held). A reader that takes the object's critical section therefore serializes against construction. frozendict_hash was copied from frozenset_hash; keeping the cached-hash short-circuit lock-free with an atomic load keeps that parity. Scalar ma_used reads going through GET_USED() matches dict_length and set_len. The construction (write) side already uses STORE_USED (FT_ATOMIC_STORE_SSIZE_RELAXED) and a critical section, and is unchanged. Tests / verification On a free-threading + ThreadSanitizer build (./configure --disable-gil --with-thread-sanitizer), HEAD 9688d252d330b0b586760a121ee8c8f7215176e8: A reader thread hammering len/repr/hash on a frozendict that is still being constructed (leaked via gc.get_objects() from __getitem__) reports zero ThreadSanitizer warnings with this change, where unpatched main reports data races on all three. The lock-free read fast paths (__getitem__, get, in, iteration, __eq__) were already free-threading-safe and remain clean. FrozenDictTests / FrozenDictMappingTests (21 tests), test_set and test_free_threading all pass on the same build; test_dict passes. Note on frozendict copy copy_lock_held was deliberately made lock-free for frozendict sources in #145920. It reads source state the same way and so has the same latent construction-time race, but re-adding a critical section there would revert that performance decision, so this PR does not touch the copy table walk (only the scalar ma_used guard, which is a pure atomicity fix). The construction-window question for copy is discussed in the linked issue.

Open Graph Description: In the free-threading build, len(fd), repr(fd) and hash(fd) on a frozendict (PEP 814) read the object's internal state without synchronization. That is safe for a finished frozendict, but not w...

X Description: In the free-threading build, len(fd), repr(fd) and hash(fd) on a frozendict (PEP 814) read the object's internal state without synchronization. That is safe for a finished frozendict, but n...

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

X: @github

direct link

Domain: github.com

route-pattern/:user_id/:repository/pull/:id/files(.:format)
route-controllerpull_requests
route-actionfiles
fetch-noncev2:45f99e7f-bdaa-9776-4938-e69214b04a1b
current-catalog-service-hashae870bc5e265a340912cde392f23dad3671a0a881730ffdadd82f2f57d81641b
request-idD330:197185:23EE69:2FE031:6A503CE3
html-safe-nonce76d2b52016d73444a8079a78c1dc3f2b9028014979bc49e3729ccf2f8fd3e0ad
visitor-payloadeyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJEMzMwOjE5NzE4NToyM0VFNjk6MkZFMDMxOjZBNTAzQ0UzIiwidmlzaXRvcl9pZCI6Ijg0MzUwNDg4MTc0MzUzNjA0ODMiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ==
visitor-hmacdebdc1b2ea45c6ab1211ef9b60403bfc3d9c3c874e871a861aab42757e0798a0
hovercard-subject-tagpull_request:3897793720
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/151724/files
twitter:imagehttps://avatars.githubusercontent.com/u/23011166?s=400&v=4
twitter:cardsummary_large_image
og:imagehttps://avatars.githubusercontent.com/u/23011166?s=400&v=4
og:image:altIn the free-threading build, len(fd), repr(fd) and hash(fd) on a frozendict (PEP 814) read the object's internal state without synchronization. That is safe for a finished frozendict, but not w...
og:site_nameGitHub
og:typeobject
hostnamegithub.com
expected-hostnamegithub.com
Noned6dc8294eb500fa36bbc57472d61fe87c522f9c3c1d64f70f4926f66a66a7efb
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
released90666eaf032b7eb8975a378efe38e37f4ebb96c
ui-targetfull
theme-color#1e2327
color-schemelight dark

Links:

Skip to contenthttps://github.com/python/cpython/pull/151724/files#start-of-content
https://github.com/
Sign in https://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fpython%2Fcpython%2Fpull%2F151724%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%2F151724%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/151724/files
Reloadhttps://github.com/python/cpython/pull/151724/files
Reloadhttps://github.com/python/cpython/pull/151724/files
Please reload this pagehttps://github.com/python/cpython/pull/151724/files
python https://github.com/python
cpythonhttps://github.com/python/cpython
Please reload this pagehttps://github.com/python/cpython/pull/151724/files
Notifications https://github.com/login?return_to=%2Fpython%2Fcpython
Fork 34.9k https://github.com/login?return_to=%2Fpython%2Fcpython
Star 73.6k 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
tonghuaroothttps://github.com/tonghuaroot
python:mainhttps://github.com/python/cpython/tree/main
tonghuaroot:fix-frozendict-ft-read-racehttps://github.com/tonghuaroot/cpython/tree/fix-frozendict-ft-read-race
Conversation 3 https://github.com/python/cpython/pull/151724
Commits 3 https://github.com/python/cpython/pull/151724/commits
Checks 50 https://github.com/python/cpython/pull/151724/checks
Files changed https://github.com/python/cpython/pull/151724/files
Please reload this pagehttps://github.com/python/cpython/pull/151724/files
gh-151722: Fix data race on frozendict reads during construction in free-threading build https://github.com/python/cpython/pull/151724/files#top
Show all changes 3 commits https://github.com/python/cpython/pull/151724/files
675a368 gh-151722: Fix data race on frozendict reads during construction in f… tonghuaroot Jun 19, 2026 https://github.com/python/cpython/pull/151724/commits/675a368a1748f32745a22064f10058b3d60f7a99
8a4795a gh-151722: Add free-threading regression test for frozendict read race tonghuaroot Jun 19, 2026 https://github.com/python/cpython/pull/151724/commits/8a4795a173ed7f554af292910a8a7f9ed38bf826
47a4974 gh-151722: Move frozendict free-threading regression test into test_d… tonghuaroot Jun 19, 2026 https://github.com/python/cpython/pull/151724/commits/47a49747f46962724560dbf9cd75e72d83c20c33
Clear filters https://github.com/python/cpython/pull/151724/files
Please reload this pagehttps://github.com/python/cpython/pull/151724/files
Please reload this pagehttps://github.com/python/cpython/pull/151724/files
test_dict.py https://github.com/python/cpython/pull/151724/files#diff-25bb976fc431825293483b53edd6d2b24c6dcc6644e37b281371d4d1b6831303
2026-06-19-10-03-42.gh-issue-151722.oTiOPF.rst https://github.com/python/cpython/pull/151724/files#diff-5b6c6f4efe95f43664c30a9d87481b102a5e9cfa2fb3e98d1d7bbd90673c51b8
dictobject.c https://github.com/python/cpython/pull/151724/files#diff-b08a47ddc5bc20b2e99ac2e5aa199ca24a56b994e7bc64e918513356088c20ae
Lib/test/test_free_threading/test_dict.pyhttps://github.com/python/cpython/pull/151724/files#diff-25bb976fc431825293483b53edd6d2b24c6dcc6644e37b281371d4d1b6831303
View file https://github.com/tonghuaroot/cpython/blob/47a49747f46962724560dbf9cd75e72d83c20c33/Lib/test/test_free_threading/test_dict.py
Open in desktop https://desktop.github.com
https://github.co/hiddenchars
https://github.com/python/cpython/pull/151724/{{ revealButtonHref }}
https://github.com/python/cpython/pull/151724/files#diff-25bb976fc431825293483b53edd6d2b24c6dcc6644e37b281371d4d1b6831303
https://github.com/python/cpython/pull/151724/files#diff-25bb976fc431825293483b53edd6d2b24c6dcc6644e37b281371d4d1b6831303
https://github.com/python/cpython/pull/151724/files#diff-25bb976fc431825293483b53edd6d2b24c6dcc6644e37b281371d4d1b6831303
Misc/NEWS.d/next/Core_and_Builtins/2026-06-19-10-03-42.gh-issue-151722.oTiOPF.rsthttps://github.com/python/cpython/pull/151724/files#diff-5b6c6f4efe95f43664c30a9d87481b102a5e9cfa2fb3e98d1d7bbd90673c51b8
View file https://github.com/tonghuaroot/cpython/blob/47a49747f46962724560dbf9cd75e72d83c20c33/Misc/NEWS.d/next/Core_and_Builtins/2026-06-19-10-03-42.gh-issue-151722.oTiOPF.rst
Open in desktop https://desktop.github.com
https://github.co/hiddenchars
https://github.com/python/cpython/pull/151724/{{ revealButtonHref }}
https://github.com/python/cpython/blob/main/.github/CODEOWNERS#L215
Objects/dictobject.chttps://github.com/python/cpython/pull/151724/files#diff-b08a47ddc5bc20b2e99ac2e5aa199ca24a56b994e7bc64e918513356088c20ae
View file https://github.com/tonghuaroot/cpython/blob/47a49747f46962724560dbf9cd75e72d83c20c33/Objects/dictobject.c
Open in desktop https://desktop.github.com
https://github.co/hiddenchars
https://github.com/python/cpython/pull/151724/{{ revealButtonHref }}
https://github.com/python/cpython/pull/151724/files#diff-b08a47ddc5bc20b2e99ac2e5aa199ca24a56b994e7bc64e918513356088c20ae
https://github.com/python/cpython/pull/151724/files#diff-b08a47ddc5bc20b2e99ac2e5aa199ca24a56b994e7bc64e918513356088c20ae
https://github.com/python/cpython/pull/151724/files#diff-b08a47ddc5bc20b2e99ac2e5aa199ca24a56b994e7bc64e918513356088c20ae
https://github.com/python/cpython/pull/151724/files#diff-b08a47ddc5bc20b2e99ac2e5aa199ca24a56b994e7bc64e918513356088c20ae
https://github.com/python/cpython/pull/151724/files#diff-b08a47ddc5bc20b2e99ac2e5aa199ca24a56b994e7bc64e918513356088c20ae
https://github.com/python/cpython/pull/151724/files#diff-b08a47ddc5bc20b2e99ac2e5aa199ca24a56b994e7bc64e918513356088c20ae
https://github.com/python/cpython/pull/151724/files#diff-b08a47ddc5bc20b2e99ac2e5aa199ca24a56b994e7bc64e918513356088c20ae
https://github.com/python/cpython/pull/151724/files#diff-b08a47ddc5bc20b2e99ac2e5aa199ca24a56b994e7bc64e918513356088c20ae
https://github.com/python/cpython/pull/151724/files#diff-b08a47ddc5bc20b2e99ac2e5aa199ca24a56b994e7bc64e918513356088c20ae
https://github.com/python/cpython/pull/151724/files#diff-b08a47ddc5bc20b2e99ac2e5aa199ca24a56b994e7bc64e918513356088c20ae
Please reload this pagehttps://github.com/python/cpython/pull/151724/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.