[3.12] gh-126083: Fix a reference leak in asyncio.Task when reinitializing with new non-None context (GH-126103) (#126230)

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:
Miss Islington (bot) 2024-10-31 09:19:56 +01:00 committed by GitHub
parent 975911887d
commit 90de322b32
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 24 additions and 1 deletions

View file

@ -2489,6 +2489,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