Title: Free-threaded CPython can starve a thread reattaching during repeated stop-the-world GC · Issue #151518 · python/cpython · GitHub
Open Graph Title: Free-threaded CPython can starve a thread reattaching during repeated stop-the-world GC · Issue #151518 · python/cpython
X Title: Free-threaded CPython can starve a thread reattaching during repeated stop-the-world GC · Issue #151518 · python/cpython
Description: Bug report While hardening free-threaded Python support in numba-cuda, we found a workload that could wedge when a stress worker repeatedly called gc.collect() in a tight loop while other threads were doing CUDA dispatch work. Removing o...
Open Graph Description: Bug report While hardening free-threaded Python support in numba-cuda, we found a workload that could wedge when a stress worker repeatedly called gc.collect() in a tight loop while other threads w...
X Description: Bug report While hardening free-threaded Python support in numba-cuda, we found a workload that could wedge when a stress worker repeatedly called gc.collect() in a tight loop while other threads w...
Opengraph URL: https://github.com/python/cpython/issues/151518
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Free-threaded CPython can starve a thread reattaching during repeated stop-the-world GC","articleBody":"# Bug report\n\nWhile hardening free-threaded Python support in `numba-cuda`, we found a workload that could wedge when a stress worker repeatedly called `gc.collect()` in a tight loop while other threads were doing CUDA dispatch work. Removing only the tight manual-GC worker made the workload pass.\n\nI reduced this to a no-third-party CPython reproducer. The reduced case is strongest on a debug free-threaded CPython `main` build, where the extra free-threaded GC validation work makes the stop-the-world window long enough to reproduce reliably.\n\n```python\nimport faulthandler\nimport gc\nimport importlib\nimport os\nimport sys\nimport threading\nimport time\n\nfaulthandler.dump_traceback_later(25, exit=True)\n\nmodules = (\n \"abc\", \"argparse\", \"collections\", \"contextlib\",\n \"decimal\", \"enum\", \"functools\", \"heapq\",\n \"importlib\", \"inspect\", \"itertools\", \"json\",\n \"math\", \"operator\", \"random\", \"re\",\n \"statistics\", \"threading\", \"types\", \"weakref\",\n)\nfor name in modules:\n importlib.import_module(name)\n\nprint(\"python\", sys.version, flush=True)\nprint(\"abiflags\", getattr(sys, \"abiflags\", \"\"), flush=True)\nprint(\"gil\", getattr(sys, \"_is_gil_enabled\", lambda: None)(), flush=True)\nprint(\"debug\", hasattr(sys, \"gettotalrefcount\"), flush=True)\nprint(\"pid\", os.getpid(), flush=True)\n\nstop = threading.Event()\ncount = 0\n\ndef collect():\n global count\n while not stop.is_set():\n gc.collect()\n count += 1\n\nthread = threading.Thread(target=collect, name=\"gc-collector\")\nstarted = time.monotonic()\nthread.start()\n\ntime.sleep(5.0)\nstop.set()\nthread.join(10.0)\n\nelapsed = time.monotonic() - started\nprint(f\"alive={thread.is_alive()} count={count} elapsed={elapsed:.6f}\",\n flush=True)\n\nraise SystemExit(1 if thread.is_alive() else 0)\n```\n\nBuild used for the reproducer:\n\n```bash\n./configure --with-pydebug --disable-gil --without-ensurepip\nmake -j\n```\n\nObserved on CPython `main` debug/free-threaded:\n\n```text\nPython 3.16.0a0 free-threading build\nabiflags td\nGIL disabled\ndebug True\n```\n\nI reproduced this on a B200 Linux system with 224 CPUs at the host level. I have not yet reproduced it on a smaller local workstation. The test process was running under a Slurm allocation that reported 28 CPUs available to the process and 224 CPUs on the system.\n\nThe script regularly times out via the faulthandler watchdog. The Python-level traceback shows the GC worker in `gc.collect()` and the main thread apparently still at `time.sleep()`. A native stack shows the more precise state:\n\n```text\ngc-collector:\n validate_refcounts() / validate_gc_objects()\n gc_visit_heaps()\n deduce_unreachable_heap()\n gc_collect_internal()\n _PyGC_Collect()\n gc.collect()\n\nmain thread:\n _PyParkingLot_Park()\n tstate_wait_attach()\n _PyThreadState_Attach()\n PyEval_RestoreThread()\n pysleep()\n time.sleep()\n```\n\nAt the time of the stall, GDB showed the interpreter stop-the-world state as:\n\n```text\nrequested = true\nworld_stopped = true\nthread_countdown = 0\nrequester = GC worker thread state\nmain thread state = _Py_THREAD_SUSPENDED\n```\n\nThe suspected issue is that after `start_the_world()` moves suspended threads back to detached and unparks them, a thread running a tight `gc.collect()` loop can immediately request another stop-the-world pause and re-suspend a just-unparked thread before that thread gets to attach. The result is starvation of a thread trying to return from a detached operation such as `time.sleep()`.\n\nThis was not reproduced in my minimized matrix on:\n\n- a non-debug free-threaded CPython `main` build\n- conda-forge Python 3.14.6t\n\nHowever, the original larger stress workload was first encountered while testing Python 3.14t free-threaded package support, and the reduced CPython `main` debug build gives a clean way to expose the progress bug.\n\nI have a local CPython patch that tracks thread states waiting to attach after a stop-the-world suspension and prevents a subsequent stop-the-world requester from immediately re-suspending those attach waiters. With that patch:\n\n- the minimized reproducer passes\n- the broader pure-Python GC stress matrix passes\n- `test_free_threading.test_gc` passes under debug and non-debug free-threaded builds\n- `test_free_threading.test_gc test_threading -v` passes under the debug free-threaded build\n\nI will prepare a PR with the fix and regression test once this issue exists.\n\nCPython versions tested on:\n\n- 3.14\n- 3.16\n- CPython main branch\n\nOperating systems tested on:\n\n- Linux\n\n\n\u003c!-- gh-linked-prs -->\n### Linked PRs\n* gh-152826\n* gh-158186\n* gh-158187\n\u003c!-- /gh-linked-prs -->\n","author":{"url":"https://github.com/tpn","@type":"Person","name":"tpn"},"datePublished":"2026-06-15T22:18:29Z","url":"https://github.com/python/cpython/issues/151518"}
| 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:057706fc-0d7b-ce23-7c74-2d474378ce19 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | A700:1CCECA:14CAF12:1BA7958:6B0207A6 |
| html-safe-nonce | 8ce6bd4e287b5233e7d1deb31db847d4b842920e56035dbb00d3859f89da2e77 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBNzAwOjFDQ0VDQToxNENBRjEyOjFCQTc5NTg6NkIwMjA3QTYiLCJ2aXNpdG9yX2lkIjoiMTc5ODA3NzE3NjgxODU2NzA3OCIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 893b57463c6743a00814e2e044efed91daf20277acb7d3b2f85ed4421b09f56c |
| hovercard-subject-tag | issue:4669336721 |
| 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/151518/issue_layout |
| twitter:image | https://opengraph.githubassets.com/ecd135dc44485abc193f89d7cda5a6e02ec24ffbb864b33e433c4a4e025afb19/python/cpython/issues/151518 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/ecd135dc44485abc193f89d7cda5a6e02ec24ffbb864b33e433c4a4e025afb19/python/cpython/issues/151518 |
| og:image:alt | Bug report While hardening free-threaded Python support in numba-cuda, we found a workload that could wedge when a stress worker repeatedly called gc.collect() in a tight loop while other threads w... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | tpn |
| 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