Skip to content

Commit

Permalink
bpo-44911: Fixed IsolatedAsyncioTestCase from throwing an exception o…
Browse files Browse the repository at this point in the history
…n leaked tasks (pythonGH-27765)
  • Loading branch information
bharel authored Aug 16, 2021
1 parent ad0a8a9 commit 2cb1a68
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Lib/unittest/async_case.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ def _tearDownAsyncioLoop(self):
task.cancel()

loop.run_until_complete(
asyncio.gather(*to_cancel, loop=loop, return_exceptions=True))
asyncio.gather(*to_cancel, return_exceptions=True))

for task in to_cancel:
if task.cancelled():
Expand Down
20 changes: 20 additions & 0 deletions Lib/unittest/test/test_async_case.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,26 @@ async def test_cancel(self):
output = test.run()
self.assertFalse(output.wasSuccessful())

def test_cancellation_hanging_tasks(self):
cancelled = False
class Test(unittest.IsolatedAsyncioTestCase):
async def test_leaking_task(self):
async def coro():
nonlocal cancelled
try:
await asyncio.sleep(1)
except asyncio.CancelledError:
cancelled = True
raise

# Leave this running in the background
asyncio.create_task(coro())

test = Test("test_leaking_task")
output = test.run()
self.assertTrue(cancelled)




if __name__ == "__main__":
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
:class:`~unittest.IsolatedAsyncioTestCase` will no longer throw an exception while cancelling leaked tasks. Patch by Bar Harel.

0 comments on commit 2cb1a68

Please sign in to comment.