[3.9] bpo-45806: Fix recovery from stack overflow for 3.9. Again. (GH-29640)

Co-authored-by: Łukasz Langa <lukasz@langa.pl>
This commit is contained in:
Mark Shannon 2021-11-19 18:51:50 +00:00 committed by GitHub
parent c06c7c489a
commit 4296396db0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 69 additions and 63 deletions

View file

@ -797,19 +797,21 @@ _Py_CheckRecursiveCall(PyThreadState *tstate, const char *where)
/* Somebody asked that we don't check for recursion. */
return 0;
if (tstate->overflowed) {
if (tstate->recursion_depth > recursion_limit + 50) {
if (tstate->recursion_depth > recursion_limit + 50 || tstate->overflowed > 50) {
/* Overflowing while handling an overflow. Give up. */
Py_FatalError("Cannot recover from stack overflow.");
}
return 0;
}
if (tstate->recursion_depth > recursion_limit) {
--tstate->recursion_depth;
tstate->overflowed = 1;
_PyErr_Format(tstate, PyExc_RecursionError,
"maximum recursion depth exceeded%s",
where);
return -1;
else {
if (tstate->recursion_depth > recursion_limit) {
tstate->overflowed++;
_PyErr_Format(tstate, PyExc_RecursionError,
"maximum recursion depth exceeded%s",
where);
tstate->overflowed--;
--tstate->recursion_depth;
return -1;
}
}
return 0;
}

View file

@ -317,12 +317,14 @@ _PyErr_NormalizeException(PyThreadState *tstate, PyObject **exc,
PyObject **val, PyObject **tb)
{
int recursion_depth = 0;
tstate->overflowed++;
PyObject *type, *value, *initial_tb;
restart:
type = *exc;
if (type == NULL) {
/* There was no exception, so nothing to do. */
tstate->overflowed--;
return;
}
@ -374,6 +376,7 @@ _PyErr_NormalizeException(PyThreadState *tstate, PyObject **exc,
}
*exc = type;
*val = value;
tstate->overflowed--;
return;
error:

View file

@ -17,7 +17,7 @@ Data members:
#include "Python.h"
#include "code.h"
#include "frameobject.h" // PyFrame_GetBack()
#include "pycore_ceval.h" // _Py_RecursionLimitLowerWaterMark()
#include "pycore_ceval.h"
#include "pycore_initconfig.h"
#include "pycore_object.h"
#include "pycore_pathconfig.h"
@ -1160,7 +1160,6 @@ static PyObject *
sys_setrecursionlimit_impl(PyObject *module, int new_limit)
/*[clinic end generated code: output=35e1c64754800ace input=b0f7a23393924af3]*/
{
int mark;
PyThreadState *tstate = _PyThreadState_GET();
if (new_limit < 1) {
@ -1178,8 +1177,7 @@ sys_setrecursionlimit_impl(PyObject *module, int new_limit)
Reject too low new limit if the current recursion depth is higher than
the new low-water mark. Otherwise it may not be possible anymore to
reset the overflowed flag to 0. */
mark = _Py_RecursionLimitLowerWaterMark(new_limit);
if (tstate->recursion_depth >= mark) {
if (tstate->recursion_depth >= new_limit) {
_PyErr_Format(tstate, PyExc_RecursionError,
"cannot set the recursion limit to %i at "
"the recursion depth %i: the limit is too low",