-
-
Notifications
You must be signed in to change notification settings - Fork 32.7k
gh-126083: Fix a reference leak in asyncio.Task
when reinitializing with new non-None
context
#126103
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
gh-126083: Fix a reference leak in asyncio.Task
when reinitializing with new non-None
context
#126103
Changes from 3 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 |
---|---|---|
|
@@ -2688,6 +2688,32 @@ def test_get_context(self): | |
finally: | ||
loop.close() | ||
|
||
def test_proper_refcounts(self): | ||
# see: https://github.com/python/cpython/issues/126083 | ||
class Break: | ||
def __str__(self): | ||
raise RuntimeError("break") | ||
|
||
obj = object() | ||
initial_refcount = sys.getrefcount(obj) | ||
|
||
coro = coroutine_function() | ||
loop = asyncio.new_event_loop() | ||
task = asyncio.Task.__new__(asyncio.Task) | ||
|
||
for _ in range(5): | ||
try: | ||
task.__init__(coro, loop=loop, context=obj, name=Break()) | ||
except Exception as e: | ||
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. question: do we expect this exception in all cases? if so, use 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. So update it to be this? for _ in range(5):
with self.assertRaisesRegex(RuntimeError, 'break'):
task.__init__(coro, loop=loop, context=obj, name=Break()) 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. FTR: In order to catch the correct exception in #126120, I used a custom exception + a special argument so that I'm sure that I catch and expect the correct one (and not just a random one because of something else). So what I did was something like: # what I want to catch
def foo():
raise ReachableCode(1)
# what I don't want to catch yet
def bar():
raise ReachableCode(2)
# where I call the thing I want to catch
with self.assertRaises(ReachableCode) as cm:
something_that_calls_foo()
self.assertEqual(len(cm.exception.args), 1)
self.assertIs(cm.exception.args[0], 1) I find this pattern quite good (maybe a bit too exhaustive) because I can really see what I'm catching. 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 like the idea of the ReachableCode exception class, but I still think it's easier and more readable to just do with self.assertRaisesRegex(ReachableCode, 'str here'):
... and use a string in the arg instead of an int 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 think I'll go with that as well to simplify the tests on my side (but not today). |
||
# exception should only come from Break.__str__ as it's used to | ||
# avoid having to do too much setup for this test | ||
self.assertIs(type(e), RuntimeError) | ||
self.assertEqual(e.args[0], "break") | ||
coro.close() | ||
del task | ||
Nico-Posada marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
self.assertEqual(sys.getrefcount(obj), initial_refcount) | ||
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. Please avoid tests for exact refcounts, the test should test that no references are leaked rather than reference count should be equal, both are not same thing. In this particular case all of this seems unnecessary, our existing refleak checker is sufficient for this. 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. Oh I was unaware of the existing refleak checker. Do you have any examples of where it's used so i can check it out? 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 can run |
||
|
||
|
||
def add_subclass_tests(cls): | ||
BaseTask = cls.Task | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Fixed a reference leak in :class:`asyncio.Task` objects when reinitializing the same object with a non-``None`` context. Patch by Nico Posada. |
Uh oh!
There was an error while loading. Please reload this page.