@@ -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+
126149static void
127150hardware_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