From 4c36b0748d42045193d940efc5ae8aa2125242e9 Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Mon, 19 Oct 2026 20:57:40 -0700 Subject: [PATCH 1/2] gh-159603: Call pthread_attr_init() in hardware_stack_limits() Always call pthread_attr_init() in hardware_stack_limits(), not only on FreeBSD (if pthread_attr_get_np() is available). --- Python/ceval.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/Python/ceval.c b/Python/ceval.c index d63dffdd789d566..5dcf7f113725d5a 100644 --- a/Python/ceval.c +++ b/Python/ceval.c @@ -158,11 +158,12 @@ hardware_stack_limits(uintptr_t *base, uintptr_t *top, uintptr_t sp) size_t stack_size, guard_size; void *stack_addr; pthread_attr_t attr; -#ifdef _Py_HAVE_PTHREAD_ATTR_GET_NP + int err = pthread_attr_init(&attr); +#ifdef _Py_HAVE_PTHREAD_ATTR_GET_NP err |= pthread_attr_get_np(pthread_self(), &attr); #else - int err = pthread_getattr_np(pthread_self(), &attr); + err |= pthread_getattr_np(pthread_self(), &attr); #endif if (err == 0) { err = pthread_attr_getguardsize(&attr, &guard_size); @@ -175,6 +176,7 @@ hardware_stack_limits(uintptr_t *base, uintptr_t *top, uintptr_t sp) return; } # endif + // Add some space for caller function then round to minimum page size // This is a guess at the top of the stack, but should be a reasonably // good guess if called from _PyThreadState_Attach when creating a thread. From 8cf4b8feae615bbced5ee5084bf818fcc0293835 Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Tue, 20 Oct 2026 12:04:43 -0500 Subject: [PATCH 2/2] Emscripten: add pthread_attr_init() workaround --- Python/ceval.c | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/Python/ceval.c b/Python/ceval.c index 5dcf7f113725d5a..2b8c425789c0398 100644 --- a/Python/ceval.c +++ b/Python/ceval.c @@ -79,6 +79,7 @@ _Py_ReachedRecursionLimitWithMargin(PyThreadState *tstate, int margin_count) #include "emscripten/stack.h" #define pthread_attr_t workaround_pthread_attr_t +#define pthread_attr_init workaround_pthread_attr_init #define pthread_getattr_np workaround_pthread_getattr_np #define pthread_attr_getguardsize workaround_pthread_attr_getguardsize #define pthread_attr_getstack workaround_pthread_attr_getstack @@ -91,6 +92,11 @@ typedef struct { extern __attribute__((__visibility__("hidden"))) unsigned __default_guardsize; +int pthread_attr_init(pthread_attr_t *a) +{ + return 0; +} + // Modified version of pthread_getattr_np from the upstream PR. int pthread_getattr_np(pthread_t thread, pthread_attr_t *attr) {