Skip to content

Commit 0193b83

Browse files
authored
Merge branch 'python:main' into main
2 parents 19eaf85 + d07dcce commit 0193b83

File tree

3 files changed

+24
-1
lines changed

3 files changed

+24
-1
lines changed

Lib/test/test_asyncio/test_tasks.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2688,6 +2688,28 @@ def test_get_context(self):
26882688
finally:
26892689
loop.close()
26902690

2691+
def test_proper_refcounts(self):
2692+
# see: https://github.com/python/cpython/issues/126083
2693+
class Break:
2694+
def __str__(self):
2695+
raise RuntimeError("break")
2696+
2697+
obj = object()
2698+
initial_refcount = sys.getrefcount(obj)
2699+
2700+
coro = coroutine_function()
2701+
loop = asyncio.new_event_loop()
2702+
task = asyncio.Task.__new__(asyncio.Task)
2703+
2704+
for _ in range(5):
2705+
with self.assertRaisesRegex(RuntimeError, 'break'):
2706+
task.__init__(coro, loop=loop, context=obj, name=Break())
2707+
2708+
coro.close()
2709+
del task
2710+
2711+
self.assertEqual(sys.getrefcount(obj), initial_refcount)
2712+
26912713

26922714
def add_subclass_tests(cls):
26932715
BaseTask = cls.Task
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fixed a reference leak in :class:`asyncio.Task` objects when reinitializing the same object with a non-``None`` context. Patch by Nico Posada.

Modules/_asynciomodule.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2120,7 +2120,7 @@ _asyncio_Task___init___impl(TaskObj *self, PyObject *coro, PyObject *loop,
21202120
return -1;
21212121
}
21222122
} else {
2123-
self->task_context = Py_NewRef(context);
2123+
Py_XSETREF(self->task_context, Py_NewRef(context));
21242124
}
21252125

21262126
Py_CLEAR(self->task_fut_waiter);

0 commit comments

Comments
 (0)