Title: run_with_limited_c_stack(size=...) is unsafe below Py_C_STACK_SIZE on platforms without stack-size detection · Issue #159879 · python/cpython · GitHub
Open Graph Title: run_with_limited_c_stack(size=...) is unsafe below Py_C_STACK_SIZE on platforms without stack-size detection · Issue #159879 · python/cpython
X Title: run_with_limited_c_stack(size=...) is unsafe below Py_C_STACK_SIZE on platforms without stack-size detection · Issue #159879 · python/cpython
Description: Bug report Bug description: test.support.run_with_limited_c_stack() runs a test body in a thread with a chosen C stack size. Its default is deliberately conservative, and the comment says why (Lib/test/support/__init__.py:2942-2945): # S...
Open Graph Description: Bug report Bug description: test.support.run_with_limited_c_stack() runs a test body in a thread with a chosen C stack size. Its default is deliberately conservative, and the comment says why (Lib/...
X Description: Bug report Bug description: test.support.run_with_limited_c_stack() runs a test body in a thread with a chosen C stack size. Its default is deliberately conservative, and the comment says why (Lib/...
Opengraph URL: https://github.com/python/cpython/issues/159879
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"run_with_limited_c_stack(size=...) is unsafe below Py_C_STACK_SIZE on platforms without stack-size detection","articleBody":"# Bug report\n\n### Bug description:\n\n`test.support.run_with_limited_c_stack()` runs a test body in a thread with a chosen C stack size. Its\ndefault is deliberately conservative, and the comment says why\n(`Lib/test/support/__init__.py:2942-2945`):\n\n```python\n# Small enough to be exhausted by tens of thousands of recursive calls,\n# but not smaller than Py_C_STACK_SIZE (4 MiB) which the interpreter\n# assumes if it cannot query the thread stack size.\nC_STACK_SIZE = 8 * 1024 * 1024\n```\n\nThat reasoning is sound, but nothing enforces it on callers that pass `size` explicitly, and one of them\ngoes below the limit the comment warns about:\n\n```\nLib/test/test_lazy_import/__init__.py:311\n @support.run_with_limited_c_stack(depth=10_000, size=256 * 1024)\n\nLib/test/test_profiling/test_sampling_profiler/test_collectors.py:614\n @run_with_limited_c_stack(size=1024 * 1024)\n```\n\nEvery other caller uses the default.\n\nOn a platform where `hardware_stack_limits()` cannot query the real stack, the interpreter assumes\n`Py_C_STACK_SIZE` and its recursion guard is calibrated to that assumption. Giving the thread *less* than\nthat means the guard's soft limit sits below the real stack base and can never fire, so the test does not\nget a `RecursionError` — it gets SIGSEGV.\n\nThat is what happens on Alpine Linux / musl today: `test_lazy_import.LazyImportTypeTests`\n`.test_deeply_dotted_name` crashes the interpreter. Details and the underlying detection problem are in\n[gh-159876](https://github.com/python/cpython/issues/159876); this report is about the test-side contract, which is worth tightening independently of whether\ndetection is ever fixed for musl.\n\nThe set of affected platforms is exactly the exclusion list in `Python/ceval.c` (main:150-152):\n\n```c\n# if defined(HAVE_PTHREAD_GETATTR_NP) && !defined(_AIX) && \\\n !defined(__NetBSD__) && \\\n (defined(__GLIBC__) || defined(__ANDROID__) || !defined(__linux__))\n```\n\nso **AIX, NetBSD and non-glibc Linux** all run on the assumed-size fallback and are all exposed to a caller\nthat passes a small `size`.\n\nTwo further wrinkles:\n\n- The comment's \"(4 MiB)\" is only the `#else` default. When configure sets `-Wl,-z,stack-size=`, the value\n becomes `_Py_LINKER_THREAD_STACK_SIZE` instead — 1 MiB on a stock Linux build. There are also\n `#elif` arms of 320000 (s390x), 1200000 (Android), 1600000 (sparc) and 2000000 (hppa, powerpc64). So the\n real floor is build- and platform-dependent, and a literal `size=` in a test cannot know it.\n- `size=1024 * 1024` in `test_collectors.py` happens to be exactly `Py_C_STACK_SIZE` on a stock Linux build\n — not currently failing, but with no margin at all.\n\nThe helper already has everything it needs to notice: it calls `_has_huge_c_stack()`, which reads\n`_testinternalcapi.get_c_recursion_remaining()`. A thread whose reported headroom does not change when its\nstack size changes is one where detection is not working, which is observable from Python.\n\nReproducer for the general shape, independent of `test_lazy_import` (prints the same number twice where\ndetection is unavailable, two different numbers where it works):\n\n```python\nimport threading, _testinternalcapi as T\nfor size in (256 * 1024, 8 * 1024 * 1024):\n threading.stack_size(size)\n def show(size=size):\n print(\"thread stack %7d B -> c_recursion_remaining=%d\"\n % (size, T.get_c_recursion_remaining()))\n t = threading.Thread(target=show); t.start(); t.join()\n```\n\n```\nAlpine 3.24 / musl: 262144 -> 1450 8388608 -> 1450\nDebian / glibc: 262144 -> 250 8388608 -> 12650\n```\n\n### CPython versions tested on:\n\n3.15, CPython main branch\n\n### Operating systems tested on:\n\nLinux\n\n\n\u003c!-- gh-linked-prs -->\n### Linked PRs\n* gh-159883\n\u003c!-- /gh-linked-prs -->\n","author":{"url":"https://github.com/serhiy-storchaka","@type":"Person","name":"serhiy-storchaka"},"datePublished":"2026-10-20T12:31:47Z","url":"https://github.com/python/cpython/issues/159879"}
| 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:f63df60e-91a3-879e-437a-0a9a0bd1a87d |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | C88A:1201C:3AE928B:4E21F09:6B021B73 |
| html-safe-nonce | 5a449ce253085a819d3d77d0f8691736bc87b0c94f265aeeb4abc013e4d942b5 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJDODhBOjEyMDFDOjNBRTkyOEI6NEUyMUYwOTo2QjAyMUI3MyIsInZpc2l0b3JfaWQiOiI4MjM2ODA0NzEwNTkwNDU0NjQzIiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | b57df726aae9e8dae3af7039dd4fc1a34aee9944955b5cfced5c5e9d0d4e5f1b |
| hovercard-subject-tag | issue:5941671879 |
| 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/159879/issue_layout |
| twitter:image | https://opengraph.githubassets.com/6594f6c9a8ab3775653e14a660604100d3bdb1a300673aff340edc6c0bfc903e/python/cpython/issues/159879 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/6594f6c9a8ab3775653e14a660604100d3bdb1a300673aff340edc6c0bfc903e/python/cpython/issues/159879 |
| og:image:alt | Bug report Bug description: test.support.run_with_limited_c_stack() runs a test body in a thread with a chosen C stack size. Its default is deliberately conservative, and the comment says why (Lib/... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | serhiy-storchaka |
| 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