-
-
Notifications
You must be signed in to change notification settings - Fork 32.3k
bpo-34769: Thread safety for _asyncgen_finalizer_hook(). #9716
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
bpo-34769: Thread safety for _asyncgen_finalizer_hook(). #9716
Conversation
Should fix bpo-34769 in debug mode.
Thanks! Let's try to figure out a test for this. |
Tests whether async generators are finalized correctly when garbage collected in the same thread.
There doesn't seem to be tests for _asyncgen_finalizer_hook in general, so I have added one for the normal case: when an async generator is garbage collected in the same thread. I'll add one for the bugged case next. |
async def leave_unfinalized_asyncgen(self): | ||
# The following should create an async generator, iterate | ||
# it partially, and leave it to be garbage collected | ||
# in the future. |
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 coroutine does what it says now, in cpython. It's probably dependent on how precisely reference counting and garbage collection works, and may stop working in future versions or in other implementations. Is it OK to rely on this behavior in the test below?
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.
I think it's OK, just document this fact.
Could you please add a NEWS entry asap? We might be able to get this merged into 3.7.1rc2 cc @ned-deily |
OK, I think it's ready. |
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.
Suggestions on commenting and small typo
@@ -0,0 +1,2 @@ | |||
Fix for async generators not finalizing when event loop in debug mode and |
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.
...event loop is in...
@@ -926,6 +926,76 @@ def test_run_forever_pre_stopped(self): | |||
self.loop.run_forever() | |||
self.loop._selector.select.assert_called_once_with(0) | |||
|
|||
async def leave_unfinalized_asyncgen(self): | |||
# The following should create an async generator, iterate |
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.
Let's slim down the comments:
# Create an async generator, iterate it partially, and leave it to be garbage collected.
# Used in async generator finalization tests.
# Depends on implementation details of garbage collector. Changes in gc may break this function
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.
Thanks for the changes @twisteroidambassador.
Thanks @twisteroidambassador for the PR, and @1st1 for merging it 🌮🎉.. I'm working now to backport this PR to: 3.6, 3.7. |
(cherry picked from commit c880ffe) Co-authored-by: twisteroid ambassador <twisteroidambassador@users.noreply.github.com>
GH-9772 is a backport of this pull request to the 3.7 branch. |
(cherry picked from commit c880ffe) Co-authored-by: twisteroid ambassador <twisteroidambassador@users.noreply.github.com>
GH-9773 is a backport of this pull request to the 3.6 branch. |
@ned-deily Please consider cherry picking this one in 3.7.1rc2 |
Should fix bpo-34769 in debug mode.
https://bugs.python.org/issue34769