Skip to content

Commit 1475d8b

Browse files
miss-islingtonbharel
authored andcommitted
[3.9] pythonGH-89074: Fixed IsolatedAsyncioTestCase from throwing an exception on leaked tasks (pythonGH-27765) (python#91471)
(cherry picked from commit 2cb1a68) Co-authored-by: Bar Harel <[email protected]>
1 parent 07b7ab2 commit 1475d8b

File tree

3 files changed

+22
-1
lines changed

3 files changed

+22
-1
lines changed

Lib/unittest/async_case.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ def _tearDownAsyncioLoop(self):
134134
task.cancel()
135135

136136
loop.run_until_complete(
137-
asyncio.gather(*to_cancel, loop=loop, return_exceptions=True))
137+
asyncio.gather(*to_cancel, return_exceptions=True))
138138

139139
for task in to_cancel:
140140
if task.cancelled():

Lib/unittest/test/test_async_case.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,26 @@ async def test_cancel(self):
278278
output = test.run()
279279
self.assertFalse(output.wasSuccessful())
280280

281+
def test_cancellation_hanging_tasks(self):
282+
cancelled = False
283+
class Test(unittest.IsolatedAsyncioTestCase):
284+
async def test_leaking_task(self):
285+
async def coro():
286+
nonlocal cancelled
287+
try:
288+
await asyncio.sleep(1)
289+
except asyncio.CancelledError:
290+
cancelled = True
291+
raise
292+
293+
# Leave this running in the background
294+
asyncio.create_task(coro())
295+
296+
test = Test("test_leaking_task")
297+
output = test.run()
298+
self.assertTrue(cancelled)
299+
300+
281301

282302
if __name__ == "__main__":
283303
unittest.main()
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
:class:`~unittest.IsolatedAsyncioTestCase` will no longer throw an exception while cancelling leaked tasks. Patch by Bar Harel.

0 commit comments

Comments
 (0)