8000 gh-128717: Stop-the-world when setting the recursion limit (#128741) · python/cpython@f6c61bf · GitHub
[go: up one dir, main page]

Skip to content

Commit f6c61bf

Browse files
gh-128717: Stop-the-world when setting the recursion limit (#128741)
1 parent ff39e3f commit f6c61bf

File tree

3 files changed

+19
-0
lines changed

3 files changed

+19
-0
lines changed

Lib/test/test_free_threading/test_races.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,21 @@ def mutate():
270270

271271
do_race(set_value, mutate)
272272

273+
def test_racing_recursion_limit(self):
274+
def something_recursive():
275+
def count(n):
276+
if n > 0:
277+
return count(n - 1) + 1
278+
return 0
279+
280+
count(50)
281+
282+
def set_recursion_limit():
283+
for limit in range(100, 200):
284+
sys.setrecursionlimit(limit)
285+
286+
do_race(something_recursive, set_recursion_limit)
287+
273288

274289
if __name__ == "__main__":
275290
unittest.main()
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix a crash when setting the recursion limit while other threads are active
2+
on the :term:`free threaded <free threading>` build.

Python/ceval.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,13 +294,15 @@ void
294294
Py_SetRecursionLimit(int new_limit)
295295
{
296296
PyInterpreterState *interp = _PyInterpreterState_GET();
297+
_PyEval_StopTheWorld(interp);
297298
interp->ceval.recursion_limit = new_limit;
298299
_Py_FOR_EACH_TSTATE_BEGIN(interp, p) {
299300
int depth = p->py_recursion_limit - p->py_recursion_remaining;
300301
p->py_recursion_limit = new_limit;
301302
p->py_recursion_remaining = new_limit - depth;
302303
}
303304
_Py_FOR_EACH_TSTATE_END(interp);
305+
_PyEval_StartTheWorld(interp);
304306
}
305307

306308
/* The function _Py_EnterRecursiveCallTstate() only calls _Py_CheckRecursiveCall()

0 commit comments

Comments
 (0)
0