René's URL Explorer Experiment


Title: gh-155619: Fix data races in the context watcher registry by cobed95 · Pull Request #155943 · python/cpython · GitHub

Open Graph Title: gh-155619: Fix data races in the context watcher registry by cobed95 · Pull Request #155943 · python/cpython

X Title: gh-155619: Fix data races in the context watcher registry by cobed95 · Pull Request #155943 · python/cpython

Description: #155619 Reproduction When threads race, the watcher registry hands out duplicate IDs and accepts duplicate clears. On current main the new tests fail on a free-threaded debug build: Test Fails How test_concurrent_add_clear_watchers 10/10 Two threads get the same ID, so one clear raises ValueError: No context watcher set for ID 0 test_concurrent_clear_same_watcher 10/10 Two, three or four clears of the same ID all succeed test_clear_watcher_races_notification 9/10 The same duplicate-ID ValueError The same binary with PYTHON_GIL=1 passes. (The double-clear test skips itself there, since the calls can't overlap.) gh-159739 already replaced the assert(cb != NULL) in notify_context_watchers() with a NULL check, so a clear racing with a context switch no longer crashes. What's left is that the registry is read and written with plain accesses from several threads, and an add or clear isn't atomic as a whole. Fix PyContext_AddWatcher() and PyContext_ClearWatcher() now run under Py_BEGIN_CRITICAL_SECTION_MUTEX() on a new PyMutex in the interpreter state, the same pattern as TYPE_LOCK. They run about once per extension, so the lock costs nothing that matters. notify_context_watchers() runs on every context switch and still takes no lock. It loads each callback with an acquire load, paired with the release store in PyContext_AddWatcher(), and keeps gh-159739's NULL check for a watcher cleared by an earlier callback or by another thread. I kept the bitmask rather than scanning all eight slots because it makes the common zero-watcher case one load and a not-taken branch. Docs Doc/c-api/contextvars.rst had no threading guidance at all, unlike dict and type watchers, so I added some. Testing The new tests pass 20/20 runs. test_free_threading, test_context, test_capi, test_contextlib and test_asyncio pass on a free-threaded debug build (5,149 tests), and so does the same binary with PYTHON_GIL=1 (1,690 tests). test_concurrent_clear_same_watcher releases its threads with a busy-wait instead of a Barrier. A barrier wakes them too far apart to overlap and found nothing in 20,000 rounds, while the busy-wait hits the race in about a quarter of rounds. No ThreadSanitizer run from me. It wouldn't catch the double clear anyway: in the previous revision every access was already atomic, so there was no data race to report, just clears that weren't atomic as a whole. Benchmarks pyperformance async_tree, async_tree_io, async_generators (asyncio runs each task step through ctx.run()), plus nbody as a control that never enters a context. Free-threaded build, baseline vs. this change: Benchmark Baseline Patched async_generators 245 ms +- 23 ms 236 ms +- 5 ms 1.04x faster, not significant async_tree_io 374 ms +- 23 ms 363 ms +- 5 ms 1.03x faster, not significant async_tree_none 175 ms +- 8 ms 177 ms +- 8 ms 1.01x slower, not significant nbody (control) 77.0 ms +- 5.6 ms 76.6 ms +- 1.5 ms 1.01x faster, not significant These were measured on the previous revision. Notification, the only path they exercise, hasn't changed since; add and clear now take a lock, but they aren't on that path. The default build was the same story, except async_generators came out 1.08x slower there. I think that one is noise: its spread is about 12% of the mean, t=-2.54, and the same benchmark went faster on the free-threaded build. These were plain -O3 builds with --fast on a laptop, so they answer "does this cost anything" and not much else; happy to redo them properly if it matters. Issue: gh-155619

Open Graph Description: #155619 Reproduction When threads race, the watcher registry hands out duplicate IDs and accepts duplicate clears. On current main the new tests fail on a free-threaded debug build: Test Fails H...

