Skip to content

Commit 9914c15

Browse files
rokmmiss-islington
authored andcommitted
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. (cherry picked from commit 7016044) Co-authored-by: Rok Mandeljc <[email protected]>
1 parent 460ad1a commit 9914c15

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
@@ -437,6 +437,13 @@ _Py_InitializeRecursionLimits(PyThreadState *tstate)
437437
SetThreadStackGuarantee(&guarantee);
438438
_tstate->c_stack_hard_limit = ((uintptr_t)low) + guarantee + _PyOS_STACK_MARGIN_BYTES;
439439
_tstate->c_stack_soft_limit = _tstate->c_stack_hard_limit + _PyOS_STACK_MARGIN_BYTES;
440+
#elif defined(__APPLE__)
441+
pthread_t this_thread = pthread_self();
442+
void *stack_addr = pthread_get_stackaddr_np(this_thread); // top of the stack
443+
size_t stack_size = pthread_get_stacksize_np(this_thread);
444+
_tstate->c_stack_top = (uintptr_t)stack_addr;
445+
_tstate->c_stack_hard_limit = _tstate->c_stack_top - stack_size;
446+
_tstate->c_stack_soft_limit = _tstate->c_stack_hard_limit + _PyOS_STACK_MARGIN_BYTES;
440447
#else
441448
uintptr_t here_addr = _Py_get_machine_stack_pointer();
442449
/// XXX musl supports HAVE_PTHRED_GETATTR_NP, but the resulting stack size

0 commit comments

Comments
 (0)