René's URL Explorer Experiment


Title: PyContext watcher registry has data races in free-threaded builds · Issue #155619 · python/cpython · GitHub

Open Graph Title: PyContext watcher registry has data races in free-threaded builds · Issue #155619 · python/cpython

X Title: PyContext watcher registry has data races in free-threaded builds · Issue #155619 · python/cpython

Description: Bug description PyContext_AddWatcher() and PyContext_ClearWatcher() are public in Python 3.14's and above free-threaded builds, but CPython's context-watcher registry is accessed without synchronization. The public declarations are avail...

Open Graph Description: Bug description PyContext_AddWatcher() and PyContext_ClearWatcher() are public in Python 3.14's and above free-threaded builds, but CPython's context-watcher registry is accessed without synchroniz...

X Description: Bug description PyContext_AddWatcher() and PyContext_ClearWatcher() are public in Python 3.14's and above free-threaded builds, but CPython's context-watcher registry is accessed without sy...

Opengraph URL: https://github.com/python/cpython/issues/155619

X: @github

direct link

Domain: github.com


Hey, it has json ld scripts:
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"PyContext watcher registry has data races in free-threaded builds","articleBody":"### Bug description\n\n`PyContext_AddWatcher()` and `PyContext_ClearWatcher()` are public in Python 3.14's and above free-threaded builds, but CPython's context-watcher registry is accessed without synchronization.\n\nThe public declarations are available unconditionally for CPython 3.14+ builds:\n\n- [`PyContext_WatchCallback`, `PyContext_AddWatcher()`, and `PyContext_ClearWatcher()`](https://github.com/python/cpython/blob/dbac03b411549c0f40fbb66127ae6f445027fcc8/Include/cpython/context.h#L30-L62)\n\nThe per-interpreter registry consists of two ordinary, non-atomic fields:\n\n- [`PyInterpreterState.context_watchers[CONTEXT_MAX_WATCHERS]`](https://github.com/python/cpython/blob/dbac03b411549c0f40fbb66127ae6f445027fcc8/Include/internal/pycore_interp_structs.h#L979-L985), an array of callback function pointers\n- [`PyInterpreterState.active_context_watchers`](https://github.com/python/cpython/blob/dbac03b411549c0f40fbb66127ae6f445027fcc8/Include/internal/pycore_interp_structs.h#L982-L985), a `uint8_t` bitmask indicating which slots are active\n\nAll three operations access this shared state without a lock or atomic operations:\n\n1. [`notify_context_watchers()`](https://github.com/python/cpython/blob/dbac03b411549c0f40fbb66127ae6f445027fcc8/Python/context.c#L113-L142) reads the bitmask and callback pointers.\n2. [`PyContext_AddWatcher()`](https://github.com/python/cpython/blob/dbac03b411549c0f40fbb66127ae6f445027fcc8/Python/context.c#L145-L161) scans and writes the callback array, then performs a read-modify-write on the bitmask.\n3. [`PyContext_ClearWatcher()`](https://github.com/python/cpython/blob/dbac03b411549c0f40fbb66127ae6f445027fcc8/Python/context.c#L164-L180) reads and clears the callback slot, then performs another read-modify-write on the bitmask.\n\nNotification is performed directly by the thread whose current context changes: [`context_switched()` calls `notify_context_watchers()`](https://github.com/python/cpython/blob/dbac03b411549c0f40fbb66127ae6f445027fcc8/Python/context.c#L183-L190), including from `PyContext_Enter()` and `PyContext_Exit()`. On a free-threaded interpreter, multiple attached threads in the same interpreter can therefore dispatch callbacks concurrently while another thread calls AddWatcher or ClearWatcher.\n\nThis creates CPython-internal data races, independently of whether the callback protects its own state:\n\n| Concurrent operations | Shared objects | Possible result |\n|---|---|---|\n| AddWatcher / AddWatcher | Callback slots and active bitmask | Both callers can select the same empty slot, one callback can overwrite the other, and both can return the same watcher ID. Concurrent bitmask read-modify-writes can also lose an update. |\n| AddWatcher / notification | Callback slots and active bitmask | Notification can observe an active bit without a safely published callback pointer, or otherwise observe inconsistent/stale registry state. |\n| ClearWatcher / notification | Callback slots and active bitmask | Notification can snapshot an active bit and then load a callback slot which has become NULL. In a debug build this reaches `assert(cb != NULL)`; in a release build it may attempt to call a NULL or stale function pointer. |\n| ClearWatcher / ClearWatcher | Callback slot and active bitmask | Both callers can observe the slot as registered, and their unsynchronized reads/writes constitute a data race. |\n| AddWatcher / ClearWatcher | Callback slots and active bitmask | A slot may be reused while another operation is clearing it, producing a mismatched callback/active-bit state. |\n\nThe same implementation is present on the current [Python 3.14 branch](https://github.com/python/cpython/blob/e2ec020314487c3fea9807894172becb6c5f4577/Python/context.c#L113-L180).\n\nThere is also an API-lifetime question: `PyContext_ClearWatcher()` does not specify whether a callback already selected by another thread may begin or continue after ClearWatcher returns. Without that guarantee, an extension cannot know when it is safe to tear down state used by its callback. Separately, because notifications run on the switching threads, the documentation should say explicitly that a single `PyContext_WatchCallback` may be invoked concurrently by multiple threads in a free-threaded interpreter.\n\nThe [context watcher documentation](https://github.com/python/cpython/blob/dbac03b411549c0f40fbb66127ae6f445027fcc8/Doc/c-api/contextvars.rst#L104-L148) currently specifies registration, clearing, and exception handling, but does not describe any free-threading restrictions or concurrency guarantees.\n\nFor comparison:\n\n- [Dictionary watcher documentation](https://github.com/python/cpython/blob/dbac03b411549c0f40fbb66127ae6f445027fcc8/Doc/c-api/dict.rst#L443-L470) explicitly says AddWatcher and ClearWatcher are not internally synchronized and require external synchronization on free-threaded builds.\n- [Type watcher documentation](https://github.com/python/cpython/blob/dbac03b411549c0f40fbb66127ae6f445027fcc8/Doc/c-api/type.rst#L78-L99) explicitly requires `PyType_AddWatcher()` to run at startup before spawning the first thread.\n\nA startup-only rule would avoid concurrent AddWatcher calls, but it would not by itself make ClearWatcher safe against notifications from active threads. Context switches are normal runtime operations, so external synchronization against internal notification is not generally available to an extension.\n\n### Expected behavior\n\nAccess to CPython's per-interpreter context-watcher registry should be data-race-free in free-threaded builds.\n\nThe API should also define:\n\n- whether the same callback can execute concurrently on multiple threads;\n- whether `PyContext_ClearWatcher()` waits for, cancels, or permits already-selected/in-flight callbacks;\n- when callback-owned state may safely be destroyed after clearing a watcher.\n\nThe implementation likely needs synchronization or an atomic publication/snapshot scheme for AddWatcher, ClearWatcher, and notification. Any solution should account for callback reentrancy and avoid holding a registry lock while calling extension callbacks.\n\nAt minimum, if concurrent registration or clearing cannot be supported, the documentation should state enforceable startup/shutdown restrictions. Documentation alone does not appear sufficient for ClearWatcher racing with notification unless the API also provides a usable lifetime rule.\n\n### CPython versions\n\n- Python 3.14 branch at `e2ec020314487c3fea9807894172becb6c5f4577`\n- CPython main at `dbac03b411549c0f40fbb66127ae6f445027fcc8`\n\nThis report is based on source inspection. The races are platform-independent C data races in the free-threaded build; I have not included a probabilistic crash reproducer.\n\n### Operating systems\n\nAll platforms using a free-threaded CPython build.\n\n### Related issues\n\n- [gh-127124: Pass opaque state to PyContext_AddWatcher callback](https://github.com/python/cpython/issues/127124) is related to callback state management, but does not appear to cover synchronization of the watcher registry.\n\n\n### CPython versions tested on:\n\n3.14\n\n### Operating systems tested on:\n\nmacOS\n\n\u003c!-- gh-linked-prs -->\n### Linked PRs\n* gh-155943\n\u003c!-- /gh-linked-prs -->\n","author":{"url":"https://github.com/florentinl","@type":"Person","name":"florentinl"},"datePublished":"2026-08-12T11:45:34Z","url":"https://github.com/python/cpython/issues/155619"}

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/_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format)
route-controllervoltron_issues_fragments
route-actionissue_layout
fetch-noncev2:f2e4cafc-c14d-bd76-88fc-1e4034f865dc
current-catalog-service-hash81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114
request-id9D0E:1FAE:14FBA65:1BF27E6:6B022F6A
html-safe-nonce57e2136acf59305e706e577da05c026525cad86f6484c7906a406d32acdc013f
visitor-payloadeyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI5RDBFOjFGQUU6MTRGQkE2NToxQkYyN0U2OjZCMDIyRjZBIiwidmlzaXRvcl9pZCI6IjQwNzI0ODAwNzYyMzc3Nzg3OTQiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ==
visitor-hmac08d9528a31837c11bab2ff07d814e059d4ec27c229de6ca39b71b73d1d0a4901
hovercard-subject-tagissue:5130502966
github-keyboard-shortcutsrepository,issues,copilot
google-site-verificationApib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I
octolytics-urlhttps://collector.github.com/github/collect
analytics-location///voltron/issues_fragments/issue_layout
fb:app_id1401488693436528
apple-itunes-appapp-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/python/cpython/155619/issue_layout
twitter:imagehttps://opengraph.githubassets.com/64bd416edee51f3a9228215f5c80df3d62504e0a8d95c6c0437ee4a1193ee8f3/python/cpython/issues/155619
twitter:cardsummary_large_image
og:imagehttps://opengraph.githubassets.com/64bd416edee51f3a9228215f5c80df3d62504e0a8d95c6c0437ee4a1193ee8f3/python/cpython/issues/155619
og:image:altBug description PyContext_AddWatcher() and PyContext_ClearWatcher() are public in Python 3.14's and above free-threaded builds, but CPython's context-watcher registry is accessed without synchroniz...
og:image:width1200
og:image:height600
og:site_nameGitHub
og:typeobject
og:author:usernameflorentinl
hostnamegithub.com
expected-hostnamegithub.com
None87e774ee7c6b3bee0490394e4d3e57ec9dbc956f74b87ea685dc8996c1676456
turbo-cache-controlno-preview
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
disable-turbofalse
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
react-app-nameissues

