Skip to content

bpo-44911: Fixed IsolatedAsyncioTestCase from throwing an exception on leaked tasks #27765

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Aug 16, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.