René's URL Explorer Experiment


Title: `functools.lru_cache` critical section locks `self` but `_LockHeld` dict APIs assert `self->cache` is locked · Issue #148180 · python/cpython · GitHub

Open Graph Title: `functools.lru_cache` critical section locks `self` but `_LockHeld` dict APIs assert `self->cache` is locked · Issue #148180 · python/cpython

X Title: `functools.lru_cache` critical section locks `self` but `_LockHeld` dict APIs assert `self->cache` is locked · Issue #148180 · python/cpython

Description: Crash report What happened? On a debug free-threaded build, functools.lru_cache crashes with a critical section assertion failure when gc.get_objects() is called between cache uses. bounded_lru_cache_wrapper (line 1498) acquires Py_BEGIN...

Open Graph Description: Crash report What happened? On a debug free-threaded build, functools.lru_cache crashes with a critical section assertion failure when gc.get_objects() is called between cache uses. bounded_lru_cac...

X Description: Crash report What happened? On a debug free-threaded build, functools.lru_cache crashes with a critical section assertion failure when gc.get_objects() is called between cache uses. bounded_lru_cac...

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

X: @github

direct link

Domain: github.com


Hey, it has json ld scripts:
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"`functools.lru_cache` critical section locks `self` but `_LockHeld` dict APIs assert `self->cache` is locked","articleBody":"# Crash report\n\n### What happened?\n\n\nOn a debug free-threaded build, `functools.lru_cache` crashes with a critical section assertion failure when `gc.get_objects()` is called between cache uses.\n\n`bounded_lru_cache_wrapper` (line 1498) acquires `Py_BEGIN_CRITICAL_SECTION(self)`, locking the lru_cache object's mutex. It then calls `bounded_lru_cache_get_lock_held` (line 1325) which calls `_PyDict_GetItemRef_KnownHash_LockHeld` on `self->cache`. The `_LockHeld` variant asserts that the **dict's** per-object mutex is held (`_Py_CRITICAL_SECTION_ASSERT_OBJECT_LOCKED(mp)` at `dictobject.c:1254`), but only `self`'s mutex is held — `self` and `self->cache` have different mutexes.\n\nIn 3.15 we have this abort message:\n```\nAssertion \"(cs != ((void*)0) && cs->_cs_mutex == mutex)\" failed: Critical section of object is not held\n```\n\nThis was introduced in gh-131757 (PR #131758), which split the critical section to allow the cached function to execute concurrently. The same pattern was extended in gh-132641 (PR #133787) which added more `_LockHeld` calls following the same assumption.\n\nOn non-debug builds the assertion is compiled out and the code happens to work in practice (no actual contention in the reproducer), but the lock contract is violated.\n\n### Reproducer\n\nRequires a debug free-threaded build (e.g., `--with-pydebug --disable-gil`).\n\n```python\nfrom functools import lru_cache\nimport gc\n\n@lru_cache(maxsize=32)\ndef get_pep(num):\n    return type('', (), {})()\n\nfor n in 8, 290, 308, 320, 8, 218, 320, 279, 289, 320, 9991:\n    pep = get_pep(n)\n    print(n, pep)\n\nobjs = gc.get_objects()\nprint(dir(get_pep))\n\nfor n in 8, 290, 308, 320, 8, 218, 320, 279, 289, 320, 9991:\n    pep = get_pep(n)\n    print(n, pep)\n```\n\n\nBacktrace on 3.14 (trimmed):\n```\npython: ./Include/internal/pycore_critical_section.h:226: void _PyCriticalSection_AssertHeld(PyMutex *): Assertion `cs != NULL && cs->_cs_mutex == mutex' failed.\n[...]\n#5  _PyCriticalSection_AssertHeld (mutex=...) at pycore_critical_section.h:226\n#6  _Py_dict_lookup (mp=...) at Objects/dictobject.c:1254\n#7  _PyDict_GetItemRef_KnownHash_LockHeld (...) at Objects/dictobject.c:2420\n#8  bounded_lru_cache_get_lock_held (...) at Modules/_functoolsmodule.c:1325\n#9  bounded_lru_cache_wrapper (...) at Modules/_functoolsmodule.c:1499\n```\n\nBacktrace on 3.15:\n```\n./Include/internal/pycore_critical_section.h:249: _PyCriticalSection_AssertHeldObj: Assertion \"(cs != ((void*)0) && cs->_cs_mutex == mutex)\" failed: Critical section of object is not held\nEnable tracemalloc to get the memory block allocation traceback\n\nobject address  : 0x7bffb670ccd0\nobject refcount : 2\nobject type     : 0x555556749840\nobject type name: dict\nobject repr     : {8: \u003cfunctools._lru_list_elem object at 0x7bffb65701d0>, 290: \u003cfunctools._lru_list_elem object at 0x7bffb6570240>, 308: \u003cfunctools._lru_list_elem object at 0x7bffb65702b0>, 320: \u003cfunctools._lru_list_elem object at 0x7bffb6570320>, 218: \u003cfunctools._lru_list_elem object at 0x7bffb6570390>, 279: \u003cfunctools._lru_list_elem object at 0x7bffb6570400>, 289: \u003cfunctools._lru_list_elem object at 0x7bffb6570470>, 9991: \u003cfunctools._lru_list_elem object at 0x7bffb65704e0>}\n\nFatal Python error: _PyObject_AssertFailed: _PyObject_AssertFailed\nPython runtime state: initialized\n\nStack (most recent call first):\n  File \"/home/danzin/crashers/yifei_01.py\", line 15 in \u003cmodule>\n\nProgram received signal SIGABRT, Aborted.\nDownload failed: Invalid argument.  Continuing without source file ./nptl/./nptl/pthread_kill.c.\n__pthread_kill_implementation (threadid=\u003coptimized out>, signo=6, no_tid=0) at ./nptl/pthread_kill.c:44\nwarning: 44     ./nptl/pthread_kill.c: No such file or directory\n(gdb) bt\n#0  __pthread_kill_implementation (threadid=\u003coptimized out>, signo=6, no_tid=0) at ./nptl/pthread_kill.c:44\n#1  __pthread_kill_internal (threadid=\u003coptimized out>, signo=6) at ./nptl/pthread_kill.c:89\n#2  __GI___pthread_kill (threadid=\u003coptimized out>, signo=signo@entry=6) at ./nptl/pthread_kill.c:100\n#3  0x00007ffff7c45e2e in __GI_raise (sig=sig@entry=6) at ../sysdeps/posix/raise.c:26\n#4  0x00007ffff7c28888 in __GI_abort () at ./stdlib/abort.c:77\n#5  0x000055555606dd54 in fatal_error_exit (status=status@entry=-1) at Python/pylifecycle.c:3389\n#6  0x000055555606da1b in fatal_error (fd=fd@entry=2, header=header@entry=1, prefix=prefix@entry=0x555556405360 \u003c__func__._PyObject_AssertFailed> \"_PyObject_AssertFailed\",\n    msg=msg@entry=0x555556405360 \u003c__func__._PyObject_AssertFailed> \"_PyObject_AssertFailed\", status=status@entry=-1) at Python/pylifecycle.c:3612\n#7  0x0000555556069730 in _Py_FatalErrorFunc (func=0x555556405360 \u003c__func__._PyObject_AssertFailed> \"_PyObject_AssertFailed\",\n    msg=0x555556405360 \u003c__func__._PyObject_AssertFailed> \"_PyObject_AssertFailed\") at Python/pylifecycle.c:3635\n#8  0x00005555557fc43b in _PyObject_AssertFailed (obj=obj@entry=0x7bffb670ccd0, expr=\u003coptimized out>, msg=\u003coptimized out>, file=\u003coptimized out>, line=line@entry=249,\n    function=\u003coptimized out>) at Objects/object.c:3259\n#9  0x0000555555bb7711 in _PyCriticalSection_AssertHeldObj (op=0x7bffb670ccd0) at ./Include/internal/pycore_critical_section.h:247\n#10 _Py_dict_lookup (mp=0x7bffb670ccd0, key=key@entry=0x55555693c4f8 \u003c_PyRuntime+30776>, hash=hash@entry=8, value_addr=value_addr@entry=0x7fffffff9d00) at Objects/dictobject.c:1305\n#11 0x0000555555bbbc61 in _PyDict_GetItemRef_KnownHash_LockHeld (op=0x196f39, key=0x196f39, key@entry=0x55555693c4f8 \u003c_PyRuntime+30776>, hash=6, hash@entry=8,\n    result=result@entry=0x7fffffff9e80) at Objects/dictobject.c:2480\n#12 0x00005555562ed221 in bounded_lru_cache_get_lock_held (self=0x7bffb6826c90, args=0x7bffb69bb290, kwds=0x0, result=\u003coptimized out>, key=\u003coptimized out>, hash=\u003coptimized out>)\n    at ./Modules/_functoolsmodule.c:1416\n#13 bounded_lru_cache_wrapper (self=0x7bffb6826c90, args=0x7bffb69bb290, kwds=0x0) at ./Modules/_functoolsmodule.c:1590\n#14 0x0000555555ac6b5d in _PyObject_MakeTpCall (tstate=0x555556997d60 \u003c_PyRuntime+405664>, callable=0x7bffb6826c90, args=0x7fffffffa048, nargs=1, keywords=0x0) at Objects/call.c:242\n#15 0x0000555555ea9e3b in _Py_VectorCallInstrumentation_StackRefSteal (callable=..., arguments=arguments@entry=0x7e8ff6fe0290, total_args=total_args@entry=1, kwnames=kwnames@entry=...,\n    call_instrumentation=false, frame=frame@entry=0x7e8ff6fe0220, this_instr=0x7bffb6405394, tstate=0x555556997d60 \u003c_PyRuntime+405664>) at Python/ceval.c:775\n#16 0x0000555555ee754d in _PyEval_EvalFrameDefault (tstate=\u003coptimized out>, frame=\u003coptimized out>, throwflag=\u003coptimized out>) at Python/generated_cases.c.h:1841\n#17 0x0000555555ea8d04 in _PyEval_EvalFrame (tstate=0x555556997d60 \u003c_PyRuntime+405664>, frame=0x7e8ff6fe0220, throwflag=0) at ./Include/internal/pycore_ceval.h:118\n#18 _PyEval_Vector (tstate=tstate@entry=0x555556997d60 \u003c_PyRuntime+405664>, func=func@entry=0x7bffb64c3e30, locals=locals@entry=0x7bffb670ca90, args=args@entry=0x0,\n    argcount=argcount@entry=0, kwnames=kwnames@entry=0x0) at Python/ceval.c:2179\n#19 0x0000555555ea80db in PyEval_EvalCode (co=co@entry=0x7bffb6405210, globals=globals@entry=0x7bffb670ca90, locals=locals@entry=0x7bffb670ca90) at Python/ceval.c:686\n#20 0x00005555560c6ca0 in run_eval_code_obj (tstate=tstate@entry=0x555556997d60 \u003c_PyRuntime+405664>, co=co@entry=0x7bffb6405210, globals=0x7bffb670ca90, locals=locals@entry=0x7bffb670ca90)\n    at Python/pythonrun.c:1369\n```\n\n\n### Possible fix\n\nEither lock both objects:\n```c\n// Line 1498, 1511: lock both self and self->cache\nPy_BEGIN_CRITICAL_SECTION2(self, self->cache);\n```\n\nOr use the regular (self-locking) dict APIs instead of `_LockHeld` variants for the get path.\n\nThe initial crash and reproducer were found by @zhuyifei1999. Root cause analysis and issue draft done with assistance from Claude Code.\n\n\n### CPython versions tested on:\n\n3.15, CPython main branch, 3.14\n\n### Operating systems tested on:\n\nLinux\n\n### Output from running 'python -VV' on the command line:\n\nPython 3.15.0a7+ free-threading build (heads/main:4d0e8ee649c, Mar 29 2026, 23:20:29) [Clang 21.1.2 (2ubuntu6)]","author":{"url":"https://github.com/devdanzin","@type":"Person","name":"devdanzin"},"datePublished":"2026-04-06T17:06:19Z","url":"https://github.com/python/cpython/issues/148180"}

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:44fec397-aeb7-890d-cd4c-0e4d5ef0b78d
current-catalog-service-hash81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114
request-idD7C6:20AA01:4B03896:5EF91BA:6B023F32
html-safe-nonce685c234f66fb984f1c361d65291eaeced0b100273c4611e6fb2b8358811c7f7a
visitor-payloadeyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJEN0M2OjIwQUEwMTo0QjAzODk2OjVFRjkxQkE6NkIwMjNGMzIiLCJ2aXNpdG9yX2lkIjoiNzgyODIyMjczNjU1NTE5NjIxMCIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9
visitor-hmac275d84ce03d7181f18b68b430f2cdbc6494661cae1dfd3efb185dcaa5c3790d1
hovercard-subject-tagissue:4212959178
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/148180/issue_layout
twitter:imagehttps://opengraph.githubassets.com/119d06991b2a99e711047d0bf71024386042b582b8b40376c5741ee28f71874c/python/cpython/issues/148180
twitter:cardsummary_large_image
og:imagehttps://opengraph.githubassets.com/119d06991b2a99e711047d0bf71024386042b582b8b40376c5741ee28f71874c/python/cpython/issues/148180
og:image:altCrash report What happened? On a debug free-threaded build, functools.lru_cache crashes with a critical section assertion failure when gc.get_objects() is called between cache uses. bounded_lru_cac...
og:image:width1200
og:image:height600
og:site_nameGitHub
og:typeobject
og:author:usernamedevdanzin
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/148180#start-of-content
https://github.com/
Sign inhttps://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fpython%2Fcpython%2Fissues%2F148180
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%2F148180
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/148180
Reloadhttps://github.com/python/cpython/issues/148180
Reloadhttps://github.com/python/cpython/issues/148180
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/148180
New issuehttps://github.com/login?return_to=https://github.com/python/cpython/issues/148180
functools.lru_cache critical section locks self but _LockHeld dict APIs assert self->cache is lockedhttps://github.com/python/cpython/issues/148180#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/devdanzin
devdanzinhttps://github.com/devdanzin
on Apr 6, 2026https://github.com/python/cpython/issues/148180#issue-4212959178
gh-131757https://github.com/python/cpython/issues/131757
#131758https://github.com/python/cpython/pull/131758
gh-132641https://github.com/python/cpython/issues/132641
#133787https://github.com/python/cpython/pull/133787
@zhuyifei1999https://github.com/zhuyifei1999
devdanzinhttps://github.com/devdanzin
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 Apr 6, 2026https://github.com/python/cpython/issues/148180#event-24230433286
https://github.com/colesbury
colesburyhttps://github.com/colesbury
on Apr 6, 2026https://github.com/python/cpython/issues/148180#issuecomment-4193814342
https://github.com/devdanzin
devdanzinhttps://github.com/devdanzin
on Apr 6, 2026https://github.com/python/cpython/issues/148180#issuecomment-4193859345
https://github.com/colesbury
colesburyhttps://github.com/colesbury
on Apr 6, 2026https://github.com/python/cpython/issues/148180#issuecomment-4193942838
https://github.com/zhuyifei1999
zhuyifei1999https://github.com/zhuyifei1999
on Apr 7, 2026https://github.com/python/cpython/issues/148180#issuecomment-4196807824
zhuyifei1999/guppy3#52 (comment)https://github.com/zhuyifei1999/guppy3/issues/52#issuecomment-4190484726
https://github.com/robertsdotpm
robertsdotpmhttps://github.com/robertsdotpm
on Jun 28, 2026https://github.com/python/cpython/issues/148180#issuecomment-4824321054
robertsdotpmhttps://github.com/robertsdotpm
on Jul 16, 2026https://github.com/python/cpython/issues/148180#event-28048955748
big_100: skip p303 lru_cache over-grow test (upstream CPython gh-148180)https://github.com/robertsdotpm/runloom/commit/190395704a1558955c7aded78128d1d197001c35
1903957https://github.com/robertsdotpm/runloom/commit/190395704a1558955c7aded78128d1d197001c35
https://github.com/kumaraditya303
kumaraditya303https://github.com/kumaraditya303
on Oct 22, 2026https://github.com/python/cpython/issues/148180#issuecomment-6337760062
kumaraditya303https://github.com/kumaraditya303
duplicatehttps://github.com/python/cpython/issues?q=is%3Aissue%20state%3Aclosed%20archived%3Afalse%20reason%3Aduplicate
Warn that gc.get_objects() can expose objects under construction #159730https://github.com/python/cpython/issues/159730
on Oct 22, 2026https://github.com/python/cpython/issues/148180#event-33935802848
Sign up for freehttps://github.com/signup?return_to=https://github.com/python/cpython/issues/148180
Sign in to commenthttps://github.com/login?return_to=https://github.com/python/cpython/issues/148180
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
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.