René's URL Explorer Experiment


Title: Segfault in _pickle.c:batch_dict_exact when pickling dict with concurrent mutation (free-threading) · Issue #146452 · python/cpython · GitHub

Open Graph Title: Segfault in _pickle.c:batch_dict_exact when pickling dict with concurrent mutation (free-threading) · Issue #146452 · python/cpython

X Title: Segfault in _pickle.c:batch_dict_exact when pickling dict with concurrent mutation (free-threading) · Issue #146452 · python/cpython

Description: Bug report Bug description I've been testing the free-threaded build for thread safety issues in unaudited modules (ref #116738 — _pickle.c is unchecked). Found a segfault when pickle.dumps() runs concurrently with dict mutations on the ...

Open Graph Description: Bug report Bug description I've been testing the free-threaded build for thread safety issues in unaudited modules (ref #116738 — _pickle.c is unchecked). Found a segfault when pickle.dumps() runs ...

X Description: Bug report Bug description I've been testing the free-threaded build for thread safety issues in unaudited modules (ref #116738 — _pickle.c is unchecked). Found a segfault when pickle.dumps() r...

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

X: @github

direct link

Domain: github.com


Hey, it has json ld scripts:
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Segfault in _pickle.c:batch_dict_exact when pickling dict with concurrent mutation (free-threading)","articleBody":"# Bug report\n\n### Bug description\n\nI've been testing the free-threaded build for thread safety issues in unaudited modules (ref #116738 — `_pickle.c` is unchecked). Found a segfault when `pickle.dumps()` runs concurrently with dict mutations on the same dict.\n\nThe crash happens in `batch_dict_exact` at `Modules/_pickle.c:3496` — it iterates dict items via `_PyDict_Next` which gives borrowed references, but there's no critical section on the dict. If another thread pops or replaces a key mid-iteration, the borrowed reference goes stale and `Py_INCREF` hits a freed/NULL pointer.\n\nSometimes it raises `RuntimeError: dictionary changed size during iteration` cleanly, but other times the reference gets invalidated before the size check and it just segfaults.\n\n### Reproducer\n\n```python\nimport pickle\nimport threading\n\nshared = {str(i): list(range(50)) for i in range(100)}\n\nfor trial in range(2000):\n    barrier = threading.Barrier(3)\n\n    def dumper():\n        try:\n            barrier.wait()\n            pickle.dumps(shared)\n        except Exception:\n            pass\n\n    def mutator():\n        try:\n            barrier.wait()\n            for j in range(200):\n                key = str(j % 100)\n                shared[key] = list(range(j % 50))\n                if j % 10 == 0:\n                    shared.pop(str(j % 100), None)\n                    shared[str(j % 100)] = [j]\n        except Exception:\n            pass\n\n    threads = [\n        threading.Thread(target=dumper),\n        threading.Thread(target=dumper),\n        threading.Thread(target=mutator),\n    ]\n    for t in threads: t.start()\n    for t in threads: t.join()\n```\n\nCrashes within a few hundred iterations usually. I ran it 10 times, got 9 segfaults.\n\n### Tested on\n\n**3.14.0a4 free-threading (stock install, no sanitizers):**\n```\n$ python3.14t reproducer.py\nSegmentation fault (core dumped)\n```\n\n**3.15.0a7+ free-threading (built with `--with-address-sanitizer`):**\n```\nAddressSanitizer:DEADLYSIGNAL\n==1334802==ERROR: AddressSanitizer: SEGV on unknown address 0x00000000000c\n    #0 _Py_atomic_load_uint32_relaxed Include/cpython/pyatomic_gcc.h:367\n    #1 Py_INCREF Include/refcount.h:267\n    #2 batch_dict_exact Modules/_pickle.c:3496\n    #3 save_dict Modules/_pickle.c:3563\n    #4 save Modules/_pickle.c:4568\n    #5 dump Modules/_pickle.c:4768\n    #6 _pickle_dumps_impl Modules/_pickle.c:7992\n```\n\n### Root cause\n\n- `batch_dict_exact` calls `PyDict_Next()` which returns borrowed references to key and value\n- Between `PyDict_Next()` returning and `Py_INCREF(key)` / `Py_INCREF(value)`, another thread can mutate the dict\n- The borrowed reference points to a freed object, `Py_INCREF` dereferences it and segfaults\n- The size check at line 3512 (`PyDict_GET_SIZE(obj) != dict_size`) only catches mutations that change dict size, and only after the batch — too late to prevent the stale reference\n- Same issue exists in both the single-item path (line 3474) and the batch loop (line 3494)\n\n### Possible fix\n\n- Wrap `PyDict_Next()` + `Py_INCREF(key)` + `Py_INCREF(value)` in `Py_BEGIN_CRITICAL_SECTION(obj)` / `Py_END_CRITICAL_SECTION()` to prevent dict mutation while converting borrowed refs to owned refs\n- Keep `save()` calls outside the critical section since they can reenter Python\n- Same approach as the set iteration path at line 3644 which already uses `Py_BEGIN_CRITICAL_SECTION(obj)` with `_PySet_NextEntryRef`\n- Both `PyDict_Next` call sites in `batch_dict_exact` need this (line 3474 and line 3494)\n\n### CPython versions tested on\n\n3.14, CPython main branch\n\n### Operating systems tested on\n\nLinux\n\n\u003c!-- gh-linked-prs -->\n### Linked PRs\n* gh-146470\n* gh-149940\n* gh-150025\n* gh-150039\n* gh-150062\n* gh-150261\n* gh-150262\n* gh-150263\n* gh-150292\n\u003c!-- /gh-linked-prs -->\n","author":{"url":"https://github.com/overlorde","@type":"Person","name":"overlorde"},"datePublished":"2026-03-26T07:51:32Z","url":"https://github.com/python/cpython/issues/146452"}

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:588f30eb-79a9-add4-e859-e491a8d1d93e
current-catalog-service-hash81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114
request-idBCD0:ADA9D:39F4DA0:4A4DE55:6B01F35C
html-safe-noncebc21773f6808e203b9c4e9c2a360df98093e18cdc4ffcf6e9f407531eeaaa4bd
visitor-payloadeyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJCQ0QwOkFEQTlEOjM5RjREQTA6NEE0REU1NTo2QjAxRjM1QyIsInZpc2l0b3JfaWQiOiI2NDY4MTE4NDM0ODYwMzY0NjM2IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0=
visitor-hmac3109bd53cfad658d96c5905d4a89054a8f628b0d29e63f40a18b219034d0c964
hovercard-subject-tagissue:4141521889
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/146452/issue_layout
twitter:imagehttps://opengraph.githubassets.com/45c9b19e07d5e3b4822bfc1f102f5418956ce540ca32c9a2f3ec8bf3c9da17db/python/cpython/issues/146452
twitter:cardsummary_large_image
og:imagehttps://opengraph.githubassets.com/45c9b19e07d5e3b4822bfc1f102f5418956ce540ca32c9a2f3ec8bf3c9da17db/python/cpython/issues/146452
og:image:altBug report Bug description I've been testing the free-threaded build for thread safety issues in unaudited modules (ref #116738 — _pickle.c is unchecked). Found a segfault when pickle.dumps() runs ...
og:image:width1200
og:image:height600
og:site_nameGitHub
og:typeobject
og:author:usernameoverlorde
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/146452#start-of-content
https://github.com/
Sign inhttps://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fpython%2Fcpython%2Fissues%2F146452
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%2F146452
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/146452
Reloadhttps://github.com/python/cpython/issues/146452
Reloadhttps://github.com/python/cpython/issues/146452
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/146452
New issuehttps://github.com/login?return_to=https://github.com/python/cpython/issues/146452
Segfault in _pickle.c:batch_dict_exact when pickling dict with concurrent mutation (free-threading)https://github.com/python/cpython/issues/146452#top
extension-modulesC modules in the Modules dirhttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22extension-modules%22
topic-free-threadinghttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22topic-free-threading%22
type-crashA hard crash of the interpreter, possibly with a core dumphttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22type-crash%22
https://github.com/overlorde
overlordehttps://github.com/overlorde
on Mar 26, 2026https://github.com/python/cpython/issues/146452#issue-4141521889
#116738https://github.com/python/cpython/issues/116738
gh-146452: Fix pickle segfault on concurrent mutation of dict and list #146470https://github.com/python/cpython/pull/146470
[3.14] gh-146452: Fix pickle segfault on concurrent mutation of dict in pickle (GH-146470) #149940https://github.com/python/cpython/pull/149940
gh-146452: Improve locking granularity in pickle's batch_dict_exact #150025https://github.com/python/cpython/pull/150025
[3.15] gh-146452: Improve locking granularity in pickle's batch_dict_exact and fix race condition (GH-150025) #150039https://github.com/python/cpython/pull/150039
[3.14] gh-146452: Improve locking granularity in pickle's batch_dict_… #150062https://github.com/python/cpython/pull/150062
Revert "gh-146452: Improve locking granularity in pickle's batch_dict_exact and fix race condition (#150025)" #150261https://github.com/python/cpython/pull/150261
[3.15] Revert "[3.15] gh-146452: Improve locking granularity in pickle's batch_dict_exact and fix race condition (GH-150025) (#150039)" #150262https://github.com/python/cpython/pull/150262
[3.14] Revert "[3.14] gh-146452: Improve locking granularity in pickle's batch_dict_… (#150062)" #150263https://github.com/python/cpython/pull/150263
[3.15] gh-146452: Fix pickle segfault on concurrent mutation of dict in pickle (GH-146470) #150292https://github.com/python/cpython/pull/150292
picnixzhttps://github.com/picnixz
extension-modulesC modules in the Modules dirhttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22extension-modules%22
type-crashA hard crash of the interpreter, possibly with a core dumphttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22type-crash%22
topic-free-threadinghttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22topic-free-threading%22
on Mar 26, 2026https://github.com/python/cpython/issues/146452#event-23925916948
picnixzhttps://github.com/picnixz
Pickle and copy issues 🥒https://github.com/orgs/python/projects/9
on Mar 26, 2026https://github.com/python/cpython/issues/146452#event-23925962040
https://github.com/overlorde
overlordehttps://github.com/overlorde
on Mar 26, 2026https://github.com/python/cpython/issues/146452#issuecomment-4135203916
overlordehttps://github.com/overlorde
on Mar 26, 2026https://github.com/python/cpython/issues/146452#event-23937509871
pythongh-146452: Fix pickle segfault when pickling dict with concurre…https://github.com/overlorde/cpython/commit/ab4d60e5407d2a9918b67c259cc0bcdd5d00547b
pythongh-146452https://github.com/python/cpython/issues/146452
ab4d60ehttps://github.com/overlorde/cpython/commit/ab4d60e5407d2a9918b67c259cc0bcdd5d00547b
https://github.com/overlorde
overlordehttps://github.com/overlorde
on Mar 27, 2026https://github.com/python/cpython/issues/146452#issuecomment-4139602282
overlordehttps://github.com/overlorde
on Mar 27, 2026https://github.com/python/cpython/issues/146452#event-23984390815
pythongh-146452: Fix pickle segfault when pickling dict with concurre…https://github.com/overlorde/cpython/commit/8f55561513fd39b3e129abadfe2c095b921fd155
pythongh-146452https://github.com/python/cpython/issues/146452
8f55561https://github.com/overlorde/cpython/commit/8f55561513fd39b3e129abadfe2c095b921fd155
picnixzhttps://github.com/picnixz
Segfault in _pickle.c:batch_list_exact when pickling list with concurrent mutation (free-threading) #146533https://github.com/python/cpython/issues/146533
on Mar 27, 2026https://github.com/python/cpython/issues/146452#event-23988087794
https://github.com/mjbommar
mjbommarhttps://github.com/mjbommar
on Apr 16, 2026https://github.com/python/cpython/issues/146452#issuecomment-4263653760
#146470https://github.com/python/cpython/pull/146470
kumaraditya303https://github.com/kumaraditya303
on May 17, 2026https://github.com/python/cpython/issues/146452#event-25623373230
gh-146452: Fix pickle segfault on concurrent mutation of dict in pick…https://github.com/python/cpython/commit/e62a61177f8b793d787e337034a740ca75c1ab44
gh-146452https://github.com/python/cpython/issues/146452
e62a611https://github.com/python/cpython/commit/e62a61177f8b793d787e337034a740ca75c1ab44
[3.14] gh-146452: Fix pickle segfault on concurrent mutation of dict …https://github.com/python/cpython/commit/f380f85e6105180ab1c2e40afdbd14f2e12e082a
gh-146452https://github.com/python/cpython/issues/146452
f380f85https://github.com/python/cpython/commit/f380f85e6105180ab1c2e40afdbd14f2e12e082a
JelleZijlstrahttps://github.com/JelleZijlstra
on May 18, 2026https://github.com/python/cpython/issues/146452#event-25681585650
gh-146452: Improve locking granularity in pickle's batch_dict_exact a…https://github.com/python/cpython/commit/57a0e570d36f41b953a91bbaf4262a5d05d0391b
gh-146452https://github.com/python/cpython/issues/146452
57a0e57https://github.com/python/cpython/commit/57a0e570d36f41b953a91bbaf4262a5d05d0391b
[3.15] gh-146452: Improve locking granularity in pickle's batch_dict_…https://github.com/python/cpython/commit/66ade2861fec1d6c18998710938a1c71fde5f76b
gh-146452https://github.com/python/cpython/issues/146452
66ade28https://github.com/python/cpython/commit/66ade2861fec1d6c18998710938a1c71fde5f76b
[3.14] gh-146452: Improve locking granularity in pickle's batch_dict_… (https://github.com/python/cpython/commit/1243cd677d7f62b90c50546c5ff27e26ef24ca4f
gh-146452https://github.com/python/cpython/issues/146452
1243cd6https://github.com/python/cpython/commit/1243cd677d7f62b90c50546c5ff27e26ef24ca4f
vstinnerhttps://github.com/vstinner
on May 22, 2026https://github.com/python/cpython/issues/146452#event-25867965302
Revert "gh-146452: Improve locking granularity in pickle's batch_dict…https://github.com/python/cpython/commit/b770b2350e5c034afeee4213c9d63348f6f3b341
gh-146452https://github.com/python/cpython/issues/146452
b770b23https://github.com/python/cpython/commit/b770b2350e5c034afeee4213c9d63348f6f3b341
kumaraditya303https://github.com/kumaraditya303
on May 23, 2026https://github.com/python/cpython/issues/146452#event-25877936170
[3.15] Revert "[3.15] gh-146452: Improve locking granularity in pickl…https://github.com/python/cpython/commit/795dd3bd3500c49c6a08281a15a9472a28f416d3
gh-146452https://github.com/python/cpython/issues/146452
795dd3bhttps://github.com/python/cpython/commit/795dd3bd3500c49c6a08281a15a9472a28f416d3
[3.14] Revert "[3.14] gh-146452: Improve locking granularity in pickl…https://github.com/python/cpython/commit/0bd80a1d486592ae4af51e010616c8227d6b589d
gh-146452https://github.com/python/cpython/issues/146452
0bd80a1https://github.com/python/cpython/commit/0bd80a1d486592ae4af51e010616c8227d6b589d
[3.15] gh-146452: Fix pickle segfault on concurrent mutation of dict …https://github.com/python/cpython/commit/77cc4428a772b190ce10ec4d383594de09a03a45
gh-146452https://github.com/python/cpython/issues/146452
77cc442https://github.com/python/cpython/commit/77cc4428a772b190ce10ec4d383594de09a03a45
kumaraditya303https://github.com/kumaraditya303
completedhttps://github.com/python/cpython/issues?q=is%3Aissue%20state%3Aclosed%20archived%3Afalse%20reason%3Acompleted
on May 23, 2026https://github.com/python/cpython/issues/146452#event-25878615859
github-project-automationhttps://github.com/apps/github-project-automation
Pickle and copy issues 🥒https://github.com/orgs/python/projects/9
on May 23, 2026https://github.com/python/cpython/issues/146452
Sign up for freehttps://github.com/signup?return_to=https://github.com/python/cpython/issues/146452
Sign in to commenthttps://github.com/login?return_to=https://github.com/python/cpython/issues/146452
extension-modulesC modules in the Modules dirhttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22extension-modules%22
topic-free-threadinghttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22topic-free-threading%22
type-crashA hard crash of the interpreter, possibly with a core dumphttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22type-crash%22
Pickle and copy issues 🥒https://github.com/orgs/python/projects/9
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.