Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 94c2263

Browse files
committed
Issue 24374: Plug refleak in set_coroutine_wrapper
1 parent 53f9502 commit 94c2263

2 files changed

Lines changed: 21 additions & 33 deletions

File tree

Lib/test/test_coroutines.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1006,10 +1006,10 @@ async def wrap(coro):
10061006

10071007
sys.set_coroutine_wrapper(wrapper)
10081008
try:
1009-
with self.assertRaisesRegex(
1009+
with silence_coro_gc(), self.assertRaisesRegex(
10101010
RuntimeError,
10111011
"coroutine wrapper.*\.wrapper at 0x.*attempted to "
1012-
"recursively wrap <coroutine.*\.wrap"):
1012+
"recursively wrap .* wrap .*"):
10131013

10141014
foo()
10151015
finally:

Python/ceval.c

Lines changed: 19 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -146,8 +146,6 @@ static void format_exc_unbound(PyCodeObject *co, int oparg);
146146
static PyObject * unicode_concatenate(PyObject *, PyObject *,
147147
PyFrameObject *, unsigned char *);
148148
static PyObject * special_lookup(PyObject *, _Py_Identifier *);
149-
static PyObject * apply_coroutine_wrapper(PyObject *);
150-
151149

152150
#define NAME_ERROR_MSG \
153151
"name '%.200s' is not defined"
@@ -3923,6 +3921,18 @@ _PyEval_EvalCodeWithName(PyObject *_co, PyObject *globals, PyObject *locals,
39233921

39243922
if (co->co_flags & CO_GENERATOR) {
39253923
PyObject *gen;
3924+
PyObject *coro_wrapper = tstate->coroutine_wrapper;
3925+
int is_coro = co->co_flags & (CO_COROUTINE | CO_ITERABLE_COROUTINE);
3926+
3927+
if (is_coro && tstate->in_coroutine_wrapper) {
3928+
assert(coro_wrapper != NULL);
3929+
PyErr_Format(PyExc_RuntimeError,
3930+
"coroutine wrapper %.200R attempted "
3931+
"to recursively wrap %.200R",
3932+
coro_wrapper,
3933+
co);
3934+
goto fail;
3935+
}
39263936

39273937
/* Don't need to keep the reference to f_back, it will be set
39283938
* when the generator is resumed. */
@@ -3936,8 +3946,13 @@ _PyEval_EvalCodeWithName(PyObject *_co, PyObject *globals, PyObject *locals,
39363946
if (gen == NULL)
39373947
return NULL;
39383948

3939-
if (co->co_flags & (CO_COROUTINE | CO_ITERABLE_COROUTINE))
3940-
return apply_coroutine_wrapper(gen);
3949+
if (is_coro && coro_wrapper != NULL) {
3950+
PyObject *wrapped;
3951+
tstate->in_coroutine_wrapper = 1;
3952+
wrapped = PyObject_CallFunction(coro_wrapper, "N", gen);
3953+
tstate->in_coroutine_wrapper = 0;
3954+
return wrapped;
3955+
}
39413956

39423957
return gen;
39433958
}
@@ -5232,33 +5247,6 @@ unicode_concatenate(PyObject *v, PyObject *w,
52325247
return res;
52335248
}
52345249

5235-
static PyObject *
5236-
apply_coroutine_wrapper(PyObject *gen)
5237-
{
5238-
PyObject *wrapped;
5239-
PyThreadState *tstate = PyThreadState_GET();
5240-
PyObject *wrapper = tstate->coroutine_wrapper;
5241-
5242-
if (tstate->in_coroutine_wrapper) {
5243-
assert(wrapper != NULL);
5244-
PyErr_Format(PyExc_RuntimeError,
5245-
"coroutine wrapper %.200R attempted "
5246-
"to recursively wrap %.200R",
5247-
wrapper,
5248-
gen);
5249-
return NULL;
5250-
}
5251-
5252-
if (wrapper == NULL) {
5253-
return gen;
5254-
}
5255-
5256-
tstate->in_coroutine_wrapper = 1;
5257-
wrapped = PyObject_CallFunction(wrapper, "N", gen);
5258-
tstate->in_coroutine_wrapper = 0;
5259-
return wrapped;
5260-
}
5261-
52625250
#ifdef DYNAMIC_EXECUTION_PROFILE
52635251

52645252
static PyObject *

0 commit comments

Comments
 (0)