Skip to content

Commit

Permalink
bpo-39101: Fixes BaseException hang in IsolatedAsyncioTestCase. (GH-2…
Browse files Browse the repository at this point in the history
…2654)

(cherry picked from commit 8374d2e)

Co-authored-by: Lisa Roach <lisaroach14@gmail.com>
  • Loading branch information
miss-islington and lisroach committed Dec 16, 2020
1 parent 0a24a57 commit d549d0b
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 2 deletions.
4 changes: 2 additions & 2 deletions Lib/unittest/async_case.py
Expand Up @@ -102,9 +102,9 @@ async def _asyncioLoopRunner(self, fut):
ret = await awaitable
if not fut.cancelled():
fut.set_result(ret)
except asyncio.CancelledError:
except (SystemExit, KeyboardInterrupt):
raise
except Exception as ex:
except (BaseException, asyncio.CancelledError) as ex:

This comment has been minimized.

Copy link
@r-owen

r-owen Mar 2, 2021

Contributor

asyncio.CancelledError inherits from Exception so wouldn't it make more sense to either use

except BaseException as ex:

or

except (Exception, asyncio.CancelledError) as ex
if not fut.cancelled():
fut.set_exception(ex)

Expand Down
27 changes: 27 additions & 0 deletions Lib/unittest/test/test_async_case.py
Expand Up @@ -190,6 +190,33 @@ async def on_async_cleanup(self, val):
'async_cleanup 2',
'sync_cleanup 1'])

def test_base_exception_from_async_method(self):
events = []
class Test(unittest.IsolatedAsyncioTestCase):
async def test_base(self):
events.append("test_base")
raise BaseException()
events.append("not it")

async def test_no_err(self):
events.append("test_no_err")

async def test_cancel(self):
raise asyncio.CancelledError()

test = Test("test_base")
output = test.run()
self.assertFalse(output.wasSuccessful())

test = Test("test_no_err")
test.run()
self.assertEqual(events, ['test_base', 'test_no_err'])

test = Test("test_cancel")
output = test.run()
self.assertFalse(output.wasSuccessful())



if __name__ == "__main__":
unittest.main()
@@ -0,0 +1 @@
Fixed tests using IsolatedAsyncioTestCase from hanging on BaseExceptions.

0 comments on commit d549d0b

Please sign in to comment.