René's URL Explorer Experiment


Title: Free-threaded (no-GIL): dumps() crashes when another thread mutates the dict being serialized (PyDict_Next iterator invalidation) · Issue #234 · python-rapidjson/python-rapidjson · GitHub

Open Graph Title: Free-threaded (no-GIL): dumps() crashes when another thread mutates the dict being serialized (PyDict_Next iterator invalidation) · Issue #234 · python-rapidjson/python-rapidjson

X Title: Free-threaded (no-GIL): dumps() crashes when another thread mutates the dict being serialized (PyDict_Next iterator invalidation) · Issue #234 · python-rapidjson/python-rapidjson

Description: Ahead of the free-threading work in #225: dumps() walks the object being serialized with PyDict_Next, which is documented as unsafe against concurrent mutation. On a free-threaded interpreter, another thread structurally mutating that di...

Open Graph Description: Ahead of the free-threading work in #225: dumps() walks the object being serialized with PyDict_Next, which is documented as unsafe against concurrent mutation. On a free-threaded interpreter, anot...

X Description: Ahead of the free-threading work in #225: dumps() walks the object being serialized with PyDict_Next, which is documented as unsafe against concurrent mutation. On a free-threaded interpreter, anot...

Opengraph URL: https://github.com/python-rapidjson/python-rapidjson/issues/234

X: @github

direct link

Domain: github.com


