Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions Lib/test/test_asyncio/test_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -2688,6 +2688,25 @@ def test_get_context(self):
finally:
loop.close()

def test_proper_refcounts(self):
class Break:
def __str__(self):
raise Exception("break")

obj = object()
initial_refcount = sys.getrefcount(obj)

coro = coroutine_function()
task = asyncio.Task.__new__(asyncio.Task)

for _ in range(10000):
Copy link
Member

Choose a reason for hiding this comment

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

Do we really need 10000 iterations to find a leak?

Copy link
Member

Choose a reason for hiding this comment

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

Well, maybe not 10000, but finding leaks is certainly easier with more iterations. Maybe 1000 should do?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I changed it to 5, the bug would be triggered every time so with a low amount you should still be able to see the ref leak.

try:
task.__init__(coro, context=obj, name=Break())
except: pass
del task

self.assertEqual(sys.getrefcount(obj), initial_refcount)
Copy link
Contributor

@kumaraditya303 kumaraditya303 Nov 2, 2024

Choose a reason for hiding this comment

The 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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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?

Copy link
Member

Choose a reason for hiding this comment

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

You can run python -m test XXX -R : (it's with -R :)



def add_subclass_tests(cls):
BaseTask = cls.Task
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fixed a reference leak that would happen in asyncio tasks when you would
reinitialize your task object with a non-None context.
2 changes: 1 addition & 1 deletion Modules/_asynciomodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -2120,7 +2120,7 @@ _asyncio_Task___init___impl(TaskObj *self, PyObject *coro, PyObject *loop,
return -1;
}
} else {
self->task_context = Py_NewRef(context);
Py_XSETREF(self->task_context, Py_NewRef(context));
}

Py_CLEAR(self->task_fut_waiter);
Expand Down
Loading