Title: `asyncio` `Task` created from a finalizer during `PyThreadState_Clear()` registers into memory that is about to be freed (3.14 regression) · Issue #159041 · python/cpython · GitHub
Open Graph Title: `asyncio` `Task` created from a finalizer during `PyThreadState_Clear()` registers into memory that is about to be freed (3.14 regression) · Issue #159041 · python/cpython
X Title: `asyncio` `Task` created from a finalizer during `PyThreadState_Clear()` registers into memory that is about to be freed (3.14 regression) · Issue #159041 · python/cpython
Description: Bug report PyThreadState_Clear() merges the exiting thread's asyncio task list into the interpreter's list, then, later in the same function, clears tstate->context, tstate->dict, tstate->current_exception and a few other fields that can...
Open Graph Description: Bug report PyThreadState_Clear() merges the exiting thread's asyncio task list into the interpreter's list, then, later in the same function, clears tstate->context, tstate->dict, tstate->current_e...
X Description: Bug report PyThreadState_Clear() merges the exiting thread's asyncio task list into the interpreter's list, then, later in the same function, clears tstate->context, tstate->dict, tst...
Opengraph URL: https://github.com/python/cpython/issues/159041
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"`asyncio` `Task` created from a finalizer during `PyThreadState_Clear()` registers into memory that is about to be freed (3.14 regression)","articleBody":"### Bug report\n\n`PyThreadState_Clear()` merges the exiting thread's asyncio task list into the interpreter's list, then, later in the same function, clears `tstate->context`, `tstate->dict`, `tstate->current_exception` and a few other fields that can run arbitrary Python code through a finalizer:\n\nhttps://github.com/python/cpython/blob/c63aec69bd59c55314c06c23f4c22c03de76fe45/Python/pystate.c#L1783-L1818\n\nIf one of those finalizers creates a new `asyncio.Task`, `register_task()` inserts it into `_PyThreadState_GET()`'s own task list:\n\nhttps://github.com/python/cpython/blob/c63aec69bd59c55314c06c23f4c22c03de76fe45/Modules/_asynciomodule.c#L2199-L2209\n\n`_PyThreadState_GET()` still returns the thread calling `PyThreadState_Clear()` on itself, now past the merge point. The Task is otherwise ordinary: nothing is wrong with it, and the event loop that owns it runs it to completion normally, on another thread. The thread that creates the Task is not the loop's thread, but that is not the bug: `register_task()` runs synchronously inside `Task.__init__` and always reads `_PyThreadState_GET()`. A loop on another thread is needed only so the Task outlives the thread and is finished there. Finishing it calls `unregister_task()`, which removes it from the list it was registered on:\n\nhttps://github.com/python/cpython/blob/c63aec69bd59c55314c06c23f4c22c03de76fe45/Modules/_asynciomodule.c#L2211-L2220\n\nBy then the thread that registered it has exited and its `PyThreadState` has been freed. The list the Task's `task_node` points into is embedded in that freed struct. `llist_remove()` writes into it.\n\nIn the reproducer, a background thread runs an event loop. Short-lived threads each store, in a `contextvars.ContextVar`, an object whose `__del__` creates a task on that loop:\n\n```python\nimport _thread\nimport asyncio\nimport contextvars\nimport sys\nimport threading\nimport time\n\nloop = asyncio.new_event_loop()\nthreading.Thread(target=loop.run_forever, daemon=True).start()\nvar = contextvars.ContextVar(\"var\")\n\n\nasync def noop():\n pass\n\n\nclass CreatesTaskOnClear:\n def __del__(self):\n # Runs inside PyThreadState_Clear(), on the thread being cleared.\n loop.create_task(noop())\n loop.call_soon_threadsafe(lambda: None) # wake the loop to run it\n\n\ndef body():\n var.set(CreatesTaskOnClear()) # dropped by Py_CLEAR(tstate->context)\n\n\nn = int(sys.argv[1]) if len(sys.argv) > 1 else 50\nfor _ in range(n):\n _thread.start_new_thread(body, ())\n time.sleep(0.01) # several threads overlap in mid-teardown at once\ntime.sleep(1)\nprint(f\"{sys.version.split()[0]}: ran {n} threads, \"\n f\"{len(asyncio.all_tasks(loop))} tasks still tracked\", flush=True)\n```\n\nThreads started and joined one at a time, with nothing else running, did not reproduce this for me. Several threads overlapping in their last moments, as under real load, did, reliably. Under AddressSanitizer the finalizer-created task triggers a heap-use-after-free in `llist_remove`: 6 of 6 runs on 3.14.6 and 10 of 10 on 3.15.0rc3. The identical script on 3.13.16 under the same sanitizer ran clean, 0 of 10, because 3.13 still tracks tasks in a Python-level `weakref.WeakSet`, not a per-thread C list, so this ordering does not occur. (3.12.13 is the same WeakSet design and did not reproduce either.)\n\nAddressSanitizer trace (3.14.6 built `--with-address-sanitizer --without-pymalloc --with-assertions`, frames inside glibc/ASan runtime trimmed):\n\n```\nERROR: AddressSanitizer: heap-use-after-free ... WRITE of size 8 ... thread T1\n #0 llist_remove Include/internal/pycore_llist.h:83\n #1 unregister_task_safe Modules/_asynciomodule.c:2219\n #2 unregister_task Modules/_asynciomodule.c:2240\n #3 future_schedule_callbacks Modules/_asynciomodule.c:440\n #4 future_set_result Modules/_asynciomodule.c:653\n #5 task_step_impl Modules/_asynciomodule.c:3144\n #6 task_step Modules/_asynciomodule.c:3460\n #7 TaskStepMethWrapper_call Modules/_asynciomodule.c:2121\n\nfreed by another thread here:\n #2 free_threadstate Python/pystate.c:1519\n #3 _PyThreadState_DeleteCurrent Python/pystate.c:1963\n #4 thread_run Modules/_threadmodule.c:377\n\npreviously allocated by thread T0 here:\n #3 alloc_threadstate Python/pystate.c:1497\n #4 new_threadstate Python/pystate.c:1624\n #5 _PyThreadState_New Python/pystate.c:1690\n #6 ThreadHandle_start Modules/_threadmodule.c:428\n```\n\nA stock (non-sanitized) build did not visibly crash from this for me, up to 1000 threads in one run. That is consistent with a narrow corruption of a few freed bytes that does not always get reinterpreted as a bad pointer soon enough to show as a fault.\n\nNo fork is involved: this fires on ordinary thread exit alone. Any threaded process that creates asyncio tasks from `contextvars`-held (or `threading.local()`-held, or thread-dict-held) finalizers is exposed.\n\nI looked for an existing report of this before filing. The closest issues are a different mechanism in the same function family: #142556 (re-registration inside `call_exception_handler` during a Task's own dealloc, fixed) and #153809 (a free-threaded-only GC-tracking race in the same dealloc path, fixed). Neither is this ordering hazard between the per-thread task-list merge and the later clearing of `context`/`dict`/`current_exception` in `PyThreadState_Clear()`.\n\nThis could have been introduced in https://github.com/python/cpython/pull/128869.\n\n### Potential fix\n\nEither clear `tstate->context` (and the other fields that can run a finalizer) before the task-list merge, so a task created by one of them is still merged along with the rest, or re-check and re-merge the thread's task list after those clears, in case a finalizer added to it. I have not attempted a patch; I don't know this code well enough to judge GIL-disabled-build implications.\n\n---\n\nI used Opus 5.5 for assistance with investigation and the writeup.\n\n### CPython versions tested on:\n\n3.12, 3.14, 3.13, 3.15\n\n### Operating systems tested on:\n\nLinux\n\n\u003c!-- gh-linked-prs -->\n### Linked PRs\n* gh-159073\n* gh-159319\n* gh-159694\n* gh-159695\n\u003c!-- /gh-linked-prs -->\n","author":{"url":"https://github.com/krassowski","@type":"Person","name":"krassowski"},"datePublished":"2026-10-08T21:16:10Z","url":"https://github.com/python/cpython/issues/159041"}
| react-profiling | 0 |
| react-import-map | react,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-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:e34dc63e-8994-cc9b-c214-377de2fefb86 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | D4EC:2AF0D8:37BC6D:457B3D:6B01FA24 |
| html-safe-nonce | 2baf25c73a25cf6660969b895136b7a16fd20283a7067012338381796480ed14 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJENEVDOjJBRjBEODozN0JDNkQ6NDU3QjNEOjZCMDFGQTI0IiwidmlzaXRvcl9pZCI6IjUxMDkzMTA5NTEyOTU5NDExNTYiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 3141ad5358309214a706068d9973b515d5618e36a6c901fbededdaa95edba627 |
| hovercard-subject-tag | issue:5769023542 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/python/cpython/159041/issue_layout |
| twitter:image | https://opengraph.githubassets.com/53d6dcfbc81ce02f3ddaa867031523b971f8b8ae9b70a7eb82f7a5c7b823dc96/python/cpython/issues/159041 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/53d6dcfbc81ce02f3ddaa867031523b971f8b8ae9b70a7eb82f7a5c7b823dc96/python/cpython/issues/159041 |
| og:image:alt | Bug report PyThreadState_Clear() merges the exiting thread's asyncio task list into the interpreter's list, then, later in the same function, clears tstate->context, tstate->dict, tstate->current_e... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | krassowski |
| hostname | github.com |
| expected-hostname | github.com |
| None | 87e774ee7c6b3bee0490394e4d3e57ec9dbc956f74b87ea685dc8996c1676456 |
| turbo-cache-control | no-preview |
| go-import | github.com/python/cpython git https://github.com/python/cpython.git |
| octolytics-dimension-user_id | 1525981 |
| octolytics-dimension-user_login | python |
| octolytics-dimension-repository_id | 81598961 |
| octolytics-dimension-repository_nwo | python/cpython |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 81598961 |
| octolytics-dimension-repository_network_root_nwo | python/cpython |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 423dbb54c5e7bfecc838eb387c1435eb3d0e760d |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width