Skip to content
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-46309: Added reference to task created by StreamReaderProtocol #30505

Closed
wants to merge 5 commits into from
Closed

bpo-46309: Added reference to task created by StreamReaderProtocol #30505

wants to merge 5 commits into from

Conversation

simwr872
Copy link

@simwr872 simwr872 commented Jan 9, 2022

No reference was kept to a task created by StreamReaderProtocol and was therefore eligible for garbage collection under certain circumstances.
https://bugs.python.org/issue46309

https://bugs.python.org/issue46309

@the-knights-who-say-ni

This comment was marked as outdated.

Copy link
Contributor

@bharel bharel left a comment

Choose a reason for hiding this comment

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

Welcome to CPython's repo! Thank you for your contribution. As written by our lovely bot, please sign the CLA agreement.
I've added a few changes to your code. Right now the task will be replaced in case of multiple concurrent connections which is not ideal. I've created a set to hold the tasks and added a callback to remove them once done.

Lib/asyncio/streams.py Show resolved Hide resolved
Lib/asyncio/streams.py Show resolved Hide resolved
Comment on lines 808 to 810

async def callback(*args):
await self.loop.create_future()

This comment was marked as resolved.

Copy link
Author

Choose a reason for hiding this comment

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

Thanks for looking at the changes! I signed the CLA ~3 hours before this PR, so hopefully it will be OK'd sometime today. Your requested changes makes sense. However from my testing, creating the future outside of the callback will not allow the task to be garbage collected. That is, the test will pass even without keeping a reference to the task in StreamReaderProtocol.

One solution could be to still create the future inside the callback, but cancel the task via the newly created reference in StreamReaderProtocol. Perhaps you have a better suggestion. I can spend some more time on this later today.

Copy link
Contributor

Choose a reason for hiding this comment

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

You are completely correct. I've spent quite a few hours triaging the source of the original issue on stackoverflow and realized the bug is caused when the garbage collector breaks a cyclic reference between the writer, the reader, the task and a future created inside the reader. If any of them are referenced outside of the function, the bug will not appear.

I don't suggest accessing the internals from the test. You're welcome to decline my changes in the tests as long as asyncio doesn't throw any warning about the leak. The garbage collector will take care of it when the loop is closed, or it will be cancelled accordingly.

Copy link
Author

@simwr872 simwr872 Jan 10, 2022

Choose a reason for hiding this comment

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

I've accepted your suggested changes in streams.py. As for the leaking test callback, a different solution could be to use asyncio.all_tasks(self.loop).pop().cancel() right after the assertion.

Copy link
Contributor

@bharel bharel Jan 11, 2022

Choose a reason for hiding this comment

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

I believe it's unnecessary - It will cause more confusion, and will get bugged if the internals will change and a new task will be created in the background.

I think we're better off letting the cleanup take care of it.

protocol.connection_made(transport)
await asyncio.sleep(0)
gc.collect()

This comment was marked as resolved.

Copy link
Contributor

@bharel bharel left a comment

Choose a reason for hiding this comment

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

Changes requested.

Co-authored-by: Bar Harel <bzvi7919@gmail.com>
Copy link
Contributor

@bharel bharel left a comment

Choose a reason for hiding this comment

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

Looks perfect.

As a side note, the tests contain only one task and not multiple tasks created at once. That might be preferable - the code being tested tested is simple and goes through the same path for both single and multiple tasks. In turn, it allows the test itself to be simpler as well and achieve good coverage.

Thank you for your contribution @simwr872!

@1st1, @asvetlov ready for core-dev :-)

@github-actions
Copy link

This PR is stale because it has been open for 30 days with no activity.

@github-actions github-actions bot added the stale Stale PR or inactive for long period of time. label Feb 11, 2022
@gvanrossum gvanrossum self-assigned this Aug 26, 2022
@gvanrossum gvanrossum self-requested a review August 26, 2022 22:24
Copy link
Member

@gvanrossum gvanrossum left a comment

Choose a reason for hiding this comment

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

This appears to be superseded by GH-96323. The latter PR is simpler, it doesn't collect multiple tasks in a set, it just stores a single task, because connection_made() is only ever called once per StreamReader instance. Because it's so simple it doesn't add a test. (The test here seems controversial among past reviewers anyway.)

If anyone here doesn't agree with my approving and merging GH-96323 please let me know!

@cpython-cla-bot
Copy link

The following commit authors need to sign the Contributor License Agreement:

Click the button to sign:
CLA not signed

@simwr872
Copy link
Author

This appears to be superseded by GH-96323. The latter PR is simpler, it doesn't collect multiple tasks in a set, it just stores a single task, because connection_made() is only ever called once per StreamReader instance. Because it's so simple it doesn't add a test. (The test here seems controversial among past reviewers anyway.)

If anyone here doesn't agree with my approving and merging GH-96323 please let me know!

Seems ok.

@simwr872 simwr872 closed this Aug 27, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
awaiting core review stale Stale PR or inactive for long period of time.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants