Skip to content

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

Merged

Conversation

twisteroidambassador
Copy link
Contributor

@twisteroidambassador twisteroidambassador commented Oct 5, 2018

Should fix bpo-34769 in debug mode.

https://bugs.python.org/issue34769

@1st1
Copy link
Member

1st1 commented Oct 5, 2018

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.
@twisteroidambassador
Copy link
Contributor Author

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.
Copy link
Contributor Author

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?

Copy link
Member

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.

@1st1 1st1 removed the DO-NOT-MERGE label Oct 8, 2018
@1st1
Copy link
Member

1st1 commented Oct 8, 2018

Could you please add a NEWS entry asap? We might be able to get this merged into 3.7.1rc2

cc @ned-deily

@twisteroidambassador
Copy link
Contributor Author

OK, I think it's ready.

Copy link
Contributor

@willingc willingc left a 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
Copy link
Contributor

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
Copy link
Contributor

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

Copy link
Contributor

@willingc willingc left a 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.

@willingc willingc self-requested a review October 9, 2018 06:39
@miss-islington
Copy link
Contributor

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

miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Oct 9, 2018
(cherry picked from commit c880ffe)

Co-authored-by: twisteroid ambassador <twisteroidambassador@users.noreply.github.com>
@bedevere-bot
Copy link

GH-9772 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 Oct 9, 2018
(cherry picked from commit c880ffe)

Co-authored-by: twisteroid ambassador <twisteroidambassador@users.noreply.github.com>
@bedevere-bot
Copy link

GH-9773 is a backport of this pull request to the 3.6 branch.

@1st1
Copy link
Member

1st1 commented Oct 9, 2018

@ned-deily Please consider cherry picking this one in 3.7.1rc2

miss-islington added a commit that referenced this pull request Oct 9, 2018
(cherry picked from commit c880ffe)

Co-authored-by: twisteroid ambassador <twisteroidambassador@users.noreply.github.com>
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.

6 participants