René's URL Explorer Experiment


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

direct link

Domain: github.com


Hey, it has json ld scripts:
{"@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-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:057706fc-0d7b-ce23-7c74-2d474378ce19
current-catalog-service-hash81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114
request-idA700:1CCECA:14CAF12:1BA7958:6B0207A6
html-safe-nonce8ce6bd4e287b5233e7d1deb31db847d4b842920e56035dbb00d3859f89da2e77
visitor-payloadeyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBNzAwOjFDQ0VDQToxNENBRjEyOjFCQTc5NTg6NkIwMjA3QTYiLCJ2aXNpdG9yX2lkIjoiMTc5ODA3NzE3NjgxODU2NzA3OCIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9
visitor-hmac893b57463c6743a00814e2e044efed91daf20277acb7d3b2f85ed4421b09f56c
hovercard-subject-tagissue:4669336721
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/151518/issue_layout
twitter:imagehttps://opengraph.githubassets.com/ecd135dc44485abc193f89d7cda5a6e02ec24ffbb864b33e433c4a4e025afb19/python/cpython/issues/151518
twitter:cardsummary_large_image
og:imagehttps://opengraph.githubassets.com/ecd135dc44485abc193f89d7cda5a6e02ec24ffbb864b33e433c4a4e025afb19/python/cpython/issues/151518
og:image:altBug 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:width1200
og:image:height600
og:site_nameGitHub
og:typeobject
og:author:usernametpn
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/151518#start-of-content
https://github.com/
Sign inhttps://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fpython%2Fcpython%2Fissues%2F151518
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%2F151518
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/151518
Reloadhttps://github.com/python/cpython/issues/151518
Reloadhttps://github.com/python/cpython/issues/151518
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/151518
New issuehttps://github.com/login?return_to=https://github.com/python/cpython/issues/151518
#152826https://github.com/python/cpython/pull/152826
Free-threaded CPython can starve a thread reattaching during repeated stop-the-world GChttps://github.com/python/cpython/issues/151518#top
#152826https://github.com/python/cpython/pull/152826
interpreter-core(Objects, Python, Grammar, and Parser dirs)https://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22interpreter-core%22
topic-free-threadinghttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22topic-free-threading%22
type-bugAn unexpected behavior, bug, or errorhttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22type-bug%22
https://github.com/tpn
tpnhttps://github.com/tpn
on Jun 15, 2026https://github.com/python/cpython/issues/151518#issue-4669336721
gh-151518: Avoid STW starvation of attaching threads #152826https://github.com/python/cpython/pull/152826
[3.15] gh-151518: Avoid STW starvation of attaching threads (GH-152826) #158186https://github.com/python/cpython/pull/158186
[3.14] gh-151518: Avoid STW starvation of attaching threads (GH-152826) #158187https://github.com/python/cpython/pull/158187
tpnhttps://github.com/tpn
type-bugAn unexpected behavior, bug, or errorhttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22type-bug%22
on Jun 15, 2026https://github.com/python/cpython/issues/151518#event-26781329596
picnixzhttps://github.com/picnixz
interpreter-core(Objects, Python, Grammar, and Parser dirs)https://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22interpreter-core%22
topic-free-threadinghttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22topic-free-threading%22
on Jun 15, 2026https://github.com/python/cpython/issues/151518#event-26781713668
https://github.com/zangjiucheng
zangjiuchenghttps://github.com/zangjiucheng
on Jun 22, 2026https://github.com/python/cpython/issues/151518#issuecomment-4772060274
https://github.com/tpn
tpnhttps://github.com/tpn
on Jul 1, 2026https://github.com/python/cpython/issues/151518#issuecomment-4860299842
#152826https://github.com/python/cpython/pull/152826
https://github.com/nascheme
naschemehttps://github.com/nascheme
on Jul 3, 2026https://github.com/python/cpython/issues/151518#issuecomment-4878947140
@colesburyhttps://github.com/colesbury
tpnhttps://github.com/tpn
on Sep 20, 2026https://github.com/python/cpython/issues/151518#event-31470653448
pythongh-151518: Avoid STW starvation of attaching threadshttps://github.com/tpn/cpython-fork/commit/b3b504c290738db92044abfac3b812dce39ba610
pythongh-151518https://github.com/python/cpython/issues/151518
b3b504chttps://github.com/tpn/cpython-fork/commit/b3b504c290738db92044abfac3b812dce39ba610
pythongh-151518: Track attach waiters in the thread statehttps://github.com/tpn/cpython-fork/commit/26c9cb00aebc2a736046cf6ca03a5b32769d13ab
pythongh-151518https://github.com/python/cpython/issues/151518
26c9cb0https://github.com/tpn/cpython-fork/commit/26c9cb00aebc2a736046cf6ca03a5b32769d13ab
pythongh-151518: Simplify the GC fairness regressionhttps://github.com/tpn/cpython-fork/commit/3a368a683f4deca2c93e71af6aa45693ac23896b
pythongh-151518https://github.com/python/cpython/issues/151518
3a368a6https://github.com/tpn/cpython-fork/commit/3a368a683f4deca2c93e71af6aa45693ac23896b
pythongh-151518: Exercise STW fairness directlyhttps://github.com/tpn/cpython-fork/commit/bc207193deb41d44d86a8f69f24c63dbfb4aac61
pythongh-151518https://github.com/python/cpython/issues/151518
bc20719https://github.com/tpn/cpython-fork/commit/bc207193deb41d44d86a8f69f24c63dbfb4aac61
pythongh-151518: Avoid STW starvation of attaching threadshttps://github.com/tpn/cpython-fork/commit/14a870c1c2c9a2de64f2755efef690b7751bad0d
pythongh-151518https://github.com/python/cpython/issues/151518
14a870chttps://github.com/tpn/cpython-fork/commit/14a870c1c2c9a2de64f2755efef690b7751bad0d
pythongh-151518: Track attach waiters in the thread statehttps://github.com/tpn/cpython-fork/commit/d2adaf428f62530831ca4e6a2fbcbb62b86b0264
pythongh-151518https://github.com/python/cpython/issues/151518
d2adaf4https://github.com/tpn/cpython-fork/commit/d2adaf428f62530831ca4e6a2fbcbb62b86b0264
pythongh-151518: Simplify the GC fairness regressionhttps://github.com/tpn/cpython-fork/commit/09c0d2a64fca33e515fd60a4ae87ac4b891c1196
pythongh-151518https://github.com/python/cpython/issues/151518
09c0d2ahttps://github.com/tpn/cpython-fork/commit/09c0d2a64fca33e515fd60a4ae87ac4b891c1196
pythongh-151518: Exercise STW fairness directlyhttps://github.com/tpn/cpython-fork/commit/cec1cce55eab978f3f75d3e06628f492f0d467d9
pythongh-151518https://github.com/python/cpython/issues/151518
cec1ccehttps://github.com/tpn/cpython-fork/commit/cec1cce55eab978f3f75d3e06628f492f0d467d9
pythongh-151518: Preserve attach waiters when resuming BRC suspensionshttps://github.com/tpn/cpython-fork/commit/c34abd44ea5c1fbd3f3f42da6f19e79a242dd2a8
pythongh-151518https://github.com/python/cpython/issues/151518
c34abd4https://github.com/tpn/cpython-fork/commit/c34abd44ea5c1fbd3f3f42da6f19e79a242dd2a8
pythongh-151518: Strengthen the STW attach fairness regressionhttps://github.com/tpn/cpython-fork/commit/15c4b6dfdb9d2b0e8580120df609d6b4da6d9f74
pythongh-151518https://github.com/python/cpython/issues/151518
15c4b6dhttps://github.com/tpn/cpython-fork/commit/15c4b6dfdb9d2b0e8580120df609d6b4da6d9f74
pythongh-151518: Restrict STW test pauses to free-threaded buildshttps://github.com/tpn/cpython-fork/commit/4093ea6429d65a31ef6ecf059667be9a5613eed4
pythongh-151518https://github.com/python/cpython/issues/151518
4093ea6https://github.com/tpn/cpython-fork/commit/4093ea6429d65a31ef6ecf059667be9a5613eed4
pythongh-151518: Wake only registered attach waitershttps://github.com/tpn/cpython-fork/commit/1a200bfe9b1456c3fa86e8e4962f3e1970399d9b
pythongh-151518https://github.com/python/cpython/issues/151518
1a200bfhttps://github.com/tpn/cpython-fork/commit/1a200bfe9b1456c3fa86e8e4962f3e1970399d9b
gh-151518: Avoid STW starvation of attaching threads (#152826)https://github.com/python/cpython/commit/f1565109603123c39bd2a0c6002f5ea15a62b6e7
gh-151518https://github.com/python/cpython/issues/151518
#152826https://github.com/python/cpython/pull/152826
f156510https://github.com/python/cpython/commit/f1565109603123c39bd2a0c6002f5ea15a62b6e7
tpnhttps://github.com/tpn
completedhttps://github.com/python/cpython/issues?q=is%3Aissue%20state%3Aclosed%20archived%3Afalse%20reason%3Acompleted
#152826https://github.com/python/cpython/pull/152826
on Sep 25, 2026https://github.com/python/cpython/issues/151518#event-31856567343
tpnhttps://github.com/tpn
on Sep 28, 2026https://github.com/python/cpython/issues/151518#event-32017289618
[3.14] pythongh-151518: Avoid STW starvation of attaching threadshttps://github.com/tpn/cpython-fork/commit/3e1a1e66c2906491edb0fac12ec1bb7ab1f785c7
pythongh-151518https://github.com/python/cpython/issues/151518
3e1a1e6https://github.com/tpn/cpython-fork/commit/3e1a1e66c2906491edb0fac12ec1bb7ab1f785c7
encukouhttps://github.com/encukou
on Oct 13, 2026https://github.com/python/cpython/issues/151518#event-33148645681
[3.15] gh-151518: Avoid STW starvation of attaching threads (GH-152826)…https://github.com/python/cpython/commit/ed4f3bccbd42cdc7f90cd7b5d17fe0f2f29be320
gh-151518https://github.com/python/cpython/issues/151518
GH-152826https://github.com/python/cpython/pull/152826
ed4f3bchttps://github.com/python/cpython/commit/ed4f3bccbd42cdc7f90cd7b5d17fe0f2f29be320
Sign up for freehttps://github.com/signup?return_to=https://github.com/python/cpython/issues/151518
Sign in to commenthttps://github.com/login?return_to=https://github.com/python/cpython/issues/151518
interpreter-core(Objects, Python, Grammar, and Parser dirs)https://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22interpreter-core%22
topic-free-threadinghttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22topic-free-threading%22
type-bugAn unexpected behavior, bug, or errorhttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22type-bug%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.