X Description: #155619 Reproduction When threads race, the watcher registry hands out duplicate IDs and accepts duplicate clears. On current main the new tests fail on a free-threaded debug build: Test Fails H...

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

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:1df14ce4-e7ad-4991-4c7f-4f6bb8a2e0bc
current-catalog-service-hashae870bc5e265a340912cde392f23dad3671a0a881730ffdadd82f2f57d81641b
request-idC036:3B07B3:FC7989:13C4A86:6B023B87
html-safe-noncebd0b5d445db6127bd1d490edf9f567df18223aa04b87c1efc9b356d0bb454169
visitor-payloadeyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJDMDM2OjNCMDdCMzpGQzc5ODk6MTNDNEE4Njo2QjAyM0I4NyIsInZpc2l0b3JfaWQiOiI3OTM1MDUyNzc2NjYzNTYzMTQzIiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0=
visitor-hmac9d031a5b09c2db326a6cc9f4cfd8875b4d2df45bdcfdc31586e99609e57de337
hovercard-subject-tagpull_request:4294152390
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/155943/files
twitter:imagehttps://avatars.githubusercontent.com/u/33369354?s=400&v=4
twitter:cardsummary_large_image
og:imagehttps://avatars.githubusercontent.com/u/33369354?s=400&v=4
og:image:alt#155619 Reproduction When threads race, the watcher registry hands out duplicate IDs and accepts duplicate clears. On current main the new tests fail on a free-threaded debug build: Test Fails H...
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/155943/files#start-of-content
https://github.com/
Sign inhttps://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fpython%2Fcpython%2Fpull%2F155943%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%2F155943%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/155943/files
Reloadhttps://github.com/python/cpython/pull/155943/files
Reloadhttps://github.com/python/cpython/pull/155943/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
cobed95https://github.com/cobed95
python:mainhttps://github.com/python/cpython/tree/main
cobed95:gh-155619https://github.com/cobed95/cpython/tree/gh-155619
Conversation 19 https://github.com/python/cpython/pull/155943
Commits 4 https://github.com/python/cpython/pull/155943/commits
Checks 52 https://github.com/python/cpython/pull/155943/checks
Files changed https://github.com/python/cpython/pull/155943/files
Please reload this pagehttps://github.com/python/cpython/pull/155943/files
gh-155619: Fix data races in the context watcher registry https://github.com/python/cpython/pull/155943/files#top
Show all changes 4 commits https://github.com/python/cpython/pull/155943/files
2b13f29 gh-155619: Fix data races in the context watcher registry cobed95 Aug 17, 2026 https://github.com/python/cpython/pull/155943/commits/2b13f29cf883272428bf7d377c35c29cf2dc57e1
54350da Merge branch 'main' into gh-155619 cobed95 Nov 10, 2026 https://github.com/python/cpython/pull/155943/commits/54350daeda197eed6987ac1be2f778c09fc2b6a6
56c8877 Serialize context watcher add and clear cobed95 Nov 10, 2026 https://github.com/python/cpython/pull/155943/commits/56c8877245a69884ebafc31366e231969df16f8d
d297fc5 Don't abort the barrier when the double-clear test passes cobed95 Nov 10, 2026 https://github.com/python/cpython/pull/155943/commits/d297fc5716397d31466337ad455d12efb9e4367f
Clear filters https://github.com/python/cpython/pull/155943/files
Please reload this pagehttps://github.com/python/cpython/pull/155943/files
Please reload this pagehttps://github.com/python/cpython/pull/155943/files
contextvars.rst https://github.com/python/cpython/pull/155943/files#diff-dcba959f99b35e4934a2cd76dfadf4e3387e455b7e717691e71ae94edd838538
pycore_interp_structs.h https://github.com/python/cpython/pull/155943/files#diff-6075b6142803c1eccc213aa150704fd639443c65311d5999f69a5ea77cb76756
test_context_watcher.py https://github.com/python/cpython/pull/155943/files#diff-c5b8ea5a427354ba801c959333326f7b9d8489aa454f811f03e95c0ce6bd0aaa
2026-08-17-16-08-05.gh-issue-155619.Qw8Ftz.rst https://github.com/python/cpython/pull/155943/files#diff-611c7f425a623658c31d41c30cb27a9c57af96977f59bf9d5c5f723383dcc24c
watchers.c https://github.com/python/cpython/pull/155943/files#diff-3a5f3035e419b37a194b533132a28c35c2d995c18cdd9ae3223393c2b3770ead
context.c https://github.com/python/cpython/pull/155943/files#diff-90866884a04307bbbb14a8c99bcb5c4b96edde026113a014601b2ae38881f149
https://github.com/python/cpython/blob/main/.github/CODEOWNERS#L235
Doc/c-api/contextvars.rsthttps://github.com/python/cpython/pull/155943/files#diff-dcba959f99b35e4934a2cd76dfadf4e3387e455b7e717691e71ae94edd838538
View file https://github.com/python/cpython/blob/d297fc5716397d31466337ad455d12efb9e4367f/Doc/c-api/contextvars.rst
Open in desktop https://desktop.github.com
https://github.co/hiddenchars
https://github.com/python/cpython/pull/155943/{{ revealButtonHref }}
https://github.com/python/cpython/pull/155943/files#diff-dcba959f99b35e4934a2cd76dfadf4e3387e455b7e717691e71ae94edd838538
https://github.com/python/cpython/pull/155943/files#diff-dcba959f99b35e4934a2cd76dfadf4e3387e455b7e717691e71ae94edd838538
https://github.com/python/cpython/pull/155943/files#diff-dcba959f99b35e4934a2cd76dfadf4e3387e455b7e717691e71ae94edd838538
https://github.com/python/cpython/pull/155943/files#diff-dcba959f99b35e4934a2cd76dfadf4e3387e455b7e717691e71ae94edd838538
Include/internal/pycore_interp_structs.hhttps://github.com/python/cpython/pull/155943/files#diff-6075b6142803c1eccc213aa150704fd639443c65311d5999f69a5ea77cb76756
View file https://github.com/python/cpython/blob/d297fc5716397d31466337ad455d12efb9e4367f/Include/internal/pycore_interp_structs.h
Open in desktop https://desktop.github.com
https://github.co/hiddenchars
https://github.com/python/cpython/pull/155943/{{ revealButtonHref }}
https://github.com/python/cpython/pull/155943/files#diff-6075b6142803c1eccc213aa150704fd639443c65311d5999f69a5ea77cb76756
https://github.com/python/cpython/pull/155943/files#diff-6075b6142803c1eccc213aa150704fd639443c65311d5999f69a5ea77cb76756
Lib/test/test_free_threading/test_context_watcher.pyhttps://github.com/python/cpython/pull/155943/files#diff-c5b8ea5a427354ba801c959333326f7b9d8489aa454f811f03e95c0ce6bd0aaa
View file https://github.com/python/cpython/blob/d297fc5716397d31466337ad455d12efb9e4367f/Lib/test/test_free_threading/test_context_watcher.py
Open in desktop https://desktop.github.com
https://github.co/hiddenchars
https://github.com/python/cpython/pull/155943/{{ revealButtonHref }}
johnghttps://github.com/johng
Nov 10, 2026https://github.com/python/cpython/pull/155943/files#r4530811737
Learn morehttps://docs.github.com/articles/managing-disruptive-comments/#hiding-a-comment
Please reload this pagehttps://github.com/python/cpython/pull/155943/files
johnghttps://github.com/johng
Nov 10, 2026https://github.com/python/cpython/pull/155943/files#r4529789732
Learn morehttps://docs.github.com/articles/managing-disruptive-comments/#hiding-a-comment
https://github.com/python/cpython/blob/main/Lib/test/test_free_threading/__init__.py#L7https://github.com/python/cpython/blob/main/Lib/test/test_free_threading/__init__.py#L7
Please reload this pagehttps://github.com/python/cpython/pull/155943/files
Misc/NEWS.d/next/Core_and_Builtins/2026-08-17-16-08-05.gh-issue-155619.Qw8Ftz.rsthttps://github.com/python/cpython/pull/155943/files#diff-611c7f425a623658c31d41c30cb27a9c57af96977f59bf9d5c5f723383dcc24c
View file https://github.com/python/cpython/blob/d297fc5716397d31466337ad455d12efb9e4367f/Misc/NEWS.d/next/Core_and_Builtins/2026-08-17-16-08-05.gh-issue-155619.Qw8Ftz.rst
Open in desktop https://desktop.github.com
https://github.co/hiddenchars
https://github.com/python/cpython/pull/155943/{{ revealButtonHref }}
Modules/_testcapi/watchers.chttps://github.com/python/cpython/pull/155943/files#diff-3a5f3035e419b37a194b533132a28c35c2d995c18cdd9ae3223393c2b3770ead
View file https://github.com/python/cpython/blob/d297fc5716397d31466337ad455d12efb9e4367f/Modules/_testcapi/watchers.c
Open in desktop https://desktop.github.com
https://github.co/hiddenchars
https://github.com/python/cpython/pull/155943/{{ revealButtonHref }}
https://github.com/python/cpython/pull/155943/files#diff-3a5f3035e419b37a194b533132a28c35c2d995c18cdd9ae3223393c2b3770ead
https://github.com/python/cpython/pull/155943/files#diff-3a5f3035e419b37a194b533132a28c35c2d995c18cdd9ae3223393c2b3770ead
https://github.com/python/cpython/pull/155943/files#diff-3a5f3035e419b37a194b533132a28c35c2d995c18cdd9ae3223393c2b3770ead
https://github.com/python/cpython/pull/155943/files#diff-3a5f3035e419b37a194b533132a28c35c2d995c18cdd9ae3223393c2b3770ead
https://github.com/python/cpython/blob/main/.github/CODEOWNERS#L240
Python/context.chttps://github.com/python/cpython/pull/155943/files#diff-90866884a04307bbbb14a8c99bcb5c4b96edde026113a014601b2ae38881f149
View file https://github.com/python/cpython/blob/d297fc5716397d31466337ad455d12efb9e4367f/Python/context.c
Open in desktop https://desktop.github.com
https://github.co/hiddenchars
https://github.com/python/cpython/pull/155943/{{ revealButtonHref }}
https://github.com/python/cpython/pull/155943/files#diff-90866884a04307bbbb14a8c99bcb5c4b96edde026113a014601b2ae38881f149
https://github.com/python/cpython/pull/155943/files#diff-90866884a04307bbbb14a8c99bcb5c4b96edde026113a014601b2ae38881f149
johnghttps://github.com/johng
Nov 10, 2026https://github.com/python/cpython/pull/155943/files#r4529984571
Learn morehttps://docs.github.com/articles/managing-disruptive-comments/#hiding-a-comment
Please reload this pagehttps://github.com/python/cpython/pull/155943/files
https://github.com/python/cpython/pull/155943/files#diff-90866884a04307bbbb14a8c99bcb5c4b96edde026113a014601b2ae38881f149
https://github.com/python/cpython/pull/155943/files#diff-90866884a04307bbbb14a8c99bcb5c4b96edde026113a014601b2ae38881f149
Please reload this pagehttps://github.com/python/cpython/pull/155943/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.