Title: C stack limit detection is disabled on musl, so deep C recursion crashes instead of raising RecursionError · Issue #159876 · python/cpython · GitHub
Open Graph Title: C stack limit detection is disabled on musl, so deep C recursion crashes instead of raising RecursionError · Issue #159876 · python/cpython
X Title: C stack limit detection is disabled on musl, so deep C recursion crashes instead of raising RecursionError · Issue #159876 · python/cpython
Description: Bug report Bug description: On Alpine Linux (musl libc), test_lazy_import.LazyImportTypeTests.test_deeply_dotted_name terminates the interpreter with SIGSEGV instead of raising RecursionError: $ ./python -m unittest test.test_lazy_import...
Open Graph Description: Bug report Bug description: On Alpine Linux (musl libc), test_lazy_import.LazyImportTypeTests.test_deeply_dotted_name terminates the interpreter with SIGSEGV instead of raising RecursionError: $ ./...
X Description: Bug report Bug description: On Alpine Linux (musl libc), test_lazy_import.LazyImportTypeTests.test_deeply_dotted_name terminates the interpreter with SIGSEGV instead of raising RecursionError: $ ./...
Opengraph URL: https://github.com/python/cpython/issues/159876
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"C stack limit detection is disabled on musl, so deep C recursion crashes instead of raising RecursionError","articleBody":"# Bug report\n\n### Bug description:\n\nOn Alpine Linux (musl libc), `test_lazy_import.LazyImportTypeTests.test_deeply_dotted_name` terminates the\ninterpreter with SIGSEGV instead of raising `RecursionError`:\n\n```\n$ ./python -m unittest test.test_lazy_import.LazyImportTypeTests.test_deeply_dotted_name\ntest_deeply_dotted_name (...)\nA long dotted name must not overflow the C stack. ... Segmentation fault\n```\n\nThe crash is a C stack overflow in the prologue of a recursive call:\n\n```\nThread 2 \"Thread-1 (run)\" received signal SIGSEGV, Segmentation fault.\n0x00005555556bc2dc in lazy_import_replay_from (...) at ../Objects/lazyimportobject.c:280\n#1 0x00005555556bc331 in lazy_import_replay_from (...) at ../Objects/lazyimportobject.c:287\n#2 0x00005555556bc331 in lazy_import_replay_from (...) at ../Objects/lazyimportobject.c:287\n... (repeats)\n```\n\n`lazy_import_replay_from()` does guard itself with `_Py_EnterRecursiveCallTstate()`, so the guard is simply\nnot firing.\n\n#### Cause\n\nThe test runs its body in a thread with a 256 KiB stack\n(`@support.run_with_limited_c_stack(depth=10_000, size=256 * 1024)`), but CPython never learns that.\n`hardware_stack_limits()` in `Python/ceval.c` only queries the real stack bounds when:\n\n```c\n# if defined(HAVE_PTHREAD_GETATTR_NP) && !defined(_AIX) && \\\n !defined(__NetBSD__) && \\\n (defined(__GLIBC__) || defined(__ANDROID__) || !defined(__linux__))\n```\n\n(`Python/ceval.c:150-152` on main, `:162-164` on 3.15, `:462-464` on 3.14.)\n\nOn musl, `__linux__` is defined while `__GLIBC__` and `__ANDROID__` are not, so the probe is skipped and\nCPython falls back to guessing `base = sp - Py_C_STACK_SIZE`. With the `-Wl,-z,stack-size=` that configure\nadds, `Py_C_STACK_SIZE` is `_Py_LINKER_THREAD_STACK_SIZE` = 1048576, so **the interpreter assumes 1 MiB on\nevery thread regardless of the thread's real size**.\n\nMeasured with `_testinternalcapi.get_c_recursion_remaining()`:\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: thread stack 262144 B -> c_recursion_remaining=1450\n thread stack 8388608 B -> c_recursion_remaining=1450 # constant\nDebian / glibc: thread stack 262144 B -> c_recursion_remaining=250\n thread stack 8388608 B -> c_recursion_remaining=12650 # tracks reality\n```\n\nSo on a 256 KiB thread the interpreter believes it has roughly 4x the stack it actually has, the soft limit\nlands below the real stack base, and the guard can never trigger.\n\n#### The exclusion looks broader than the problem it was added for\n\nIt came from GH-134336 (\"Disable computed stack limit checks on non-glibc linux\", gh-131338). But musl's\n`pthread_getattr_np()` is accurate for secondary threads and only wrong for the **main** thread:\n\n```c\n/* cc -o stackprobe stackprobe.c */\n#define _GNU_SOURCE\n#include \u003cpthread.h>\n#include \u003cstdio.h>\nstatic void report(const char *tag) {\n pthread_attr_t attr; void *addr = NULL; size_t size = 0, guard = 0;\n if (pthread_getattr_np(pthread_self(), &attr)) { printf(\"%s: failed\\n\", tag); return; }\n pthread_attr_getstack(&attr, &addr, &size);\n pthread_attr_getguardsize(&attr, &guard);\n pthread_attr_destroy(&attr);\n printf(\"%-14s base=%p size=%zu (%.2f MiB) guard=%zu\\n\", tag, addr, size, size/1048576.0, guard);\n}\nstatic void *thr(void *a) { report((const char *)a); return NULL; }\nint main(void) {\n pthread_attr_t a; pthread_t t;\n report(\"main\");\n pthread_attr_init(&a); pthread_attr_setstacksize(&a, 8u\u003c\u003c20);\n pthread_create(&t, &a, thr, (void *)\"thread-8MiB\"); pthread_join(t, NULL);\n pthread_attr_destroy(&a);\n pthread_create(&t, NULL, thr, (void *)\"thread-default\"); pthread_join(t, NULL);\n return 0;\n}\n```\n\n```\nAlpine 3.24 / musl 1.2:\nmain base=0x7ffeda154000 size=131072 (0.12 MiB) guard=0 \u003c-- wrong, RLIMIT_STACK is 8 MiB\nthread-8MiB base=0x7f772ee70000 size=8391456 (8.00 MiB) guard=8192 \u003c-- correct\nthread-default base=0x7f772f650000 size=133920 (0.13 MiB) guard=8192 \u003c-- correct\n\nDebian / glibc 2.41:\nmain base=0x7fffa3c90000 size=8380416 (7.99 MiB) guard=0\nthread-8MiB base=0x7909e3800000 size=8388608 (8.00 MiB) guard=4096\nthread-default base=0x7909e3800000 size=8388608 (8.00 MiB) guard=4096\n```\n\nThis is the same shape as gh-159320 / GH-159321, which re-enabled detection for Android after it had been\ncaught by the same blanket condition.\n\n#### Scope\n\nThe broken detection is not specific to `test_lazy_import` or to 3.15+: the guard is identical on 3.14, and\nthe probe above returns the same constant 1450 on a 3.14 build. 3.14 is only green because\n`test_lazy_import` does not exist there and no other test asks for a thread smaller than the assumed\n`Py_C_STACK_SIZE`. Any embedder or stdlib caller creating a musl thread with less than ~1 MiB of stack can\noverflow silently.\n\nApart from this one test, the suite is clean on Alpine: `main` 550 test files run / 1 failed, `3.15` 548 /\n1 failed (same test), `3.14` 525 / 0 failed.\n\nI appreciate musl is PEP 11 tier 0 (configure says so explicitly) and that gh-151546, gh-139467 and gh-76488\nwere all closed as `OS-unsupported`. Filing this one anyway because it is a hard crash where a guard exists\nand is expected to work, and because the Android case was treated as worth fixing.\n\n### CPython versions tested on:\n\n3.14, 3.15, CPython main branch\n\n### Operating systems tested on:\n\nLinux\n\n\n\u003c!-- gh-linked-prs -->\n### Linked PRs\n* gh-159882\n\u003c!-- /gh-linked-prs -->\n","author":{"url":"https://github.com/serhiy-storchaka","@type":"Person","name":"serhiy-storchaka"},"datePublished":"2026-10-20T12:30:49Z","url":"https://github.com/python/cpython/issues/159876"}
| 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:a684f1ef-8202-65be-7073-094930af13d1 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | AA90:11EACE:36A42D3:48EA299:6B020B54 |
| html-safe-nonce | 5d47faed80cc89f636a0e46b996acf2ef884b719234d3230ce061b59edcb1cf7 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBQTkwOjExRUFDRTozNkE0MkQzOjQ4RUEyOTk6NkIwMjBCNTQiLCJ2aXNpdG9yX2lkIjoiMzk4NTMzOTQ5NzY1NDEyNzQ0NCIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | d28bcd2de72873cfbdef4e32d5889242e734711f43ecf8f15959ab1ff50cb909 |
| hovercard-subject-tag | issue:5941658517 |
| 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/159876/issue_layout |
| twitter:image | https://opengraph.githubassets.com/2078650a88f6d980ff7b044a0cbacd36ed4b2bfbe4776ea6e282bb011622c510/python/cpython/issues/159876 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/2078650a88f6d980ff7b044a0cbacd36ed4b2bfbe4776ea6e282bb011622c510/python/cpython/issues/159876 |
| og:image:alt | Bug report Bug description: On Alpine Linux (musl libc), test_lazy_import.LazyImportTypeTests.test_deeply_dotted_name terminates the interpreter with SIGSEGV instead of raising RecursionError: $ ./... |
| 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