From 0d8f676380e5558309f32753338d312f3f921e93 Mon Sep 17 00:00:00 2001 From: "R. David Murray" Date: Tue, 20 May 2025 10:16:15 -0400 Subject: [PATCH 1/5] Disable new stack size code for non-glibc. MUSL claims to support pthread_getattr_np, but the response returned is off by at least an order of magnitude compared to reality. This is apparently a long standing problem. MUSL also does not provide any way to detect directly that it is the libc. It would be possible to add things to configure to detect that MUSL is the compiler, but since this code has caused problems for a lot of non-glibc platforms, it seems easier to just restrict it to glibc. --- Python/ceval.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Python/ceval.c b/Python/ceval.c index 490b653f132a6a..efed49b7055fce 100644 --- a/Python/ceval.c +++ b/Python/ceval.c @@ -442,7 +442,8 @@ _Py_InitializeRecursionLimits(PyThreadState *tstate) _tstate->c_stack_soft_limit = _tstate->c_stack_hard_limit + PYOS_STACK_MARGIN_BYTES; #else uintptr_t here_addr = _Py_get_machine_stack_pointer(); -# if defined(HAVE_PTHREAD_GETATTR_NP) && !defined(_AIX) && !defined(__NetBSD__) +# if defined(HAVE_PTHREAD_GETATTR_NP) && !defined(_AIX) && \ + !defined(__NetBSD__) && defined(__GLIBC__) size_t stack_size, guard_size; void *stack_addr; pthread_attr_t attr; From d7262cc9977fecbbc0bc999afc873c04498e958d Mon Sep 17 00:00:00 2001 From: "R. David Murray" Date: Sun, 15 Jun 2025 14:22:48 -0400 Subject: [PATCH 2/5] Don't require glibc if it isn't linux. This exempts alpine but doesn't affect, eg, FreeBSD. --- Python/ceval.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/Python/ceval.c b/Python/ceval.c index efed49b7055fce..6e834c0d93c8f4 100644 --- a/Python/ceval.c +++ b/Python/ceval.c @@ -442,8 +442,11 @@ _Py_InitializeRecursionLimits(PyThreadState *tstate) _tstate->c_stack_soft_limit = _tstate->c_stack_hard_limit + PYOS_STACK_MARGIN_BYTES; #else uintptr_t here_addr = _Py_get_machine_stack_pointer(); +/// XXX musl supports HAVE_PTHRED_GETATTR_NP, but the resulting stack size +/// (on alpine at least) is much smaller than expected and imposes undue limits +/// compared to the old stack size estimation. # if defined(HAVE_PTHREAD_GETATTR_NP) && !defined(_AIX) && \ - !defined(__NetBSD__) && defined(__GLIBC__) + !defined(__NetBSD__) && (defined(__GLIBC__) || !defined(__linux__)) size_t stack_size, guard_size; void *stack_addr; pthread_attr_t attr; From 5c55790c96adc5a06a1a6fd8f9dc18f8eedfb570 Mon Sep 17 00:00:00 2001 From: "R. David Murray" Date: Mon, 16 Jun 2025 15:53:12 -0400 Subject: [PATCH 3/5] Clarify why we check __glibc__. --- Python/ceval.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Python/ceval.c b/Python/ceval.c index 6e834c0d93c8f4..a6f4799f462f24 100644 --- a/Python/ceval.c +++ b/Python/ceval.c @@ -444,7 +444,7 @@ _Py_InitializeRecursionLimits(PyThreadState *tstate) uintptr_t here_addr = _Py_get_machine_stack_pointer(); /// XXX musl supports HAVE_PTHRED_GETATTR_NP, but the resulting stack size /// (on alpine at least) is much smaller than expected and imposes undue limits -/// compared to the old stack size estimation. +/// compared to the old stack size estimation. (We assume musl if not glibc.) # if defined(HAVE_PTHREAD_GETATTR_NP) && !defined(_AIX) && \ !defined(__NetBSD__) && (defined(__GLIBC__) || !defined(__linux__)) size_t stack_size, guard_size; From ac92003c0439db5b79786c6c783331a27f2b9a13 Mon Sep 17 00:00:00 2001 From: Kumar Aditya Date: Fri, 25 Jul 2025 22:22:20 +0530 Subject: [PATCH 4/5] Apply suggestion from @vstinner Co-authored-by: Victor Stinner --- Python/ceval.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Python/ceval.c b/Python/ceval.c index a6f4799f462f24..f7a5f5b0104969 100644 --- a/Python/ceval.c +++ b/Python/ceval.c @@ -444,7 +444,7 @@ _Py_InitializeRecursionLimits(PyThreadState *tstate) uintptr_t here_addr = _Py_get_machine_stack_pointer(); /// XXX musl supports HAVE_PTHRED_GETATTR_NP, but the resulting stack size /// (on alpine at least) is much smaller than expected and imposes undue limits -/// compared to the old stack size estimation. (We assume musl if not glibc.) +/// compared to the old stack size estimation. (We assume musl is not glibc.) # if defined(HAVE_PTHREAD_GETATTR_NP) && !defined(_AIX) && \ !defined(__NetBSD__) && (defined(__GLIBC__) || !defined(__linux__)) size_t stack_size, guard_size; From 733a28b74e1837cee735d725e17d1c8f37e2f9be Mon Sep 17 00:00:00 2001 From: Kumar Aditya Date: Fri, 25 Jul 2025 22:32:07 +0530 Subject: [PATCH 5/5] add news --- .../2025-07-25-22-31-52.gh-issue-131338.zJDCMp.rst | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 Misc/NEWS.d/next/Core_and_Builtins/2025-07-25-22-31-52.gh-issue-131338.zJDCMp.rst diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2025-07-25-22-31-52.gh-issue-131338.zJDCMp.rst b/Misc/NEWS.d/next/Core_and_Builtins/2025-07-25-22-31-52.gh-issue-131338.zJDCMp.rst new file mode 100644 index 00000000000000..6c064e8f4a0339 --- /dev/null +++ b/Misc/NEWS.d/next/Core_and_Builtins/2025-07-25-22-31-52.gh-issue-131338.zJDCMp.rst @@ -0,0 +1,2 @@ +Disable computed stack limit checks on non-glibc linux platforms to fix +crashes on deep recursion.