Links:

Skip to contenthttps://github.com/python/cpython/issues/155619#start-of-content
https://github.com/
Sign inhttps://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fpython%2Fcpython%2Fissues%2F155619
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%2Fissues%2F155619
Sign uphttps://github.com/signup?ref_cta=Sign+up&ref_loc=header+logged+out&ref_page=%2F%3Cuser-name%3E%2F%3Crepo-name%3E%2Fvoltron%2Fissues_fragments%2Fissue_layout&source=header-repo&source_repo=python%2Fcpython
Reloadhttps://github.com/python/cpython/issues/155619
Reloadhttps://github.com/python/cpython/issues/155619
Reloadhttps://github.com/python/cpython/issues/155619
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
https://github.com/python/cpython
New issuehttps://github.com/login?return_to=https://github.com/python/cpython/issues/155619
New issuehttps://github.com/login?return_to=https://github.com/python/cpython/issues/155619
PyContext watcher registry has data races in free-threaded buildshttps://github.com/python/cpython/issues/155619#top
interpreter-core(Objects, Python, Grammar, and Parser dirs)https://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22interpreter-core%22
topic-free-threadinghttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22topic-free-threading%22
type-bugAn unexpected behavior, bug, or errorhttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22type-bug%22
https://github.com/florentinl
florentinlhttps://github.com/florentinl
on Aug 12, 2026https://github.com/python/cpython/issues/155619#issue-5130502966
PyContext_WatchCallback, PyContext_AddWatcher(), and PyContext_ClearWatcher()https://github.com/python/cpython/blob/dbac03b411549c0f40fbb66127ae6f445027fcc8/Include/cpython/context.h#L30-L62
PyInterpreterState.context_watchers[CONTEXT_MAX_WATCHERS]https://github.com/python/cpython/blob/dbac03b411549c0f40fbb66127ae6f445027fcc8/Include/internal/pycore_interp_structs.h#L979-L985
PyInterpreterState.active_context_watchershttps://github.com/python/cpython/blob/dbac03b411549c0f40fbb66127ae6f445027fcc8/Include/internal/pycore_interp_structs.h#L982-L985
notify_context_watchers()https://github.com/python/cpython/blob/dbac03b411549c0f40fbb66127ae6f445027fcc8/Python/context.c#L113-L142
PyContext_AddWatcher()https://github.com/python/cpython/blob/dbac03b411549c0f40fbb66127ae6f445027fcc8/Python/context.c#L145-L161
PyContext_ClearWatcher()https://github.com/python/cpython/blob/dbac03b411549c0f40fbb66127ae6f445027fcc8/Python/context.c#L164-L180
context_switched() calls notify_context_watchers()https://github.com/python/cpython/blob/dbac03b411549c0f40fbb66127ae6f445027fcc8/Python/context.c#L183-L190
Python 3.14 branchhttps://github.com/python/cpython/blob/e2ec020314487c3fea9807894172becb6c5f4577/Python/context.c#L113-L180
context watcher documentationhttps://github.com/python/cpython/blob/dbac03b411549c0f40fbb66127ae6f445027fcc8/Doc/c-api/contextvars.rst#L104-L148
Dictionary watcher documentationhttps://github.com/python/cpython/blob/dbac03b411549c0f40fbb66127ae6f445027fcc8/Doc/c-api/dict.rst#L443-L470
Type watcher documentationhttps://github.com/python/cpython/blob/dbac03b411549c0f40fbb66127ae6f445027fcc8/Doc/c-api/type.rst#L78-L99
gh-127124: Pass opaque state to PyContext_AddWatcher callbackhttps://github.com/python/cpython/issues/127124
gh-155619: Fix data races in the context watcher registry #155943https://github.com/python/cpython/pull/155943
florentinlhttps://github.com/florentinl
type-bugAn unexpected behavior, bug, or errorhttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22type-bug%22
on Aug 12, 2026https://github.com/python/cpython/issues/155619#event-29337175184
cocolatohttps://github.com/cocolato
interpreter-core(Objects, Python, Grammar, and Parser dirs)https://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22interpreter-core%22
topic-free-threadinghttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22topic-free-threading%22
on Aug 12, 2026https://github.com/python/cpython/issues/155619#event-29341823010
https://github.com/cobed95
cobed95https://github.com/cobed95
on Aug 17, 2026https://github.com/python/cpython/issues/155619#issuecomment-5312372968
@hugovkhttps://github.com/hugovk
@corona10https://github.com/corona10
cobed95https://github.com/cobed95
on Nov 10, 2026https://github.com/python/cpython/issues/155619#event-35690495848
Merge branch 'main' into pythongh-155619https://github.com/cobed95/cpython/commit/54350daeda197eed6987ac1be2f778c09fc2b6a6
pythongh-155619https://github.com/python/cpython/issues/155619
54350dahttps://github.com/cobed95/cpython/commit/54350daeda197eed6987ac1be2f778c09fc2b6a6
Sign up for freehttps://github.com/signup?return_to=https://github.com/python/cpython/issues/155619
Sign in to commenthttps://github.com/login?return_to=https://github.com/python/cpython/issues/155619
interpreter-core(Objects, Python, Grammar, and Parser dirs)https://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22interpreter-core%22
topic-free-threadinghttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22topic-free-threading%22
type-bugAn unexpected behavior, bug, or errorhttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22type-bug%22
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.