Skip to content
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/asyncio/timeouts.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ async def __aexit__(
raise TimeoutError from exc_val
elif exc_val is not None:
self._insert_timeout_error(exc_val)
if isinstance(exc_val, ExceptionGroup):
if isinstance(exc_val, BaseExceptionGroup):
for exc in exc_val.exceptions:
self._insert_timeout_error(exc)
elif self._state is _State.ENTERED:
Expand Down
33 changes: 33 additions & 0 deletions Lib/test/test_asyncio/test_timeouts.py
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,39 @@ async def task():
self.assertIsNone(e3.__cause__)
self.assertIs(e2.__context__, e3)

async def test_timeout_taskgroup_base_exception_group(self):
class MyBaseError(BaseException):
pass

async def task():
try:
await asyncio.sleep(2) # Will be interrupted after 0.01 second
finally:
raise MyBaseError # Crash in cleanup

with self.assertRaises(BaseExceptionGroup) as cm:
async with asyncio.timeout(0.01):
async with asyncio.TaskGroup() as tg:
tg.create_task(task())
await asyncio.sleep(1)
eg = cm.exception
self.assertNotIsInstance(eg, ExceptionGroup)
self.assertIsInstance(eg.__context__, TimeoutError)

self.assertEqual(len(eg.exceptions), 1, eg)
e1 = eg.exceptions[0]
# Expect MyBaseError raised during handling of TimeoutError
# caused by CancelledError.
self.assertIsInstance(e1, MyBaseError)
self.assertIsNone(e1.__cause__)
e2 = e1.__context__
self.assertIsInstance(e2, TimeoutError)
e3 = e2.__cause__
self.assertIsInstance(e3, asyncio.CancelledError)
self.assertIsNone(e3.__context__)
self.assertIsNone(e3.__cause__)
self.assertIs(e2.__context__, e3)


if __name__ == '__main__':
unittest.main()
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
:func:`asyncio.timeout` now inserts :exc:`TimeoutError` into the context
chain of sub-exceptions of a :exc:`BaseExceptionGroup`, as it already did for
:exc:`ExceptionGroup`.
Loading