Skip to content

Close sub-generators even if top level generator was asynchronous. #96040

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

Closed
Closed
Changes from all 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
37 changes: 16 additions & 21 deletions Objects/genobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -421,7 +421,7 @@ Raise exception in generator, return next yielded value or raise\n\
StopIteration.");

static PyObject *
_gen_throw(PyGenObject *gen, int close_on_genexit,
_gen_throw(PyGenObject *gen,
PyObject *typ, PyObject *val, PyObject *tb)
{
PyObject *yf = _PyGen_yf(gen);
Expand All @@ -430,21 +430,21 @@ _gen_throw(PyGenObject *gen, int close_on_genexit,
_PyInterpreterFrame *frame = (_PyInterpreterFrame *)gen->gi_iframe;
PyObject *ret;
int err;
if (PyErr_GivenExceptionMatches(typ, PyExc_GeneratorExit) &&
close_on_genexit
) {
if (PyErr_GivenExceptionMatches(typ, PyExc_GeneratorExit)) {
/* Asynchronous generators *should not* be closed right away.
We have to allow some awaits to work it through, hence the
`close_on_genexit` parameter here.
We have to allow some awaits to work it through, hence the
`close_on_genexit` parameter here.
*/
PyFrameState state = gen->gi_frame_state;
gen->gi_frame_state = FRAME_EXECUTING;
err = gen_close_iter(yf);
gen->gi_frame_state = state;
Py_DECREF(yf);
if (err < 0)
return gen_send_ex(gen, Py_None, 1, 0);
goto throw_here;
if(!PyAsyncGen_CheckExact(gen)) {
PyFrameState state = gen->gi_frame_state;
gen->gi_frame_state = FRAME_EXECUTING;
err = gen_close_iter(yf);
gen->gi_frame_state = state;
Py_DECREF(yf);
if (err < 0)
return gen_send_ex(gen, Py_None, 1, 0);
goto throw_here;
}
}
if (PyGen_CheckExact(yf) || PyCoro_CheckExact(yf)) {
/* `yf` is a generator or a coroutine. */
Expand All @@ -461,8 +461,7 @@ _gen_throw(PyGenObject *gen, int close_on_genexit,
'yield from' or awaiting on with 'await'. */
PyFrameState state = gen->gi_frame_state;
gen->gi_frame_state = FRAME_EXECUTING;
ret = _gen_throw((PyGenObject *)yf, close_on_genexit,
typ, val, tb);
ret = _gen_throw((PyGenObject *)yf, typ, val, tb);
gen->gi_frame_state = state;
tstate->cframe->current_frame = prev;
frame->previous = NULL;
Expand Down Expand Up @@ -586,7 +585,7 @@ gen_throw(PyGenObject *gen, PyObject *const *args, Py_ssize_t nargs)
else if (nargs == 2) {
val = args[1];
}
return _gen_throw(gen, 1, typ, val, tb);
return _gen_throw(gen, typ, val, tb);
}


Expand Down Expand Up @@ -2119,8 +2118,6 @@ async_gen_athrow_send(PyAsyncGenAThrow *o, PyObject *arg)
o->agt_gen->ag_closed = 1;

retval = _gen_throw((PyGenObject *)gen,
0, /* Do not close generator when
PyExc_GeneratorExit is passed */
PyExc_GeneratorExit, NULL, NULL);

if (retval && _PyAsyncGenWrappedValue_CheckExact(retval)) {
Expand All @@ -2138,8 +2135,6 @@ async_gen_athrow_send(PyAsyncGenAThrow *o, PyObject *arg)
}

retval = _gen_throw((PyGenObject *)gen,
0, /* Do not close generator when
PyExc_GeneratorExit is passed */
typ, val, tb);
retval = async_gen_unwrap_value(o->agt_gen, retval);
}
Expand Down