René's URL Explorer Experiment


Title: gh-157841: Synchronize access to functools.partial's mutable state by Adarsh-Me · Pull Request #157979 · python/cpython · GitHub

Open Graph Title: gh-157841: Synchronize access to functools.partial's mutable state by Adarsh-Me · Pull Request #157979 · python/cpython

X Title: gh-157841: Synchronize access to functools.partial's mutable state by Adarsh-Me · Pull Request #157979 · python/cpython

Description: Bug report Issue: 157841 Type: crash / data race in a free-threaded build functools.partial mutates its own state from __setstate__() with no synchronisation against the readers of that state. Under --disable-gil the fields (func, args, keywords, phcount) and the vectorcall slot are published as independent stores, so a reader can combine two different states, and the displaced values' decrefs can run arbitrary code while the object is half-updated. Coordinating with @espressolee on #157841, who pointed out that the readers (partial_repr, partial_call, partial_vectorcall) take no critical section either at the current head — so making only the writer atomic would leave a reader sampling each field independently. Two separate invariants had to hold: lifetime — Py_NewRef(pto->args) loads then increments, so a concurrent replacement can drop the last reference in between; the acquisition itself has to be inside the critical section, not just the use; coherence — partial_vectorcall samples phcount and args separately and combines them into tot_nargs, so both have to come from one state. How to fix the bug Every access path takes a critical section on the partial and reads or writes the whole state from it: partial_setstate() builds the replacement values first, then installs func, args, keywords, phcount and the vectorcall pointer as one unit, and only drops the displaced references after the new state is fully published (their __del__/finalisers can re-enter and read the partial); partial_repr(), partial_call() and partial_vectorcall() take the same section, copy out the strong references and phcount, then do the formatting or the call after leaving it; partial_new()'s flatten path reaches inside its argument to copy func/args/keywords, which is a reader like any other, so it goes through the same protocol and owns the references it takes. PR feature rule This is a bugfix, not a new feature. Core Dev guide Devguide: add tests for bugs (not applied to cosmetic changes) Docs & Tools Documentation changes: not needed, this is a concurrency fix in an existing method's behaviour. Verification Free-threaded amd64t Release build of this branch (MSC v.1929, 3.16.0a0), Windows. The two new TestPartial cases were run against two builds that differ only in Modules/_functoolsmodule.c: build python -m test test_free_threading.test_functools -v main's _functoolsmodule.c, new tests present test_concurrent_setstate_and_reads → Fatal Python error: PyMutex_Unlock: unlocking mutex that is not locked this branch Ran 7 tests ... OK, Result: SUCCESS The tests are 10 threads × 1000 operations with sys.setswitchinterval(1e-6), covering repr + __reduce__ + call + partial(p, 3) flattening against a concurrent __setstate__, and they check a torn read rather than only a crash: every state written carries the same value twice, once positionally and once as a keyword, so a mixture of two states fails an assertion instead of happening to survive. What I did not measure: a writer-only patch (critical section in partial_setstate() but not in the readers), which is the shape @espressolee rightly says would not give readers a coherent snapshot. Happy to add that data point if it is useful, and to run anything on a Linux build — the numbers above are Windows-only. Issue: gh-157841

Open Graph Description: Bug report Issue: 157841 Type: crash / data race in a free-threaded build functools.partial mutates its own state from __setstate__() with no synchronisation against the readers of that state. Un...

X Description: Bug report Issue: 157841 Type: crash / data race in a free-threaded build functools.partial mutates its own state from __setstate__() with no synchronisation against the readers of that state. Un...

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

X: @github

direct link

Domain: github.com

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/:user_id/:repository/pull/:id/files(.:format)
route-controllerpull_requests
route-actionfiles
fetch-noncev2:d7cb8043-70ef-a904-5351-15e4ab869098
current-catalog-service-hashae870bc5e265a340912cde392f23dad3671a0a881730ffdadd82f2f57d81641b
request-idA4C0:2314:B84366:F47899:6AF85E6F
html-safe-nonceac864773dd508d9c62094c84e1a2ca0d1bf8d7de6c17046cf2cd2acf0842ce3b
visitor-payloadeyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBNEMwOjIzMTQ6Qjg0MzY2OkY0Nzg5OTo2QUY4NUU2RiIsInZpc2l0b3JfaWQiOiIyNTIwNTk0NzM4NzY3NzQ4NzE5IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0=
visitor-hmac698dfac3b0a9897c8ed7cf4e73ca50f88aacab48c2ff19f6c5dd22f51d7572fd
hovercard-subject-tagpull_request:4611085657
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/157979/files
twitter:imagehttps://avatars.githubusercontent.com/u/122873385?s=400&v=4
twitter:cardsummary_large_image
og:imagehttps://avatars.githubusercontent.com/u/122873385?s=400&v=4
og:image:altBug report Issue: 157841 Type: crash / data race in a free-threaded build functools.partial mutates its own state from __setstate__() with no synchronisation against the readers of that state. Un...
og:site_nameGitHub
og:typeobject
hostnamegithub.com
expected-hostnamegithub.com
None5713ecd83001b2f55c3509fb7a292bf7d66d0f167c7ae87a9f7eaec5a52bf8db
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
release653be32ff5c755082a4db662491bfbbcfc9112c1
ui-targetfull
theme-color#1e2327
color-schemelight dark

