bpo-32526: Closing async generator while it is running does not raise an exception by achimnol · Pull Request #5182 · python/cpython

@achimnol

Here are my first draft PR to pass the async_generator test case provided by @njsmith.
I have reset gi_running value to 1 after gen_send_ex() and _gen_throw() calls in async generators and clear it to 0 when they close.
This PR obviously requires more test cases regarding athrow() calls, but I submit at this stage to check if my approach is correct first.

https://bugs.python.org/issue32526

@achimnol

@achimnol

@the-knights-who-say-ni

Hello, and thanks for your contribution!

I'm a bot set up to make sure that the project can legally accept your contribution by verifying you have signed the PSF contributor agreement (CLA).

Unfortunately our records indicate you have not signed the CLA. For legal reasons we need you to sign this before we can look at your contribution. Please follow the steps outlined in the CPython devguide to rectify this issue.

Thanks again to your contribution and we look forward to looking at it!

@achimnol

Hm.. my changes makes too many existing tests (test_asyncgen) failing except the new test case. 😱

Async generators awaiting its inner awaitables should ALLOW send() on its returned async iterator object (as the current Python does now but gi_running becomse False) while should FORBID another asend() on itself from outside. This PR forbids both cases and thus breaks. 😞
It seems non-trivial to fix this situation... I need deeper understanding on how async-generator is implemented in CPython (coroutines and generators individually seems to be not very difficult but it looks like that async-generator mixes them up...).

I hope @1st1 and @njsmith would have some better ideas than me!
At least, my try will help me understand what's going on there even if somebody else fixes this issue.

@njsmith

@1st1

I'll take a look tomorrow.