[Python-checkins] bpo-44911: Fixed IsolatedAsyncioTestCase from throwing an exception on leaked tasks (GH-27765)

miss-islington webhook-mailer at python.org
Mon Aug 16 05:55:17 EDT 2021


https://github.com/python/cpython/commit/8516ca500eb45ecf997a471f003df02a9eb767ce
commit: 8516ca500eb45ecf997a471f003df02a9eb767ce
branch: 3.10
author: Miss Islington (bot) <31488909+miss-islington at users.noreply.github.com>
committer: miss-islington <31488909+miss-islington at users.noreply.github.com>
date: 2021-08-16T02:54:58-07:00
summary:

bpo-44911: Fixed IsolatedAsyncioTestCase from throwing an exception on leaked tasks (GH-27765)

(cherry picked from commit 2cb1a6806c0cefab0c3a40fdd428a89a4392570e)

Co-authored-by: Bar Harel <bar.harel at biocatch.com>

files:
A Misc/NEWS.d/next/Library/2021-08-14-00-55-16.bpo-44911.uk3hYk.rst
M Lib/unittest/async_case.py
M Lib/unittest/test/test_async_case.py

diff --git a/Lib/unittest/async_case.py b/Lib/unittest/async_case.py
index 520213c3727555..86ed7046c499a3 100644
--- a/Lib/unittest/async_case.py
+++ b/Lib/unittest/async_case.py
@@ -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():
diff --git a/Lib/unittest/test/test_async_case.py b/Lib/unittest/test/test_async_case.py
index d01864b6936ca8..6e48b9e4bfed36 100644
--- a/Lib/unittest/test/test_async_case.py
+++ b/Lib/unittest/test/test_async_case.py
@@ -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__":
diff --git a/Misc/NEWS.d/next/Library/2021-08-14-00-55-16.bpo-44911.uk3hYk.rst b/Misc/NEWS.d/next/Library/2021-08-14-00-55-16.bpo-44911.uk3hYk.rst
new file mode 100644
index 00000000000000..f8aed69a40a3be
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2021-08-14-00-55-16.bpo-44911.uk3hYk.rst
@@ -0,0 +1 @@
+:class:`~unittest.IsolatedAsyncioTestCase` will no longer throw an exception while cancelling leaked tasks. Patch by Bar Harel.
\ No newline at end of file



More information about the Python-checkins mailing list