Skip to content

bpo-33786: Fix asynchronous generators to handle GeneratorExit in athrow() #7467

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

Merged
merged 3 commits into from
Jun 8, 2018

Conversation

1st1
Copy link
Member

@1st1 1st1 commented Jun 7, 2018

o->agt_state = AWAITABLE_STATE_CLOSED;
if (o->agt_args == NULL) {
/* when aclose() is called we don't want to propagate
StopAsyncIteration; just raise StopIteration, signalling
that 'aclose()' is done. */
StopAsyncIteration or GeneratorExit; just raise
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As far as I understand, this will make .aclose() raise StopIteration in the usual case (when generator doesn't handle the GeneratorExit). What about .athrow(), how will it behave? Shouldn't their behavior be similar to that of .close() and .throw() for sync generators, where .close() swallows the GeneratorExit and .throw() doesn't?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As far as I understand, this will make .aclose() raise StopIteration in the usual case (when generator doesn't handle the GeneratorExit).

Keep in mind, that raising StopIteration from any of a***() methods just means that they are returning. They are all iterators.

What about .athrow(), how will it behave?

The idea here:

  1. athrow() and aclose() share one implementation.

  2. aclose() is basically an athrow(GeneratorExit).

  3. When aclose() is called and the generator propagates GeneratorExit, it means that everything is fine and the aclose() call was successful. We raise StopIteration to make aclose() awaitable return.

  4. When athrow() is called we don't want to trap any exceptions at all. So we raise StopIteration only if the asynchronous generator swallowed the thrown error and yielded (asynchronously).

This is a bit complex, I know. I explained how asynchronous generators work in detail here: https://www.python.org/dev/peps/pep-0525/#implementation-details

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, now I get it

Copy link
Contributor

@asvetlov asvetlov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good.
Would you apply the following test to test_contexlib_async.py and check explicitly is the problem gone

 
+    @_async_test
+    async def test_async_gen(self):
+        @asynccontextmanager
+        async def ctx():
+            yield
+
+        async def gen():
+            async with ctx():
+                yield 11
+
+        ret = []
+        exc = ValueError(22)
+        with self.assertRaises(ValueError):
+            async with ctx():
+                async for val in gen():
+                    ret.append(val)
+                    raise exc
+
+        self.assertEqual(ret, [11])
+

@1st1
Copy link
Member Author

1st1 commented Jun 7, 2018

Would you apply the following test to test_contexlib_async.py and check explicitly is the problem gone

Good idea. I added your regression test.

@1st1 1st1 merged commit 52698c7 into python:master Jun 8, 2018
@miss-islington
Copy link
Contributor

Thanks @1st1 for the PR 🌮🎉.. I'm working now to backport this PR to: 3.7.
🐍🍒⛏🤖

@1st1 1st1 deleted the fix_ag branch June 8, 2018 00:31
miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Jun 8, 2018
…row() (pythonGH-7467)

(cherry picked from commit 52698c7)

Co-authored-by: Yury Selivanov <yury@magic.io>
@bedevere-bot
Copy link

GH-7506 is a backport of this pull request to the 3.7 branch.

@miss-islington
Copy link
Contributor

Thanks @1st1 for the PR 🌮🎉.. I'm working now to backport this PR to: 3.7.
🐍🍒⛏🤖

@bedevere-bot
Copy link

GH-21878 is a backport of this pull request to the 3.7 branch.

miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Aug 14, 2020
…row() (pythonGH-7467)

(cherry picked from commit 52698c7)

Co-authored-by: Yury Selivanov <yury@magic.io>
ned-deily pushed a commit that referenced this pull request Aug 14, 2020
…row() (GH-7467) (GH-21878)

(cherry picked from commit 52698c7)

Co-authored-by: Yury Selivanov <yury@magic.io>
tekknolagi pushed a commit to facebookarchive/skybison that referenced this pull request Aug 26, 2021
Summary: This functionality was changed in python/cpython#7467.

Based on Facebook D27940847
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants