Skip to content

Commit ae222a3

Browse files
pythongh-159876: Detect the C stack size of non-main threads on musl
hardware_stack_limits() skipped pthread_getattr_np() on any Linux that is neither glibc nor Android, because musl misreports the main thread's stack as its own default thread stack size rather than the RLIMIT_STACK sized real one. musl is accurate for the threads it creates itself, though, so the blanket exclusion left the interpreter assuming Py_C_STACK_SIZE on every thread: one created with a smaller stack overflowed before the recursion guard could fire, crashing instead of raising RecursionError. Query the stack on every thread except the process main thread, where the previous estimate is kept. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
1 parent 036d309 commit ae222a3

2 files changed

Lines changed: 50 additions & 20 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Query the real C stack size of non-main threads on musl-based Linux again.
2+
The interpreter assumed a fixed ``Py_C_STACK_SIZE`` there, so a thread
3+
created with a smaller stack overflowed it instead of raising
4+
:exc:`RecursionError`. The main thread, whose size musl does not report
5+
correctly, keeps using the estimate.

‎Python/ceval.c‎

Lines changed: 45 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,29 @@ int pthread_attr_destroy(pthread_attr_t *a)
123123

124124
#endif
125125

126+
// Any Linux that is neither glibc nor Android (in practice, musl) cannot
127+
// report the main thread's stack through pthread_getattr_np().
128+
#if defined(__linux__) && !defined(__GLIBC__) && !defined(__ANDROID__)
129+
# define _Py_STACK_QUERY_SKIPS_MAIN_THREAD 1
130+
#else
131+
# define _Py_STACK_QUERY_SKIPS_MAIN_THREAD 0
132+
#endif
133+
134+
#if _Py_STACK_QUERY_SKIPS_MAIN_THREAD
135+
static int
136+
stack_on_main_thread(void)
137+
{
138+
# ifdef PY_HAVE_THREAD_NATIVE_ID
139+
return PyThread_get_thread_native_id() == (unsigned long)getpid();
140+
# else
141+
// Without a thread id to compare, assume the worst and keep estimating.
142+
return 1;
143+
# endif
144+
}
145+
#else
146+
# define stack_on_main_thread() 0
147+
#endif
148+
126149
static void
127150
hardware_stack_limits(uintptr_t *base, uintptr_t *top, uintptr_t sp)
128151
{
@@ -142,27 +165,29 @@ hardware_stack_limits(uintptr_t *base, uintptr_t *top, uintptr_t sp)
142165
*top = (uintptr_t)stack_addr;
143166
*base = ((uintptr_t)stack_addr) - stack_size;
144167
#else
145-
// musl supports HAVE_PTHREAD_GETATTR_NP, but the resulting stack size
146-
// (on alpine at least) is much smaller than expected and imposes undue
147-
// limits compared to the old stack size estimation. musl doesn't give
148-
// us a way to detect it, so we have to assume any non-glibc,
149-
// non-Android Linux is musl and mustn't rely on pthread_getattr_np.
150168
# if defined(HAVE_PTHREAD_GETATTR_NP) && !defined(_AIX) && \
151-
!defined(__NetBSD__) && \
152-
(defined(__GLIBC__) || defined(__ANDROID__) || !defined(__linux__))
153-
size_t stack_size, guard_size;
154-
void *stack_addr;
155-
pthread_attr_t attr;
156-
int err = pthread_getattr_np(pthread_self(), &attr);
157-
if (err == 0) {
158-
err = pthread_attr_getguardsize(&attr, &guard_size);
159-
err |= pthread_attr_getstack(&attr, &stack_addr, &stack_size);
160-
err |= pthread_attr_destroy(&attr);
161-
}
162-
if (err == 0) {
163-
*base = ((uintptr_t)stack_addr) + guard_size;
164-
*top = (uintptr_t)stack_addr + stack_size;
165-
return;
169+
!defined(__NetBSD__)
170+
// On musl, pthread_getattr_np() reports the real stack of a thread that
171+
// the library itself created, but for the process main thread it reports
172+
// musl's default thread stack size rather than the real, RLIMIT_STACK
173+
// sized stack (see gh-131338). Keep estimating the main thread's stack
174+
// there, and query every other thread, whose size the estimate cannot
175+
// know (see gh-159876).
176+
if (!_Py_STACK_QUERY_SKIPS_MAIN_THREAD || !stack_on_main_thread()) {
177+
size_t stack_size, guard_size;
178+
void *stack_addr;
179+
pthread_attr_t attr;
180+
int err = pthread_getattr_np(pthread_self(), &attr);
181+
if (err == 0) {
182+
err = pthread_attr_getguardsize(&attr, &guard_size);
183+
err |= pthread_attr_getstack(&attr, &stack_addr, &stack_size);
184+
err |= pthread_attr_destroy(&attr);
185+
}
186+
if (err == 0) {
187+
*base = ((uintptr_t)stack_addr) + guard_size;
188+
*top = (uintptr_t)stack_addr + stack_size;
189+
return;
190+
}
166191
}
167192
# endif
168193
// Add some space for caller function then round to minimum page size

0 commit comments

Comments
 (0)