-
-
Notifications
You must be signed in to change notification settings - Fork 32k
gh-117536: Fix asyncio _asyncgen_finalizer_hook() #117751
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 | ||||||
---|---|---|---|---|---|---|---|---|
|
@@ -1044,6 +1044,43 @@ def test_asyncgen_finalization_by_gc_in_other_thread(self): | |||||||
test_utils.run_briefly(self.loop) | ||||||||
self.assertTrue(status['finalized']) | ||||||||
|
||||||||
def test_shutdown_asyncgens(self): | ||||||||
# gh-117536: Test shutdown_asyncgens() using asyncio.run() which | ||||||||
# may cancel the task which closes the asynchronous generator before | ||||||||
# calling shutdown_asyncgens(). | ||||||||
|
||||||||
ns = {'state': 'not started'} | ||||||||
async def agen_basic(): | ||||||||
try: | ||||||||
ns['state'] = 'started' | ||||||||
yield 0 | ||||||||
yield 1 | ||||||||
finally: | ||||||||
ns['state'] = 'finalized' | ||||||||
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. This should await something to prove it's getting aclosed properly
Suggested change
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. Right. I added a second test. I prefer to have two tests, since having 'await' also changes the behavior in a subtle way. |
||||||||
|
||||||||
async def reproducer(agen): | ||||||||
async for item in agen(): | ||||||||
break | ||||||||
|
||||||||
asyncio.run(reproducer(agen_basic)) | ||||||||
self.assertEqual(ns['state'], 'finalized') | ||||||||
|
||||||||
# Similar than previous test, but the generator uses 'await' in the | ||||||||
# finally block. | ||||||||
ns['state'] = 'not started' | ||||||||
async def agen_await(): | ||||||||
try: | ||||||||
ns['state'] = 'started' | ||||||||
yield 0 | ||||||||
yield 1 | ||||||||
finally: | ||||||||
ns['state'] = 'await' | ||||||||
await asyncio.sleep(0) | ||||||||
ns['state'] = 'finalized' | ||||||||
|
||||||||
asyncio.run(reproducer(agen_await)) | ||||||||
self.assertEqual(ns['state'], 'finalized') | ||||||||
|
||||||||
|
||||||||
class MyProto(asyncio.Protocol): | ||||||||
done = None | ||||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
:mod:`asyncio`: Make the finalization of asynchronous generators more | ||
reliable. Patch by Victor Stinner. |
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.
this is just side-stepping the actual bug by delaying the creation of the async_generator_aclose object to avoid the incorrect warning