Title: Free-threaded CPython: concurrent new_threadstate() / PyGILState_Ensure() from N≥~8 OS threads deadlocks in _PyEval_StopTheWorld · Issue #157666 · python/cpython · GitHub
Open Graph Title: Free-threaded CPython: concurrent new_threadstate() / PyGILState_Ensure() from N≥~8 OS threads deadlocks in _PyEval_StopTheWorld · Issue #157666 · python/cpython
X Title: Free-threaded CPython: concurrent new_threadstate() / PyGILState_Ensure() from N≥~8 OS threads deadlocks in _PyEval_StopTheWorld · Issue #157666 · python/cpython
Description: Bug report Bug description: In Python/pystate.c, new_threadstate() → _Py_qsbr_reserve() → (when QSBR freelist is empty) _PyEval_StopTheWorld(interp) (see qsbr.c: _Py_qsbr_reserve, and 3.14.0's pystate.c STW path). When ≥~8 OS threads ent...
Open Graph Description: Bug report Bug description: In Python/pystate.c, new_threadstate() → _Py_qsbr_reserve() → (when QSBR freelist is empty) _PyEval_StopTheWorld(interp) (see qsbr.c: _Py_qsbr_reserve, and 3.14.0's pyst...
X Description: Bug report Bug description: In Python/pystate.c, new_threadstate() → _Py_qsbr_reserve() → (when QSBR freelist is empty) _PyEval_StopTheWorld(interp) (see qsbr.c: _Py_qsbr_reserve, and 3.14.0's ...
Opengraph URL: https://github.com/python/cpython/issues/157666
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Free-threaded CPython: concurrent new_threadstate() / PyGILState_Ensure() from N≥~8 OS threads deadlocks in _PyEval_StopTheWorld","articleBody":"# Bug report\n\n### Bug description:\n\nIn Python/pystate.c, new_threadstate() → _Py_qsbr_reserve() → (when QSBR freelist is empty) _PyEval_StopTheWorld(interp) (see qsbr.c: _Py_qsbr_reserve, and 3.14.0's pystate.c STW path). When ≥~8 OS threads enter PyGILState_Ensure simultaneously from fresh threads, the first one to hit an empty QSBR freelist requests an STW; the other ≥7 threads are concurrently in (or about to enter) tstate_wait_attach/tstate_try_attach/new_threadstate. They cannot proceed because the STW is not yet complete, and the STW requester cannot complete because the attach path is blocked — a circular stall (futex_do_wait, ~0 CPU).\n\nDistinct from #151518 (and not fixed by the first pull request there).\n\nShort reproducer:\n\n```c\n/* N pthreads, each: PyGILState_Ensure -> loop of 5,000x PyEval_SaveThread/RestoreThread -> PyGILState_Release.\n Trigger: >=8 threads (deterministic at 8, 12, 16, 20 on every version tested);\n 6 and below: passes. \n\ncc -O2 -pthread -o repro repro_ft.c \\\n -I/tmp/\u003cprefix>/include/python3.1Xt \\\n /tmp/\u003cprefix>/lib/libpython3.1Xt.a -lpthread -lm -lutil -ldl\n\ntimeout 45 ./repro 8 # -> \"launched worker 8\" then HANG (exit 124)\n*/\n#include \u003cPython.h>\n#include \u003cpthread.h>\n#include \u003cstdio.h>\n#include \u003cstdlib.h>\n\nstatic volatile long long counter = 0;\nstatic volatile int g_stop = 0;\nstatic int g_iters = 5000;\nstatic int g_mode = 1; /* 1=churn, 0=once */\n\nstatic void *worker(void *arg) {\n PyGILState_STATE st = PyGILState_Ensure();\n long long n = 0;\n for (long long i = 0; i \u003c g_iters && !g_stop; i++) {\n if (g_mode == 0) { /* once: already ensured once, loop is Save/Restore only */\n }\n PyThreadState *ts = PyEval_SaveThread();\n for (volatile int k = 0; k \u003c 100; k++) {}\n PyEval_RestoreThread(ts);\n n++;\n }\n PyGILState_Release(st);\n __sync_fetch_and_add(&counter, n);\n return NULL;\n}\n\nint main(int argc, char **argv) {\n int n = argc > 1 ? atoi(argv[1]) : 8;\n g_mode = argc > 2 && !strcmp(argv[2], \"once\") ? 0 : 1;\n g_iters = argc > 3 ? (int)atol(argv[3]) : 5000;\n Py_Initialize();\n fprintf(stderr, \"Py_VERSION=%s\\n\", Py_GetVersion());\n pthread_t *t = malloc(sizeof(pthread_t) * n);\n for (int i = 0; i \u003c n; i++) { pthread_create(&t[i], NULL, worker, NULL); fprintf(stderr, \"launched worker %d\\n\", i); }\n for (int i = 0; i \u003c n; i++) pthread_join(t[i], NULL);\n fprintf(stderr, \"ALL WORKERS COMPLETED (PASS) counter=%lld\\n\", counter);\n Py_Finalize();\n return 0;\n}\n```\n\nEnvironment / repro notes\n- Tested on Ryzen 7700X (8 physical cores, 16 logical) (my B200 is undergoing repairs, honestly)\n- Ubuntu 25.04\n- Linux kernel 7.2.2\n- GNU C (cc) 14.2.0\n- GIL-disabled builds only (Py_GIL_DISABLED=1); GIL-enabled builds do not exercise the Py_GILState_* STW path in this code.\n- LD_PRELOAD none; no ASAN/UBSAN at build time.\n\nResults\n| Threads | 3.14.0 (installed, FT) | 3.15.0rc2 (unpatched, FT) | 3.15.0rc2 + PR #152826 (FT) |\n|---------|------------------------|---------------------------|-----------------------------|\n| 4 | PASS | PASS | PASS |\n| 6 | PASS | PASS | PASS |\n| 8 | HANG (exit 124) | HANG (exit 124) | HANG (exit 124) |\n| 12 | HANG (exit 124) | HANG (exit 124) | HANG (exit 124) |\n| 20 | HANG (exit 124) | — | — |\n\nCheckbox (per CPython \"Reporting bugs\" convention)\n- [x] I can reproduce this on the current stable (3.14.0) and current development/RC (3.15.0rc2) free-threading builds.\n- [x] I built from source (3.15.0rc2) and I can provide the exact --disable-gil build recipe.\n- [ ] I can reproduce this on the Windows or macOS free-threading builds — not tested (Linux x86_64 only so far).\n- [ ] I can reproduce it on Python \u003c 3.14 — not tested (free-threading is 3.13+/3.14+/3.15+; I expect the same stall on 3.13 free-threading but have not measured it).\n\n\n\n\n### CPython versions tested on:\n\n3.15\n\n### Operating systems tested on:\n\nLinux","author":{"url":"https://github.com/CypherGrue","@type":"Person","name":"CypherGrue"},"datePublished":"2026-09-17T08:52:38Z","url":"https://github.com/python/cpython/issues/157666"}
| 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:358ebfe3-bea1-cc8a-d582-c00b1846a813 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | E164:F7C4C:344245B:45F6A2C:6B020950 |
| html-safe-nonce | d3e4422f01f0df87f92132dc441a12707c7d0f0eabf0f03378e3269d954c2674 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJFMTY0OkY3QzRDOjM0NDI0NUI6NDVGNkEyQzo2QjAyMDk1MCIsInZpc2l0b3JfaWQiOiI3MDg2OTYzMTE3NDE5NDIwOTYiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 742653a76b1db5502b93664ce27e6a867ba16ee1307b72a216cbd33f387d7036 |
| hovercard-subject-tag | issue:5485153559 |
| 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/157666/issue_layout |
| twitter:image | https://opengraph.githubassets.com/a525f5f93e49738a41da26cb8e3700e74b0685a6d78ab17c1a5d63d078ff4911/python/cpython/issues/157666 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/a525f5f93e49738a41da26cb8e3700e74b0685a6d78ab17c1a5d63d078ff4911/python/cpython/issues/157666 |
| og:image:alt | Bug report Bug description: In Python/pystate.c, new_threadstate() → _Py_qsbr_reserve() → (when QSBR freelist is empty) _PyEval_StopTheWorld(interp) (see qsbr.c: _Py_qsbr_reserve, and 3.14.0's pyst... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | CypherGrue |
| 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