Skip to content

Commit 0fca453

Browse files
rokmhroncok
authored andcommitted
pythongh-139231: Fix estimation of available stack size for recursion limit on macOS (pythonGH-139232)
Use `pthread_get_stackaddr_np()` and `pthread_get_stacksize_np()` to determine the stack address and size. (cherry picked from commit 7016044)
1 parent 40fd66b commit 0fca453

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
@@ -449,6 +449,13 @@ _Py_InitializeRecursionLimits(PyThreadState *tstate)
449449
SetThreadStackGuarantee(&guarantee);
450450
_tstate->c_stack_hard_limit = ((uintptr_t)low) + guarantee + _PyOS_STACK_MARGIN_BYTES;
451451
_tstate->c_stack_soft_limit = _tstate->c_stack_hard_limit + _PyOS_STACK_MARGIN_BYTES;
452+
#elif defined(__APPLE__)
453+
pthread_t this_thread = pthread_self();
454+
void *stack_addr = pthread_get_stackaddr_np(this_thread); // top of the stack
455+
size_t stack_size = pthread_get_stacksize_np(this_thread);
456+
_tstate->c_stack_top = (uintptr_t)stack_addr;
457+
_tstate->c_stack_hard_limit = _tstate->c_stack_top - stack_size;
458+
_tstate->c_stack_soft_limit = _tstate->c_stack_hard_limit + _PyOS_STACK_MARGIN_BYTES;
452459
#else
453460
uintptr_t here_addr = _Py_get_machine_stack_pointer();
454461
/// XXX musl supports HAVE_PTHRED_GETATTR_NP, but the resulting stack size

0 commit comments

Comments
 (0)