René's URL Explorer Experiment


Title: asyncio: TaskObj_dealloc leaves a refcount-0 Task GC-tracked, hanging or crashing the free-threaded build · Issue #153809 · python/cpython · GitHub

Open Graph Title: asyncio: TaskObj_dealloc leaves a refcount-0 Task GC-tracked, hanging or crashing the free-threaded build · Issue #153809 · python/cpython

X Title: asyncio: TaskObj_dealloc leaves a refcount-0 Task GC-tracked, hanging or crashing the free-threaded build · Issue #153809 · python/cpython

Description: Crash report What happened? On the free-threaded build, TaskObj_dealloc leaves an _asyncio.Task at refcount 0 while it is still GC-tracked, across a _PyEval_StopTheWorld(). A gc.collect() on another thread walks the tracked set during th...

Open Graph Description: Crash report What happened? On the free-threaded build, TaskObj_dealloc leaves an _asyncio.Task at refcount 0 while it is still GC-tracked, across a _PyEval_StopTheWorld(). A gc.collect() on anothe...

X Description: Crash report What happened? On the free-threaded build, TaskObj_dealloc leaves an _asyncio.Task at refcount 0 while it is still GC-tracked, across a _PyEval_StopTheWorld(). A gc.collect() on anothe...

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

X: @github

direct link

Domain: github.com


