mirror of
https://github.com/python/cpython.git
synced 2025-08-03 00:23:06 +00:00
[3.13] gh-126083: Fix a reference leak in asyncio.Task
when reinitializing with new non-None
context (GH-126103) (#126229)
gh-126083: Fix a reference leak in `asyncio.Task` when reinitializing with new non-`None` context (GH-126103)
(cherry picked from commit d07dcce693
)
Co-authored-by: Nico-Posada <102486290+Nico-Posada@users.noreply.github.com>
This commit is contained in:
parent
57668a4be5
commit
ac00bf0e96
3 changed files with 24 additions and 1 deletions
|
@ -2684,6 +2684,28 @@ class BaseTaskTests:
|
|||
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):
|
||||
with self.assertRaisesRegex(RuntimeError, 'break'):
|
||||
task.__init__(coro, loop=loop, context=obj, name=Break())
|
||||
|
||||
coro.close()
|
||||
del task
|
||||
|
||||
self.assertEqual(sys.getrefcount(obj), initial_refcount)
|
||||
|
||||
|
||||
def add_subclass_tests(cls):
|
||||
BaseTask = cls.Task
|
||||
|
|
|
@ -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.
|
|
@ -2070,7 +2070,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);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue