Skip to content

Commit d549d0b

Browse files
bpo-39101: Fixes BaseException hang in IsolatedAsyncioTestCase. (GH-22654)
(cherry picked from commit 8374d2e) Co-authored-by: Lisa Roach <[email protected]>
1 parent 0a24a57 commit d549d0b

File tree

3 files changed

+30
-2
lines changed

3 files changed

+30
-2
lines changed

Lib/unittest/async_case.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,9 +102,9 @@ async def _asyncioLoopRunner(self, fut):
102102
ret = await awaitable
103103
if not fut.cancelled():
104104
fut.set_result(ret)
105-
except asyncio.CancelledError:
105+
except (SystemExit, KeyboardInterrupt):
106106
raise
107-
except Exception as ex:
107+
except (BaseException, asyncio.CancelledError) as ex:
108108
if not fut.cancelled():
109109
fut.set_exception(ex)
110110

Lib/unittest/test/test_async_case.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,33 @@ async def on_async_cleanup(self, val):
190190
'async_cleanup 2',
191191
'sync_cleanup 1'])
192192

193+
def test_base_exception_from_async_method(self):
194+
events = []
195+
class Test(unittest.IsolatedAsyncioTestCase):
196+
async def test_base(self):
197+
events.append("test_base")
198+
raise BaseException()
199+
events.append("not it")
200+
201+
async def test_no_err(self):
202+
events.append("test_no_err")
203+
204+
async def test_cancel(self):
205+
raise asyncio.CancelledError()
206+
207+
test = Test("test_base")
208+
output = test.run()
209+
self.assertFalse(output.wasSuccessful())
210+
211+
test = Test("test_no_err")
212+
test.run()
213+
self.assertEqual(events, ['test_base', 'test_no_err'])
214+
215+
test = Test("test_cancel")
216+
output = test.run()
217+
self.assertFalse(output.wasSuccessful())
218+
219+
193220

194221
if __name__ == "__main__":
195222
unittest.main()
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fixed tests using IsolatedAsyncioTestCase from hanging on BaseExceptions.

0 commit comments

Comments
 (0)