Hey, it has json ld scripts:
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"asyncio: TaskObj_dealloc leaves a refcount-0 Task GC-tracked, hanging or crashing the free-threaded build","articleBody":"# Crash report\n\n### What happened?\n\nOn the free-threaded build, `TaskObj_dealloc` leaves an `_asyncio.Task` at **refcount 0 while it is still GC-tracked**, across a `_PyEval_StopTheWorld()`. A `gc.collect()` on another thread walks the tracked set during that window and finds the refcount-0 object: the interpreter aborts on a debug build, and **hangs or segfaults on a release build**.\n\n## Root cause\n\n`Modules/_asynciomodule.c`, `TaskObj_dealloc`:\n\n```c\nstatic void\nTaskObj_dealloc(PyObject *self)\n{\n    if (PyObject_CallFinalizerFromDealloc(self) \u003c 0) {\n        return; // resurrected\n    }\n    // unregister the task after finalization so that\n    // if the task gets resurrected, it remains registered\n    unregister_task((TaskObj *)self);   // \u003c-- may _PyEval_StopTheWorld()\n\n    PyTypeObject *tp = Py_TYPE(self);\n    PyObject_GC_UnTrack(self);          // \u003c-- untrack happens only here\n    ...\n}\n```\n\nThe refcount is already 0 on entry, but the Task stays on the GC tracked list across both the finalizer and `unregister_task()`. For a Task deallocated on a thread other than its creator, `unregister_task()` takes a stop-the-world branch:\n\n```c\nstatic void\nunregister_task(TaskObj *task)\n{\n#ifdef Py_GIL_DISABLED\n    if (task->task_tid == _Py_ThreadId()) {\n        unregister_task_safe(task);\n    } else {\n        PyThreadState *tstate = _PyThreadState_GET();\n        _PyEval_StopTheWorld(tstate->interp);   // \u003c-- the window\n        unregister_task_safe(task);\n        _PyEval_StartTheWorld(tstate->interp);\n    }\n#else\n    unregister_task_safe(task);\n#endif\n}\n```\n\n`FutureObj_dealloc` has the same finalizer-before-untrack shape but untracks immediately afterwards, and does not reproduce — the stop-the-world inside `unregister_task()` is what widens the window enough to be hit reliably.\n\n## Reproducer\n\n```python\nimport sys, gc, threading, _asyncio\nassert not sys._is_gil_enabled()\n\nN, ITERS = 4, 6000\nbarrier = threading.Barrier(N)\n\ndef worker():\n    barrier.wait()\n    for i in range(ITERS):\n        try:\n            _asyncio.Task([1, 2, 3])   # fails the coro check before task_tid is set\n        except Exception:\n            pass\n        if i % 16 == 0:\n            gc.collect()\n\nts = [threading.Thread(target=worker) for _ in range(N)]\nfor t in ts: t.start()\nfor t in ts: t.join()\n```\n\n`Task([1, 2, 3])` fails `__init__`'s coroutine check *before* `task_tid` is assigned, so `task_tid` keeps its zero value and every transient Task takes the stop-the-world branch on teardown. A **well-formed** Task deallocated on a thread other than its creator takes the identical branch.\n\n## Observed\n\nFree-threaded CPython **3.16.0a0** (`main`, `bcf98ddbc40`):\n\n| build | result |\n|-------|--------|\n| `--disable-gil` debug | `validate_refcounts` abort, exit 134 (10/10) |\n| `--disable-gil` release | **interpreter wedges (5/5)**; segfault also observed |\n\nDebug:\n\n```\nPython/gc_free_threading.c:1083: validate_refcounts: Assertion\n  \"_Py_REFCNT(((PyObject*)((op)))) > 0\" failed:\n  tracked objects must have a reference count > 0\nobject type name: _asyncio.Task\nobject refcount : 0\nFatal Python error: _PyObject_AssertFailed\n```\n\nRelease (no `Py_DEBUG`, no sanitizer) — `gc.collect()` spins forever while every other thread parks behind it:\n\n```\nt1 (spinning, 100% CPU):\n  _mi_page_free_collect        Objects/mimalloc/page.c:245\n   \u003c- mi_heap_visit_blocks(visitor=update_refs)\n   \u003c- gc_visit_heaps_lock_held Python/gc_free_threading.c:395\n   \u003c- deduce_unreachable_heap  Python/gc_free_threading.c:1447\n   \u003c- gc_collect_main          Python/gc_free_threading.c:2257\n\nt0 / t2 / t3 (parked):\n  _PyMutex_Lock(&_PyRuntime+169592) \u003c- _PyParkingLot_Park \u003c- _PySemaphore_Wait\n```\n\nStill wedged after 150 s for work that an identical-shape control (a plain class whose `__init__` raises — same threads, same `gc.collect()` cadence, no stop-the-world in the dealloc) completes in 0.11 s. It reproduces with as few as **10** transient Tasks, and with only 2 threads.\n\n## Suggested fix\n\nMove `PyObject_GC_UnTrack(self)` to the top of `TaskObj_dealloc`, before the finalizer and `unregister_task()` — the standard dealloc discipline (`subtype_dealloc` untracks first). Resurrection re-tracks the object, so this remains compatible with gh-142556, whose fix introduced the current finalize → unregister → untrack ordering. An object at refcount 0 should not stay on the GC tracked list across a `_PyEval_StopTheWorld()`.\n\n## Related\n\n- **gh-142556** (fixed by #142565) — same function. Its fix established the current ordering and left `PyObject_GC_UnTrack` last, which is precisely the window exploited here. Distinct bug (single-threaded re-registration UAF).\n- **gh-128656** — a different assertion in the same area (`PyObject_CallFinalizerFromDealloc` called with a non-zero refcount).\n\nFull report, reproducer and complete release-build backtraces: https://gist.github.com/devdanzin/df90699fe37599d5502ce39f718f6657\n\n_AI Disclaimer: this report was drafted by Claude Code, which also created and ran the reproducer; the maintainer reviewed and edited it._\n\n\n\n### CPython versions tested on:\n\nCPython main branch\n\n### Operating systems tested on:\n\nLinux\n\n### Output from running 'python -VV' on the command line:\n\nPython 3.16.0a0 free-threading build (heads/main:bcf98ddbc40, Jul  4 2026, 15:37:00) [Clang 21.1.8 (6ubuntu1)]\n\n\u003c!-- gh-linked-prs -->\n### Linked PRs\n* gh-154563\n* gh-154665\n* gh-154666\n\u003c!-- /gh-linked-prs -->\n","author":{"url":"https://github.com/devdanzin","@type":"Person","name":"devdanzin"},"datePublished":"2026-07-16T10:25:51Z","url":"https://github.com/python/cpython/issues/153809"}

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:bef906c8-c7e5-151c-2ab8-0a4d12b98704
current-catalog-service-hash81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114
request-idAA14:CE438:12E4966:19357A6:6B0201EF
html-safe-noncec218b579aaffc0c7325385aae6f0ec503474290641ab0ade738b671b421cab09
visitor-payloadeyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBQTE0OkNFNDM4OjEyRTQ5NjY6MTkzNTdBNjo2QjAyMDFFRiIsInZpc2l0b3JfaWQiOiI4MTk2MjU3NTA5MTkxODQ0MzM1IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0=
visitor-hmac24f57033b9b3397429f9acdb3f8dcb2f8ccca70db8c979363e3e61c64cf7d761
hovercard-subject-tagissue:4901067121
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/153809/issue_layout
twitter:imagehttps://opengraph.githubassets.com/72214ee5d8e6dec3a2e8625dd4645fb73e87260cc53e1dcf47d0e8935606e356/python/cpython/issues/153809
twitter:cardsummary_large_image
og:imagehttps://opengraph.githubassets.com/72214ee5d8e6dec3a2e8625dd4645fb73e87260cc53e1dcf47d0e8935606e356/python/cpython/issues/153809
og:image:altCrash report What happened? On the free-threaded build, TaskObj_dealloc leaves an _asyncio.Task at refcount 0 while it is still GC-tracked, across a _PyEval_StopTheWorld(). A gc.collect() on anothe...
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-targetcanary-1
theme-color#1e2327
color-schemelight dark
react-app-nameissues

