Title: AsyncIO thread-safety issue in Python 3.14's free-threading · Issue #159449 · python/cpython · GitHub
Open Graph Title: AsyncIO thread-safety issue in Python 3.14's free-threading · Issue #159449 · python/cpython
X Title: AsyncIO thread-safety issue in Python 3.14's free-threading · Issue #159449 · python/cpython
Description: Bug report When a short-lived thread exits while some of its async tasks are still unfinished, Python moves those leftover tasks onto a shared global list; however, because Linux reuses the OS thread IDs of exited threads for newly creat...
Open Graph Description: Bug report When a short-lived thread exits while some of its async tasks are still unfinished, Python moves those leftover tasks onto a shared global list; however, because Linux reuses the OS thre...
X Description: Bug report When a short-lived thread exits while some of its async tasks are still unfinished, Python moves those leftover tasks onto a shared global list; however, because Linux reuses the OS thre...
Opengraph URL: https://github.com/python/cpython/issues/159449
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"AsyncIO thread-safety issue in Python 3.14's free-threading","articleBody":"### Bug report\n\n When a short-lived thread exits while some of its async tasks are still unfinished, Python moves those leftover tasks onto a shared global list; however, because Linux reuses the OS thread IDs of exited threads for newly created threads, a later thread can be mistaken for the original creator of those tasks and remove them from the shared list without locking, which corrupts the list and causes the next thread cleaning up after an async run to segfault.\n\nFound and fixed by a colleague.\n\nReproducer:\n\n```\n\"\"\"Reproduces a free-threaded CPython 3.14 SIGSEGV in `_asyncio_all_tasks`.\n\nSpawner threads repeatedly start a short-lived worker thread that cancels the\nasyncio Tasks left behind by its predecessor, creates a new batch of pending\nTasks, and exits. When a thread exits with pending Tasks, PyThreadState_Clear()\nmoves them onto the interpreter-wide `interp->asyncio_tasks_head` list. glibc\nhands the exited thread's pthread_t to the next thread, so the successor has the\nsame `_Py_ThreadId()` as the creator of those Tasks. unregister_task() then\ntakes\nits lock-free fast path and unlinks them from the shared list without stopping\nthe world, and concurrent unlinks corrupt the list.\n\nRunner threads call asyncio.run(). Its shutdown calls asyncio.all_tasks() after\nthe loop stopped running, which takes the stop-the-world slow path in\n_asyncio_all_tasks() and walks the corrupted list until it dereferences NULL.\n\nIf the runtime is not affected, the binary runs for --duration_secs and exits 0.\n\"\"\"\n\nimport asyncio\nimport collections\nfrom collections.abc import Sequence\nimport sys\nimport sysconfig\nimport threading\nfrom typing import Any\n\nfrom absl import app\nfrom absl import flags\nfrom absl import logging\n\n_DURATION_SECS = flags.DEFINE_float(\n \"duration_secs\", 30.0, \"How long to run before declaring a pass.\"\n)\n_SPAWNER_THREADS = flags.DEFINE_integer(\n \"spawner_threads\", 6, \"Threads that churn short-lived Task-owning threads.\"\n)\n_RUNNER_THREADS = flags.DEFINE_integer(\n \"runner_threads\", 2, \"Threads that call asyncio.run() in a loop.\"\n)\n_TASKS_PER_THREAD = flags.DEFINE_integer(\n \"tasks_per_thread\", 32, \"Pending Tasks each short-lived thread leaves.\"\n)\n\n_TaskBatch = list[asyncio.Task[Any]]\n\n\nclass _InertLoop:\n \"\"\"Minimal event loop stand-in for constructing `asyncio.Task` objects.\n\n `call_soon` drops the Task's first step, so a Task stays pending without a\n real event loop holding a reference to it or opening file descriptors.\n \"\"\"\n\n def get_debug(self) -> bool:\n return False\n\n def call_soon(self, *args: object, **kwargs: object) -> None:\n del args, kwargs # Unused.\n\n\n_INERT_LOOP = _InertLoop()\n\n# Keeps recently cancelled Tasks alive so their memory is not reused while a\n# corrupted list may still point at them. This turns a nondeterministic\n# use-after-free into the deterministic NULL dereference seen in production.\n_RECENTLY_CANCELLED: collections.deque[_TaskBatch] = collections.deque(\n maxlen=4096\n)\n\n\nasync def _noop() -> None:\n pass\n\n\ndef _create_pending_tasks(count: int) -> _TaskBatch:\n \"\"\"Creates `count` Tasks that stay pending until they are cancelled.\"\"\"\n tasks = []\n for _ in range(count):\n coro = _noop()\n task = asyncio.Task(coro, loop=_INERT_LOOP) # type: ignore[arg-type]\n # The Task never runs: close its coroutine to avoid \"never awaited\", and do\n # not warn about Tasks that are still pending when the process exits.\n coro.close()\n setattr(task, \"_log_destroy_pending\", False)\n tasks.append(task)\n return tasks\n\n\ndef _short_lived_worker(\n handoff: list[_TaskBatch], tasks_per_thread: int\n) -> None:\n \"\"\"Cancels the predecessor's Tasks, then exits leaving new pending Tasks.\"\"\"\n while handoff:\n tasks = handoff.pop()\n for task in tasks:\n # Future.cancel() completes the Task immediately, which unregisters it\n # from the asyncio task list (Task.cancel() would only request it).\n asyncio.Future.cancel(task)\n _RECENTLY_CANCELLED.append(tasks)\n handoff.append(_create_pending_tasks(tasks_per_thread))\n\n\ndef _spawner_loop(stop: threading.Event, tasks_per_thread: int) -> None:\n \"\"\"Starts and joins short-lived worker threads until `stop` is set.\"\"\"\n handoff: list[_TaskBatch] = []\n while not stop.is_set():\n worker = threading.Thread(\n target=_short_lived_worker, args=(handoff, tasks_per_thread)\n )\n worker.start()\n worker.join()\n\n\ndef _runner_loop(stop: threading.Event) -> None:\n \"\"\"Runs short asyncio.run() calls until `stop` is set.\"\"\"\n while not stop.is_set():\n asyncio.run(_noop())\n\n\ndef main(argv: Sequence[str]) -> None:\n if len(argv) > 1:\n raise app.UsageError(\"Too many command-line arguments.\")\n if not sysconfig.get_config_var(\"Py_GIL_DISABLED\"):\n raise app.UsageError(\"Needs a free-threaded Python build; see README.md.\")\n if sys._is_gil_enabled(): # pylint: disable=protected-access\n raise app.UsageError(\"The GIL is enabled; run with PYTHON_GIL=0.\")\n logging.info(\"Running on Python %s with the GIL disabled.\", sys.version)\n\n stop = threading.Event()\n threads = [\n threading.Thread(\n target=_spawner_loop, args=(stop, _TASKS_PER_THREAD.value)\n )\n for _ in range(_SPAWNER_THREADS.value)\n ]\n threads.extend(\n threading.Thread(target=_runner_loop, args=(stop,))\n for _ in range(_RUNNER_THREADS.value)\n )\n for thread in threads:\n thread.start()\n stop.wait(_DURATION_SECS.value)\n stop.set()\n for thread in threads:\n thread.join()\n logging.info(\"PASS: no crash after %.1f seconds.\", _DURATION_SECS.value)\n\n\nif __name__ == \"__main__\":\n app.run(main)\n```\n\nI am sending a PR separately.\n\n### CPython versions tested on:\n\n3.14\n\n### Operating systems tested on:\n\nLinux\n\n\u003c!-- gh-linked-prs -->\n### Linked PRs\n* gh-159450\n* gh-159451\n* gh-159483\n* gh-161379\n* gh-161380\n\u003c!-- /gh-linked-prs -->\n","author":{"url":"https://github.com/jblespiau","@type":"Person","name":"jblespiau"},"datePublished":"2026-10-15T08:30:54Z","url":"https://github.com/python/cpython/issues/159449"}
| 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:b05f5e19-5180-9f5d-3e28-f328532db690 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | C8F2:2F9718:2E983C7:3E80C9D:6B01EC1A |
| html-safe-nonce | bafcacc1c12eafcbf5ba04ae6a33eb42ef1d88170435dc8fcd04cad1dc18fe0e |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJDOEYyOjJGOTcxODoyRTk4M0M3OjNFODBDOUQ6NkIwMUVDMUEiLCJ2aXNpdG9yX2lkIjoiMzI1MDMxOTAwNjczMjc3NDQyNiIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 868c41b905d5a7adbd97a3f2822fb7164c9dcd4ccbe056aacc5a6c8daf991796 |
| hovercard-subject-tag | issue:5862800532 |
| 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/159449/issue_layout |
| twitter:image | https://opengraph.githubassets.com/1c7c0b9b1b3d06259f965953795213346fe3e4fe47e1cd397dbe00f06edce54c/python/cpython/issues/159449 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/1c7c0b9b1b3d06259f965953795213346fe3e4fe47e1cd397dbe00f06edce54c/python/cpython/issues/159449 |
| og:image:alt | Bug report When a short-lived thread exits while some of its async tasks are still unfinished, Python moves those leftover tasks onto a shared global list; however, because Linux reuses the OS thre... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | jblespiau |
| 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