Skip to content

Commit 2cb1a68

Browse files
authored
bpo-44911: Fixed IsolatedAsyncioTestCase from throwing an exception on leaked tasks (GH-27765)
1 parent ad0a8a9 commit 2cb1a68

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
@@ -135,7 +135,7 @@ def _tearDownAsyncioLoop(self):
135135
task.cancel()
136136

137137
loop.run_until_complete(
138-
asyncio.gather(*to_cancel, loop=loop, return_exceptions=True))
138+
asyncio.gather(*to_cancel, return_exceptions=True))
139139

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

Lib/unittest/test/test_async_case.py

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

219+
def test_cancellation_hanging_tasks(self):
220+
cancelled = False
221+
class Test(unittest.IsolatedAsyncioTestCase):
222+
async def test_leaking_task(self):
223+
async def coro():
224+
nonlocal cancelled
225+
try:
226+
await asyncio.sleep(1)
227+
except asyncio.CancelledError:
228+
cancelled = True
229+
raise
230+
231+
# Leave this running in the background
232+
asyncio.create_task(coro())
233+
234+
test = Test("test_leaking_task")
235+
output = test.run()
236+
self.assertTrue(cancelled)
237+
238+
219239

220240

221241
if __name__ == "__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)