Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[3.13] gh-128717: Stop-the-world when setting the recursion limit (GH-128741) #128757

Merged
merged 6 commits into from
Jan 14, 2025
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions Lib/test/test_sys.py
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,34 @@ def test_setrecursionlimit_to_depth(self):
finally:
sys.setrecursionlimit(old_limit)

@unittest.skipUnless(support.Py_GIL_DISABLED, "only meaningful if the GIL is disabled")
@threading_helper.requires_working_threading()
def test_racing_recursion_limit(self):
from threading import Thread
def something_recursive():
def count(n):
if n > 0:
return count(n - 1) + 1
return 0

count(50)

def set_recursion_limit():
for limit in range(100, 200):
sys.setrecursionlimit(limit)

threads = []
for _ in range(5):
threads.append(Thread(target=set_recursion_limit))

for _ in range(5):
threads.append(Thread(target=something_recursive))

with threading_helper.start_threads(threads):
with threading_helper.catch_threading_exception() as cm:
kumaraditya303 marked this conversation as resolved.
Show resolved Hide resolved
if cm.exc_value:
raise cm.exc_value

def test_getwindowsversion(self):
# Raise SkipTest if sys doesn't have getwindowsversion attribute
test.support.get_attribute(sys, "getwindowsversion")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix a crash when setting the recursion limit while other threads are active
on the :term:`free threaded <free threading>` build.
2 changes: 2 additions & 0 deletions Python/ceval.c
Original file line number Diff line number Diff line change
Expand Up @@ -265,12 +265,14 @@ void
Py_SetRecursionLimit(int new_limit)
{
PyInterpreterState *interp = _PyInterpreterState_GET();
_PyEval_StopTheWorld(interp);
interp->ceval.recursion_limit = new_limit;
for (PyThreadState *p = interp->threads.head; p != NULL; p = p->next) {
kumaraditya303 marked this conversation as resolved.
Show resolved Hide resolved
int depth = p->py_recursion_limit - p->py_recursion_remaining;
p->py_recursion_limit = new_limit;
p->py_recursion_remaining = new_limit - depth;
}
_PyEval_StartTheWorld(interp);
}

/* The function _Py_EnterRecursiveCallTstate() only calls _Py_CheckRecursiveCall()
Expand Down
Loading