diff --git a/Misc/NEWS.d/next/Build/2021-06-19-18-04-47.bpo-32307.V1wzBY.rst b/Misc/NEWS.d/next/Build/2021-06-19-18-04-47.bpo-32307.V1wzBY.rst new file mode 100644 index 00000000000000..d767051b9f8a0a --- /dev/null +++ b/Misc/NEWS.d/next/Build/2021-06-19-18-04-47.bpo-32307.V1wzBY.rst @@ -0,0 +1,4 @@ +Force a default thread stack size value for all platforms instead of +assuming that they all have enough for Python threads. This makes it so too +many recursions are caught by the interpreter instead of stack overflowing +on musl libc. diff --git a/Python/thread_pthread.h b/Python/thread_pthread.h index e660bca8406860..bec8e9b20f8c77 100644 --- a/Python/thread_pthread.h +++ b/Python/thread_pthread.h @@ -40,7 +40,9 @@ be conditional on _POSIX_THREAD_ATTR_STACKSIZE being defined. */ #ifdef _POSIX_THREAD_ATTR_STACKSIZE #ifndef THREAD_STACK_SIZE -#define THREAD_STACK_SIZE 0 /* use default stack size */ +/* -1 means THREAD_STACK_SIZE hasn't been given a proper value yet. + -DTHREAD_STACK_SIZE=0 in CFLAGS will force the platform's default instead. */ +#define THREAD_STACK_SIZE -1 #endif /* The default stack size for new threads on BSD is small enough that @@ -54,28 +56,30 @@ * as it also depends on the other configure options like chosen sanitizer * runtimes. */ -#if defined(__FreeBSD__) && defined(THREAD_STACK_SIZE) && THREAD_STACK_SIZE == 0 +#if defined(__FreeBSD__) && THREAD_STACK_SIZE == -1 #undef THREAD_STACK_SIZE #define THREAD_STACK_SIZE 0x400000 #endif -#if defined(_AIX) && defined(THREAD_STACK_SIZE) && THREAD_STACK_SIZE == 0 -#undef THREAD_STACK_SIZE -#define THREAD_STACK_SIZE 0x200000 -#endif /* bpo-38852: test_threading.test_recursion_limit() checks that 1000 recursive Python calls (default recursion limit) doesn't crash, but raise a regular RecursionError exception. In debug mode, Python function calls allocates more memory on the stack, so use a stack of 8 MiB. */ -#if defined(__ANDROID__) && defined(THREAD_STACK_SIZE) && THREAD_STACK_SIZE == 0 +#if defined(__ANDROID__) && THREAD_STACK_SIZE == -1 # ifdef Py_DEBUG # undef THREAD_STACK_SIZE # define THREAD_STACK_SIZE 0x800000 # endif #endif -#if defined(__VXWORKS__) && defined(THREAD_STACK_SIZE) && THREAD_STACK_SIZE == 0 +#if defined(__VXWORKS__) && THREAD_STACK_SIZE == -1 #undef THREAD_STACK_SIZE #define THREAD_STACK_SIZE 0x100000 #endif +#if THREAD_STACK_SIZE == -1 +#undef THREAD_STACK_SIZE +/* This is big enough for most platforms without being wasteful. */ +#define THREAD_STACK_SIZE 0x200000 +#endif + /* for safety, ensure a viable minimum stacksize */ #define THREAD_STACK_MIN 0x8000 /* 32 KiB */ #else /* !_POSIX_THREAD_ATTR_STACKSIZE */