diff options
author | Nico-Posada <102486290+Nico-Posada@users.noreply.github.com> | 2024-10-31 07:47:57 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-10-31 07:47:57 (GMT) |
commit | d07dcce6935364cab807e0df931ed09b088ade69 (patch) | |
tree | 34ad1a05566414287caa39f40c30b11b1ac5e7f7 /Lib/test/test_asyncio | |
parent | d467d9246cbe0ce5dc149c4c74223bb8374ece73 (diff) | |
download | cpython-d07dcce6935364cab807e0df931ed09b088ade69.zip cpython-d07dcce6935364cab807e0df931ed09b088ade69.tar.gz cpython-d07dcce6935364cab807e0df931ed09b088ade69.tar.bz2 |
gh-126083: Fix a reference leak in `asyncio.Task` when reinitializing with new non-`None` context (#126103)
Diffstat (limited to 'Lib/test/test_asyncio')
-rw-r--r-- | Lib/test/test_asyncio/test_tasks.py | 22 |
1 files changed, 22 insertions, 0 deletions
diff --git a/Lib/test/test_asyncio/test_tasks.py b/Lib/test/test_asyncio/test_tasks.py index a1013ab..9d2d356 100644 --- a/Lib/test/test_asyncio/test_tasks.py +++ b/Lib/test/test_asyncio/test_tasks.py @@ -2688,6 +2688,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 |