Repository navigation
bpo-46309: Added reference to task created by StreamReaderProtocol #30505
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 1 commit
62cdd31
11cc789
d1586c5
2a27d0c
2024570
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 |
|---|---|---|
|
|
@@ -801,6 +801,25 @@ def test_streamreaderprotocol_constructor_use_global_loop(self): | |
| self.assertEqual(cm.warnings[0].filename, __file__) | ||
| self.assertIs(protocol._loop, self.loop) | ||
|
|
||
| def test_streamreaderprotocol_task_reference(self): | ||
| # See http://bugs.python.org/issue46309 | ||
| messages = [] | ||
| self.loop.set_exception_handler(lambda loop, ctx: messages.append(ctx)) | ||
|
|
||
| async def callback(*args): | ||
| await self.loop.create_future() | ||
This comment was marked as resolved.
Sorry, something went wrong.
Author
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. 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 One solution could be to still create the future inside the callback, but cancel the task via the newly created reference in
Contributor
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. 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.
Author
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. I've accepted your suggested changes in
Contributor
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. 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. |
||
|
|
||
| async def test(): | ||
| reader = asyncio.StreamReader() | ||
| protocol = asyncio.StreamReaderProtocol(reader, callback) | ||
| transport = mock.Mock() | ||
| protocol.connection_made(transport) | ||
| await asyncio.sleep(0) | ||
| gc.collect() | ||
|
|
||
This comment was marked as resolved.
Sorry, something went wrong. |
||
| self.loop.run_until_complete(test()) | ||
| self.assertEqual(messages, []) | ||
|
|
||
| def test_drain_raises(self): | ||
| # See http://bugs.python.org/issue25441 | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| Added reference to task created in | ||
| :meth:`asyncio.streams.StreamReaderProtocol.connection_made`. |
Uh oh!
There was an error while loading. Please reload this page.