Hey, it has json ld scripts:
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Free-threaded (no-GIL): dumps() crashes when another thread mutates the dict being serialized (PyDict_Next iterator invalidation)","articleBody":"Ahead of the free-threading work in #225: `dumps()` walks the object being\nserialized with `PyDict_Next`, which is documented as unsafe against concurrent\nmutation. On a free-threaded interpreter, another thread structurally mutating that\ndict while `dumps()` is walking it corrupts the walk and crashes.\n\n**Latent today, not live.** The extension does not declare `Py_MOD_GIL_NOT_USED`,\nso importing it on a free-threaded build re-enables the GIL and this is safe as\nshipped. It becomes reachable when the GIL is forced off (`PYTHON_GIL=0`) — i.e.\nthe moment free-threading support (#225) lands. So this is a concrete, reproducible\ninstance of what #225 needs to address, not a bug in current default behaviour.\n\n### Site (current `master`, `rapidjson.cpp`)\n\n```cpp\nstatic inline bool all_keys_are_string(PyObject* dict) {   // 2323\n    Py_ssize_t pos = 0; PyObject* key;\n    while (PyDict_Next(dict, &pos, &key, NULL))             // walks the user dict, no copy, no lock\n        if (!PyUnicode_Check(key)) return false;\n```\n\n`dumps(d)` walks `d` here and again in `dumps_internal` (2556 / 2596). `pos` indexes\nthe dict's internal table; a structural mutation from another thread (an insert that\ntriggers a resize, or a delete) reallocates or reorders that table, so `pos` then\npoints into freed / relocated memory.\n\n### Reproducer\n\n`python3.14.0rc1t`, python-rapidjson 1.23 built from source, `PYTHON_GIL=0`. 6\nthreads call `dumps(shared)` while 3 threads structurally mutate `shared`. 10\nrounds per arm:\n\n| arm | result |\n|---|---|\n| mutate, GIL off (`PYTHON_GIL=0`) | **10/10 SIGSEGV** |\n| control — no mutator thread | clean 10/10 |\n| control — mutate a *different* dict `dumps()` never sees | clean 10/10 |\n| control — same mutation, GIL left on | clean 10/10 |\n\nThe GIL-on control staying clean is the whole point: this is a free-threading\nfault, not a generic race, and it is safe under the GIL that ships today.\n\n\u003cdetails>\u003csummary>ft_rapidjson.py\u003c/summary>\n\n```python\n\"\"\"python-rapidjson 1.23 — iterator-invalidation in dumps() over a shared dict.\n\n`rapidjson.cpp:2320` all_keys_are_string() and dumps_internal() (2553/2593) walk\nthe user dict with `PyDict_Next`, no copy and no lock:\n\n    while (PyDict_Next(dict, &pos, &key, NULL))   // 2320\n        if (!PyUnicode_Check(key)) return false;\n\n`PyDict_Next` is documented unsafe against concurrent mutation: `pos` indexes the\ndict's internal table, and a structural mutation (insert that triggers a resize,\nor a delete) reallocates or reorders that table, so a `pos` captured before the\nmutation now points into freed/relocated memory.\n\nUnder the GIL, `dumps()` never yields mid-walk, so it is safe. Under\n`Py_GIL_DISABLED`, another thread structurally mutating the same dict while\n`dumps()` walks it invalidates the iteration → crash or garbage read.\n\nThis is the iterator-invalidation class, not borrow-then-use: the corrupted thing\nis the *walk's cursor over the container*, not a single borrowed item.\n\nRun with PYTHON_GIL=0 (the module does not declare Py_MOD_GIL_NOT_USED, so import\nre-enables the GIL otherwise).\n\"\"\"\nimport os\nimport sys\nimport threading\nimport time\n\nimport rapidjson\n\nN_DUMPERS = int(os.environ.get(\"N_DUMPERS\", 6))\nN_MUTATORS = int(os.environ.get(\"N_MUTATORS\", 3))\nSIZE = int(os.environ.get(\"SIZE\", 64))\nSECONDS = float(os.environ.get(\"SECONDS\", 6))\nMUTATE = os.environ.get(\"MUTATE\", \"1\") == \"1\"\nDECOY = os.environ.get(\"DECOY\", \"0\") == \"1\"\n\n\ndef fresh(n):\n    return {f\"k{i}\": i for i in range(n)}\n\n\ndef main():\n    gil = getattr(sys, \"_is_gil_enabled\", lambda: True)()\n    print(f\"py={sys.version.split()[0]} gil={gil} \"\n          f\"dumpers={N_DUMPERS} mutators={N_MUTATORS if MUTATE else 0} \"\n          f\"decoy={DECOY}\", flush=True)\n\n    shared = fresh(SIZE)\n    decoy = fresh(SIZE)\n    stop = threading.Event()\n    barrier = threading.Barrier(N_DUMPERS + (N_MUTATORS if MUTATE else 0) + 1)\n    dumped = [0] * N_DUMPERS\n\n    def dumper(t):\n        barrier.wait()\n        n = 0\n        while not stop.is_set():\n            try:\n                rapidjson.dumps(shared)\n            except Exception:\n                pass          # value/type errors from racing data are irrelevant\n            n += 1\n        dumped[t] = n\n\n    def mutator():\n        barrier.wait()\n        target = decoy if DECOY else shared\n        i = 0\n        while not stop.is_set():\n            # STRUCTURAL mutation: grow (insert → resize) and shrink (delete),\n            # which is what invalidates the PyDict_Next cursor.\n            target[f\"x{i}\"] = i\n            if i % 3 == 0 and len(target) > SIZE:\n                try:\n                    target.pop(next(iter(target)))\n                except (KeyError, StopIteration, RuntimeError):\n                    pass\n            i += 1\n\n    threads = [threading.Thread(target=dumper, args=(t,)) for t in range(N_DUMPERS)]\n    if MUTATE:\n        threads += [threading.Thread(target=mutator) for _ in range(N_MUTATORS)]\n    for th in threads:\n        th.start()\n    barrier.wait()\n    time.sleep(SECONDS)\n    stop.set()\n    for th in threads:\n        th.join()\n    print(f\"clean: {sum(dumped)} dumps survived\", flush=True)\n    return 0\n\n\nif __name__ == \"__main__\":\n    raise SystemExit(main())\n```\n\u003c/details>\n\n### A possible fix (when #225 is worked)\n\nTake a critical section on the dict for the duration of the walk\n(`Py_BEGIN_CRITICAL_SECTION(dict)` around the `PyDict_Next` loops), or snapshot the\nkeys/items before serializing. `all_keys_are_string` and the two `dumps_internal`\nwalks are the sites.\n\n### Not claimed\n\nNo severity — reachable only with the GIL disabled, which is not the default today.\nNo exploitability — the observed faults are consistent with the mechanism; I did not\nbuild a primitive.\n","author":{"url":"https://github.com/espressolee","@type":"Person","name":"espressolee"},"datePublished":"2026-08-05T15:00:48Z","url":"https://github.com/python-rapidjson/python-rapidjson/issues/234"}

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/_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format)
route-controllervoltron_issues_fragments
route-actionissue_layout
fetch-noncev2:83ba926b-3679-6c33-06e4-e89a1e1281e7
current-catalog-service-hash81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114
request-id93F4:3B6B64:B1E56C:EA3BE2:6AF85B15
html-safe-nonce43163a20e63b76d8b97404add6adc369fa3912f1e7644e9615664ffa6840db5d
visitor-payloadeyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI5M0Y0OjNCNkI2NDpCMUU1NkM6RUEzQkUyOjZBRjg1QjE1IiwidmlzaXRvcl9pZCI6Ijg1Mzc2ODMyNTg2MjIxMDYzODkiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ==
visitor-hmac39cfb033bec01f93b575704e50ab907607809a0707b716f834beda7081109b9e
hovercard-subject-tagissue:5072259193
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-rapidjson/python-rapidjson/234/issue_layout
twitter:imagehttps://opengraph.githubassets.com/c233a4076adfcf4a579381f5861bb48aeadbc9a27eb4dc2e31634a68a19bcbda/python-rapidjson/python-rapidjson/issues/234
twitter:cardsummary_large_image
og:imagehttps://opengraph.githubassets.com/c233a4076adfcf4a579381f5861bb48aeadbc9a27eb4dc2e31634a68a19bcbda/python-rapidjson/python-rapidjson/issues/234
og:image:altAhead of the free-threading work in #225: dumps() walks the object being serialized with PyDict_Next, which is documented as unsafe against concurrent mutation. On a free-threaded interpreter, anot...
og:image:width1200
og:image:height600
og:site_nameGitHub
og:typeobject
og:author:usernameespressolee
hostnamegithub.com
expected-hostnamegithub.com
None5713ecd83001b2f55c3509fb7a292bf7d66d0f167c7ae87a9f7eaec5a52bf8db
turbo-cache-controlno-preview
go-importgithub.com/python-rapidjson/python-rapidjson git https://github.com/python-rapidjson/python-rapidjson.git
octolytics-dimension-user_id25253642
octolytics-dimension-user_loginpython-rapidjson
octolytics-dimension-repository_id42155092
octolytics-dimension-repository_nwopython-rapidjson/python-rapidjson
octolytics-dimension-repository_publictrue
octolytics-dimension-repository_is_forkfalse
octolytics-dimension-repository_network_root_id42155092
octolytics-dimension-repository_network_root_nwopython-rapidjson/python-rapidjson
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
release653be32ff5c755082a4db662491bfbbcfc9112c1
ui-targetfull
theme-color#1e2327
color-schemelight dark
react-app-nameissues

Links:

Skip to contenthttps://github.com/python-rapidjson/python-rapidjson/issues/234#start-of-content
https://github.com/
Sign inhttps://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fpython-rapidjson%2Fpython-rapidjson%2Fissues%2F234
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-rapidjson%2Fpython-rapidjson%2Fissues%2F234
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-rapidjson%2Fpython-rapidjson
Reloadhttps://github.com/python-rapidjson/python-rapidjson/issues/234
Reloadhttps://github.com/python-rapidjson/python-rapidjson/issues/234
Reloadhttps://github.com/python-rapidjson/python-rapidjson/issues/234
python-rapidjsonhttps://github.com/python-rapidjson
python-rapidjsonhttps://github.com/python-rapidjson/python-rapidjson
Codehttps://github.com/python-rapidjson/python-rapidjson
Issues12 (12)https://github.com/python-rapidjson/python-rapidjson/issues
Pull requests7 (7)https://github.com/python-rapidjson/python-rapidjson/pulls
Actionshttps://github.com/python-rapidjson/python-rapidjson/actions
Projectshttps://github.com/python-rapidjson/python-rapidjson/projects
Wikihttps://github.com/python-rapidjson/python-rapidjson/wiki
Security and qualityhttps://github.com/python-rapidjson/python-rapidjson/security
Insightshttps://github.com/python-rapidjson/python-rapidjson/pulse
https://github.com/python-rapidjson/python-rapidjson
New issuehttps://github.com/login?return_to=https://github.com/python-rapidjson/python-rapidjson/issues/234
New issuehttps://github.com/login?return_to=https://github.com/python-rapidjson/python-rapidjson/issues/234
Free-threaded (no-GIL): dumps() crashes when another thread mutates the dict being serialized (PyDict_Next iterator invalidation)https://github.com/python-rapidjson/python-rapidjson/issues/234#top
https://github.com/espressolee
espressoleehttps://github.com/espressolee
on Aug 5, 2026https://github.com/python-rapidjson/python-rapidjson/issues/234#issue-5072259193
#225https://github.com/python-rapidjson/python-rapidjson/issues/225
#225https://github.com/python-rapidjson/python-rapidjson/issues/225
#225https://github.com/python-rapidjson/python-rapidjson/issues/225
#225https://github.com/python-rapidjson/python-rapidjson/issues/225
https://github.com/lelit
lelithttps://github.com/lelit
on Aug 6, 2026https://github.com/python-rapidjson/python-rapidjson/issues/234#issuecomment-5200885700
https://github.com/espressolee
espressoleehttps://github.com/espressolee
on Aug 6, 2026https://github.com/python-rapidjson/python-rapidjson/issues/234#issuecomment-5203870735
Extension isolation and subinterpreters support #226https://github.com/python-rapidjson/python-rapidjson/issues/226
Sign up for freehttps://github.com/signup?return_to=https://github.com/python-rapidjson/python-rapidjson/issues/234
Sign in to commenthttps://github.com/login?return_to=https://github.com/python-rapidjson/python-rapidjson/issues/234
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.