Skip to content

Commit 8e5effe

Browse files
committed
Address review comments
1 parent 614d20f commit 8e5effe

File tree

2 files changed

+12
-6
lines changed

2 files changed

+12
-6
lines changed

Lib/test/test_asyncio/test_tasks.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2689,20 +2689,27 @@ def test_get_context(self):
26892689
loop.close()
26902690

26912691
def test_proper_refcounts(self):
2692+
# see: https://github.com/python/cpython/issues/126083
26922693
class Break:
26932694
def __str__(self):
2694-
raise Exception("break")
2695+
raise RuntimeError("break")
26952696

26962697
obj = object()
26972698
initial_refcount = sys.getrefcount(obj)
26982699

26992700
coro = coroutine_function()
2701+
loop = asyncio.new_event_loop()
27002702
task = asyncio.Task.__new__(asyncio.Task)
27012703

2702-
for _ in range(10000):
2704+
for _ in range(5):
27032705
try:
2704-
task.__init__(coro, context=obj, name=Break())
2705-
except: pass
2706+
task.__init__(coro, loop=loop, context=obj, name=Break())
2707+
except Exception as e:
2708+
# exception should only come from Break.__str__ as it's used to
2709+
# avoid having to do too much setup for this test
2710+
self.assertIs(type(e), RuntimeError)
2711+
self.assertEqual(e.args[0], "break")
2712+
coro.close()
27062713
del task
27072714

27082715
self.assertEqual(sys.getrefcount(obj), initial_refcount)
Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
1-
Fixed a reference leak that would happen in asyncio tasks when you would
2-
reinitialize your task object with a non-None context.
1+
Fixed a reference leak in :class:`asyncio.Task` objects when reinitializing the same object with a non-``None`` context. Patch by Nico Posada.

0 commit comments

Comments
 (0)