Links:

Skip to contenthttps://github.com/python/cpython/issues/153809#start-of-content
https://github.com/
Sign inhttps://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fpython%2Fcpython%2Fissues%2F153809
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%2F153809
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/153809
Reloadhttps://github.com/python/cpython/issues/153809
Reloadhttps://github.com/python/cpython/issues/153809
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/153809
New issuehttps://github.com/login?return_to=https://github.com/python/cpython/issues/153809
asyncio: TaskObj_dealloc leaves a refcount-0 Task GC-tracked, hanging or crashing the free-threaded buildhttps://github.com/python/cpython/issues/153809#top
https://github.com/sergey-miryanov
3.14bugs and security fixeshttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%223.14%22
3.15bugs and security fixeshttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%223.15%22
3.16new features, bugs and security fixeshttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%223.16%22
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 Jul 16, 2026https://github.com/python/cpython/issues/153809#issue-4901067121
gh-142556https://github.com/python/cpython/issues/142556
Use-after-free in asyncio Task deallocation via re-registering task in call_exception_handler #142556https://github.com/python/cpython/issues/142556
gh-142556: fix crash when a task gets re-registered during finalization #142565https://github.com/python/cpython/pull/142565
test_asyncio.test_free_threading.TestCFreeThreading.test_all_tasks_race crashes intermittently in free-threaded builds #128656https://github.com/python/cpython/issues/128656
https://gist.github.com/devdanzin/df90699fe37599d5502ce39f718f6657https://gist.github.com/devdanzin/df90699fe37599d5502ce39f718f6657
GH-153809: Untrack TaskObj from GC before calling unregister_task #154563https://github.com/python/cpython/pull/154563
[3.15] GH-153809: Untrack TaskObj from GC before calling unregister_task (GH-154563) #154665https://github.com/python/cpython/pull/154665
[3.14] GH-153809: Untrack TaskObj from GC before calling unregister_task (GH-154563) #154666https://github.com/python/cpython/pull/154666
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 Jul 16, 2026https://github.com/python/cpython/issues/153809#event-28063812456
https://github.com/sergey-miryanov
sergey-miryanovhttps://github.com/sergey-miryanov
on Jul 16, 2026https://github.com/python/cpython/issues/153809#issuecomment-4990952186
sergey-miryanovhttps://github.com/sergey-miryanov
on Jul 16, 2026https://github.com/python/cpython/issues/153809#event-28064633493
devdanzinhttps://github.com/devdanzin
on Jul 17, 2026https://github.com/python/cpython/issues/153809#event-28130720944
Fleet 04: TSAN-0035/0036 (+0034), widen TSAN-0018, fix the ingest-glo…https://github.com/devdanzin/cpython-tsan-findings/commit/43bb1bf5efb995513724ab632d4af1363d4d2518
43bb1bfhttps://github.com/devdanzin/cpython-tsan-findings/commit/43bb1bf5efb995513724ab632d4af1363d4d2518
sergey-miryanovhttps://github.com/sergey-miryanov
3.14bugs and security fixeshttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%223.14%22
3.15bugs and security fixeshttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%223.15%22
3.16new features, bugs and security fixeshttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%223.16%22
on Jul 24, 2026https://github.com/python/cpython/issues/153809#event-28456801997
kumaraditya303https://github.com/kumaraditya303
on Jul 25, 2026https://github.com/python/cpython/issues/153809#event-28465999471
GH-153809: Untrack TaskObj from GC before calling unregister_task (#1…https://github.com/python/cpython/commit/506242706e43af85f841027b7bd7f022f8acd939
GH-153809https://github.com/python/cpython/issues/153809
#1…https://github.com/python/cpython/pull/154563
5062427https://github.com/python/cpython/commit/506242706e43af85f841027b7bd7f022f8acd939
kumaraditya303https://github.com/kumaraditya303
completedhttps://github.com/python/cpython/issues?q=is%3Aissue%20state%3Aclosed%20archived%3Afalse%20reason%3Acompleted
on Jul 25, 2026https://github.com/python/cpython/issues/153809#event-28466010472
kumaraditya303https://github.com/kumaraditya303
on Jul 25, 2026https://github.com/python/cpython/issues/153809#event-28466680716
[3.14] GH-153809: Untrack TaskObj from GC before calling unregister_t…https://github.com/python/cpython/commit/e76d3333cbfa82e32aa4f8cf0b52d2664e90eb44
GH-153809https://github.com/python/cpython/issues/153809
e76d333https://github.com/python/cpython/commit/e76d3333cbfa82e32aa4f8cf0b52d2664e90eb44
[3.15] GH-153809: Untrack TaskObj from GC before calling unregister_t…https://github.com/python/cpython/commit/8f99c7a2e88774546d803c90522605a348d8fd46
GH-153809https://github.com/python/cpython/issues/153809
8f99c7ahttps://github.com/python/cpython/commit/8f99c7a2e88774546d803c90522605a348d8fd46
mbeijenhttps://github.com/mbeijen
on Aug 14, 2026https://github.com/python/cpython/issues/153809#event-29455489886
pythonGH-153809: Untrack TaskObj from GC before calling unregister_ta…https://github.com/mbeijen/cpython/commit/ce533b1b94f457e5090bc8af6270d74aa6120685
pythonGH-153809https://github.com/python/cpython/issues/153809
ce533b1https://github.com/mbeijen/cpython/commit/ce533b1b94f457e5090bc8af6270d74aa6120685
Sign up for freehttps://github.com/signup?return_to=https://github.com/python/cpython/issues/153809
Sign in to commenthttps://github.com/login?return_to=https://github.com/python/cpython/issues/153809
sergey-miryanovhttps://github.com/sergey-miryanov
3.14bugs and security fixeshttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%223.14%22
3.15bugs and security fixeshttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%223.15%22
3.16new features, bugs and security fixeshttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%223.16%22
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.