Links:

Skip to contenthttps://github.com/python/cpython/pull/157979/files#start-of-content
https://github.com/
Sign inhttps://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fpython%2Fcpython%2Fpull%2F157979%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%2F157979%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/157979/files
Reloadhttps://github.com/python/cpython/pull/157979/files
Reloadhttps://github.com/python/cpython/pull/157979/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/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
Adarsh-Mehttps://github.com/Adarsh-Me
python:mainhttps://github.com/python/cpython/tree/main
Adarsh-Me:gh-157841-partial-setstatehttps://github.com/Adarsh-Me/cpython/tree/gh-157841-partial-setstate
Conversation 8 https://github.com/python/cpython/pull/157979
Commits 2 https://github.com/python/cpython/pull/157979/commits
Checks 55 https://github.com/python/cpython/pull/157979/checks
Files changed https://github.com/python/cpython/pull/157979/files
Please reload this pagehttps://github.com/python/cpython/pull/157979/files
gh-157841: Synchronize access to functools.partial's mutable state https://github.com/python/cpython/pull/157979/files#top
Show all changes 2 commits https://github.com/python/cpython/pull/157979/files
1479169 gh-157841: Synchronize access to functools.partial's mutable state Adarsh-Me Sep 21, 2026 https://github.com/python/cpython/pull/157979/commits/14791690daf9763a19f70b50e53d370d40cdf9fe
51351e7 gh-157841: Test concurrent functools.partial mutation Adarsh-Me Sep 21, 2026 https://github.com/python/cpython/pull/157979/commits/51351e7e416f5402c8720807e7ee293f7e2cbb68
Clear filters https://github.com/python/cpython/pull/157979/files
Please reload this pagehttps://github.com/python/cpython/pull/157979/files
Please reload this pagehttps://github.com/python/cpython/pull/157979/files
test_functools.py https://github.com/python/cpython/pull/157979/files#diff-3eb5a6d77f0c1af332fdb7c6a1bf554670eeae42d0709aeac4ad2f16722f3cd4
2026-09-21-17-35-00.gh-issue-157841.Kp9zLm.rst https://github.com/python/cpython/pull/157979/files#diff-0ef5fad4ebf8a2d0fcbb077be13b425172e13f75d46e2510fe077b835987718b
_functoolsmodule.c https://github.com/python/cpython/pull/157979/files#diff-d1deea0fb29260d389bac1b24c3ec3631e843d177e85c0e93d5cf50966dee630
Lib/test/test_free_threading/test_functools.pyhttps://github.com/python/cpython/pull/157979/files#diff-3eb5a6d77f0c1af332fdb7c6a1bf554670eeae42d0709aeac4ad2f16722f3cd4
View file https://github.com/Adarsh-Me/cpython/blob/51351e7e416f5402c8720807e7ee293f7e2cbb68/Lib/test/test_free_threading/test_functools.py
Open in desktop https://desktop.github.com
https://github.co/hiddenchars
https://github.com/python/cpython/pull/157979/{{ revealButtonHref }}
https://github.com/python/cpython/pull/157979/files#diff-3eb5a6d77f0c1af332fdb7c6a1bf554670eeae42d0709aeac4ad2f16722f3cd4
https://github.com/python/cpython/pull/157979/files#diff-3eb5a6d77f0c1af332fdb7c6a1bf554670eeae42d0709aeac4ad2f16722f3cd4
Misc/NEWS.d/next/Library/2026-09-21-17-35-00.gh-issue-157841.Kp9zLm.rsthttps://github.com/python/cpython/pull/157979/files#diff-0ef5fad4ebf8a2d0fcbb077be13b425172e13f75d46e2510fe077b835987718b
View file https://github.com/Adarsh-Me/cpython/blob/51351e7e416f5402c8720807e7ee293f7e2cbb68/Misc/NEWS.d/next/Library/2026-09-21-17-35-00.gh-issue-157841.Kp9zLm.rst
Open in desktop https://desktop.github.com
https://github.co/hiddenchars
https://github.com/python/cpython/pull/157979/{{ revealButtonHref }}
Please reload this pagehttps://github.com/python/cpython/pull/157979/files
Please reload this pagehttps://github.com/python/cpython/pull/157979/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.