-
-
Notifications
You must be signed in to change notification settings - Fork 32.4k
bpo-39606: allow closing async generators that are already closed #18475
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Fix regression caused by fix for bpo-39386, that prevented calling | ||
``aclose`` on an async generator that had already been closed or exhausted. |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1797,16 +1797,22 @@ async_gen_athrow_send(PyAsyncGenAThrow *o, PyObject *arg) | |
PyFrameObject *f = gen->gi_frame; | ||
PyObject *retval; | ||
|
||
if (f == NULL || f->f_stacktop == NULL || | ||
o->agt_state == AWAITABLE_STATE_CLOSED) { | ||
if (o->agt_state == AWAITABLE_STATE_CLOSED) { | ||
PyErr_SetString( | ||
PyExc_RuntimeError, | ||
"cannot reuse already awaited aclose()/athrow()"); | ||
return NULL; | ||
} | ||
|
||
if (f == NULL || f->f_stacktop == NULL) { | ||
o->agt_state = AWAITABLE_STATE_CLOSED; | ||
PyErr_SetNone(PyExc_StopIteration); | ||
return NULL; | ||
} | ||
|
||
if (o->agt_state == AWAITABLE_STATE_INIT) { | ||
if (o->agt_gen->ag_running_async) { | ||
o->agt_state = AWAITABLE_STATE_CLOSED; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If the |
||
if (o->agt_args == NULL) { | ||
PyErr_SetString( | ||
PyExc_RuntimeError, | ||
|
@@ -1878,7 +1884,6 @@ async_gen_athrow_send(PyAsyncGenAThrow *o, PyObject *arg) | |
/* aclose() mode */ | ||
if (retval) { | ||
if (_PyAsyncGenWrappedValue_CheckExact(retval)) { | ||
o->agt_gen->ag_running_async = 0; | ||
Py_DECREF(retval); | ||
goto yield_close; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
} | ||
|
@@ -1893,16 +1898,17 @@ async_gen_athrow_send(PyAsyncGenAThrow *o, PyObject *arg) | |
|
||
yield_close: | ||
o->agt_gen->ag_running_async = 0; | ||
o->agt_state = AWAITABLE_STATE_CLOSED; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
PyErr_SetString( | ||
PyExc_RuntimeError, ASYNC_GEN_IGNORED_EXIT_MSG); | ||
return NULL; | ||
|
||
check_error: | ||
o->agt_gen->ag_running_async = 0; | ||
o->agt_state = AWAITABLE_STATE_CLOSED; | ||
if (PyErr_ExceptionMatches(PyExc_StopAsyncIteration) || | ||
PyErr_ExceptionMatches(PyExc_GeneratorExit)) | ||
{ | ||
o->agt_state = AWAITABLE_STATE_CLOSED; | ||
if (o->agt_args == NULL) { | ||
/* when aclose() is called we don't want to propagate | ||
StopAsyncIteration or GeneratorExit; just raise | ||
|
@@ -1936,6 +1942,7 @@ async_gen_athrow_throw(PyAsyncGenAThrow *o, PyObject *args) | |
/* aclose() mode */ | ||
if (retval && _PyAsyncGenWrappedValue_CheckExact(retval)) { | ||
o->agt_gen->ag_running_async = 0; | ||
o->agt_state = AWAITABLE_STATE_CLOSED; | ||
Py_DECREF(retval); | ||
PyErr_SetString(PyExc_RuntimeError, ASYNC_GEN_IGNORED_EXIT_MSG); | ||
return NULL; | ||
|
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These two hunks are the main change. Originally, this code raised
StopIteration
if the generator had no frame. The fix for bpo-39386 added the check for the athrow coroutine being inAWAITABLE_STATE_CLOSED
to the sameif
statement, and switched it to raiseRuntimeError
in both cases. But in fact, the generator having no frame should raiseStopIteration
; it's only theAWAITABLE_STATE_CLOSED
case that should raiseRuntimeError
. So I split theAWAITABLE_STATE_CLOSED
logic out into its own block, and restored the oldif (f == NULL || f->f_stacktop)
logic.Everything else is just going through to make sure that we're setting
AWAITABLE_STATE_CLOSED
at the right times.