Skip to content

Commit 4296396

Browse files
markshannonambv
andauthored
[3.9] bpo-45806: Fix recovery from stack overflow for 3.9. Again. (GH-29640)
Co-authored-by: Łukasz Langa <[email protected]>
1 parent c06c7c4 commit 4296396

File tree

7 files changed

+69
-63
lines changed

7 files changed

+69
-63
lines changed

Include/internal/pycore_ceval.h

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -90,24 +90,8 @@ static inline int _Py_EnterRecursiveCall_inline(const char *where) {
9090

9191
#define Py_EnterRecursiveCall(where) _Py_EnterRecursiveCall_inline(where)
9292

93-
/* Compute the "lower-water mark" for a recursion limit. When
94-
* Py_LeaveRecursiveCall() is called with a recursion depth below this mark,
95-
* the overflowed flag is reset to 0. */
96-
static inline int _Py_RecursionLimitLowerWaterMark(int limit) {
97-
if (limit > 200) {
98-
return (limit - 50);
99-
}
100-
else {
101-
return (3 * (limit >> 2));
102-
}
103-
}
104-
10593
static inline void _Py_LeaveRecursiveCall(PyThreadState *tstate) {
10694
tstate->recursion_depth--;
107-
int limit = tstate->interp->ceval.recursion_limit;
108-
if (tstate->recursion_depth < _Py_RecursionLimitLowerWaterMark(limit)) {
109-
tstate->overflowed = 0;
110-
}
11195
}
11296

11397
static inline void _Py_LeaveRecursiveCall_inline(void) {

Lib/test/test_exceptions.py

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1213,7 +1213,7 @@ def gen():
12131213
# tstate->recursion_depth is equal to (recursion_limit - 1)
12141214
# and is equal to recursion_limit when _gen_throw() calls
12151215
# PyErr_NormalizeException().
1216-
recurse(setrecursionlimit(depth + 2) - depth - 1)
1216+
recurse(setrecursionlimit(depth + 2) - depth)
12171217
finally:
12181218
sys.setrecursionlimit(recursionlimit)
12191219
print('Done.')
@@ -1243,6 +1243,52 @@ def test_recursion_normalizing_infinite_exception(self):
12431243
b'while normalizing an exception', err)
12441244
self.assertIn(b'Done.', out)
12451245

1246+
def test_recursion_in_except_handler(self):
1247+
1248+
def set_relative_recursion_limit(n):
1249+
depth = 1
1250+
while True:
1251+
try:
1252+
sys.setrecursionlimit(depth)
1253+
except RecursionError:
1254+
depth += 1
1255+
else:
1256+
break
1257+
sys.setrecursionlimit(depth+n)
1258+
1259+
def recurse_in_except():
1260+
try:
1261+
1/0
1262+
except:
1263+
recurse_in_except()
1264+
1265+
def recurse_after_except():
1266+
try:
1267+
1/0
1268+
except:
1269+
pass
1270+
recurse_after_except()
1271+
1272+
def recurse_in_body_and_except():
1273+
try:
1274+
recurse_in_body_and_except()
1275+
except:
1276+
recurse_in_body_and_except()
1277+
1278+
recursionlimit = sys.getrecursionlimit()
1279+
try:
1280+
set_relative_recursion_limit(10)
1281+
for func in (recurse_in_except, recurse_after_except, recurse_in_body_and_except):
1282+
with self.subTest(func=func):
1283+
try:
1284+
func()
1285+
except RecursionError:
1286+
pass
1287+
else:
1288+
self.fail("Should have raised a RecursionError")
1289+
finally:
1290+
sys.setrecursionlimit(recursionlimit)
1291+
12461292
@cpython_only
12471293
def test_recursion_normalizing_with_no_memory(self):
12481294
# Issue #30697. Test that in the abort that occurs when there is no

Lib/test/test_sys.py

Lines changed: 1 addition & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -260,42 +260,10 @@ def set_recursion_limit_at_depth(depth, limit):
260260
sys.setrecursionlimit(1000)
261261

262262
for limit in (10, 25, 50, 75, 100, 150, 200):
263-
# formula extracted from _Py_RecursionLimitLowerWaterMark()
264-
if limit > 200:
265-
depth = limit - 50
266-
else:
267-
depth = limit * 3 // 4
268-
set_recursion_limit_at_depth(depth, limit)
263+
set_recursion_limit_at_depth(limit, limit)
269264
finally:
270265
sys.setrecursionlimit(oldlimit)
271266

272-
# The error message is specific to CPython
273-
@test.support.cpython_only
274-
def test_recursionlimit_fatalerror(self):
275-
# A fatal error occurs if a second recursion limit is hit when recovering
276-
# from a first one.
277-
code = textwrap.dedent("""
278-
import sys
279-
280-
def f():
281-
try:
282-
f()
283-
except RecursionError:
284-
f()
285-
286-
sys.setrecursionlimit(%d)
287-
f()""")
288-
with test.support.SuppressCrashReport():
289-
for i in (50, 1000):
290-
sub = subprocess.Popen([sys.executable, '-c', code % i],
291-
stderr=subprocess.PIPE)
292-
err = sub.communicate()[1]
293-
self.assertTrue(sub.returncode, sub.returncode)
294-
self.assertIn(
295-
b"Fatal Python error: _Py_CheckRecursiveCall: "
296-
b"Cannot recover from stack overflow",
297-
err)
298-
299267
def test_getwindowsversion(self):
300268
# Raise SkipTest if sys doesn't have getwindowsversion attribute
301269
test.support.get_attribute(sys, "getwindowsversion")
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Re-introduced fix that allows recovery from stack overflow without crashing
2+
the interpreter. The original fix as part of :issue:`42500` was reverted
3+
(see release notes for Python 3.9.4) since it introduced an ABI change in a
4+
bugfix release which is not allowed. The new fix doesn't introduce any ABI
5+
changes. Patch by Mark Shannon.

Python/ceval.c

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -797,19 +797,21 @@ _Py_CheckRecursiveCall(PyThreadState *tstate, const char *where)
797797
/* Somebody asked that we don't check for recursion. */
798798
return 0;
799799
if (tstate->overflowed) {
800-
if (tstate->recursion_depth > recursion_limit + 50) {
800+
if (tstate->recursion_depth > recursion_limit + 50 || tstate->overflowed > 50) {
801801
/* Overflowing while handling an overflow. Give up. */
802802
Py_FatalError("Cannot recover from stack overflow.");
803803
}
804-
return 0;
805804
}
806-
if (tstate->recursion_depth > recursion_limit) {
807-
--tstate->recursion_depth;
808-
tstate->overflowed = 1;
809-
_PyErr_Format(tstate, PyExc_RecursionError,
810-
"maximum recursion depth exceeded%s",
811-
where);
812-
return -1;
805+
else {
806+
if (tstate->recursion_depth > recursion_limit) {
807+
tstate->overflowed++;
808+
_PyErr_Format(tstate, PyExc_RecursionError,
809+
"maximum recursion depth exceeded%s",
810+
where);
811+
tstate->overflowed--;
812+
--tstate->recursion_depth;
813+
return -1;
814+
}
813815
}
814816
return 0;
815817
}

Python/errors.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,12 +317,14 @@ _PyErr_NormalizeException(PyThreadState *tstate, PyObject **exc,
317317
PyObject **val, PyObject **tb)
318318
{
319319
int recursion_depth = 0;
320+
tstate->overflowed++;
320321
PyObject *type, *value, *initial_tb;
321322

322323
restart:
323324
type = *exc;
324325
if (type == NULL) {
325326
/* There was no exception, so nothing to do. */
327+
tstate->overflowed--;
326328
return;
327329
}
328330

@@ -374,6 +376,7 @@ _PyErr_NormalizeException(PyThreadState *tstate, PyObject **exc,
374376
}
375377
*exc = type;
376378
*val = value;
379+
tstate->overflowed--;
377380
return;
378381

379382
error:

Python/sysmodule.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ Data members:
1717
#include "Python.h"
1818
#include "code.h"
1919
#include "frameobject.h" // PyFrame_GetBack()
20-
#include "pycore_ceval.h" // _Py_RecursionLimitLowerWaterMark()
20+
#include "pycore_ceval.h"
2121
#include "pycore_initconfig.h"
2222
#include "pycore_object.h"
2323
#include "pycore_pathconfig.h"
@@ -1160,7 +1160,6 @@ static PyObject *
11601160
sys_setrecursionlimit_impl(PyObject *module, int new_limit)
11611161
/*[clinic end generated code: output=35e1c64754800ace input=b0f7a23393924af3]*/
11621162
{
1163-
int mark;
11641163
PyThreadState *tstate = _PyThreadState_GET();
11651164

11661165
if (new_limit < 1) {
@@ -1178,8 +1177,7 @@ sys_setrecursionlimit_impl(PyObject *module, int new_limit)
11781177
Reject too low new limit if the current recursion depth is higher than
11791178
the new low-water mark. Otherwise it may not be possible anymore to
11801179
reset the overflowed flag to 0. */
1181-
mark = _Py_RecursionLimitLowerWaterMark(new_limit);
1182-
if (tstate->recursion_depth >= mark) {
1180+
if (tstate->recursion_depth >= new_limit) {
11831181
_PyErr_Format(tstate, PyExc_RecursionError,
11841182
"cannot set the recursion limit to %i at "
11851183
"the recursion depth %i: the limit is too low",

0 commit comments

Comments
 (0)