Skip to content

Commit

Permalink
[3.9] GH-89074: Fixed IsolatedAsyncioTestCase from throwing an except…
Browse files Browse the repository at this point in the history
…ion on leaked tasks (GH-27765) (#91471)

(cherry picked from commit 2cb1a68)

Co-authored-by: Bar Harel <bar.harel@biocatch.com>
  • Loading branch information
miss-islington and bharel committed Apr 12, 2022
1 parent 8cd0f27 commit 4cc4fe2
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
Expand Up @@ -134,7 +134,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
Expand Up @@ -278,6 +278,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__":
unittest.main()
@@ -0,0 +1 @@
:class:`~unittest.IsolatedAsyncioTestCase` will no longer throw an exception while cancelling leaked tasks. Patch by Bar Harel.

0 comments on commit 4cc4fe2

Please sign in to comment.