Skip to content

Commit 7016044

Browse files
authored
gh-139231: Fix estimation of available stack size for recursion limit on macOS (GH-139232)
Use `pthread_get_stackaddr_np()` and `pthread_get_stacksize_np()` to determine the stack address and size.
1 parent a5e0562 commit 7016044

File tree

1 file changed

+7
-0
lines changed

1 file changed

+7
-0
lines changed

Python/ceval.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -451,6 +451,13 @@ _Py_InitializeRecursionLimits(PyThreadState *tstate)
451451
SetThreadStackGuarantee(&guarantee);
452452
_tstate->c_stack_hard_limit = ((uintptr_t)low) + guarantee + _PyOS_STACK_MARGIN_BYTES;
453453
_tstate->c_stack_soft_limit = _tstate->c_stack_hard_limit + _PyOS_STACK_MARGIN_BYTES;
454+
#elif defined(__APPLE__)
455+
pthread_t this_thread = pthread_self();
456+
void *stack_addr = pthread_get_stackaddr_np(this_thread); // top of the stack
457+
size_t stack_size = pthread_get_stacksize_np(this_thread);
458+
_tstate->c_stack_top = (uintptr_t)stack_addr;
459+
_tstate->c_stack_hard_limit = _tstate->c_stack_top - stack_size;
460+
_tstate->c_stack_soft_limit = _tstate->c_stack_hard_limit + _PyOS_STACK_MARGIN_BYTES;
454461
#else
455462
uintptr_t here_addr = _Py_get_machine_stack_pointer();
456463
/// XXX musl supports HAVE_PTHRED_GETATTR_NP, but the resulting stack size

0 commit comments

Comments
 (0)