mirror of
https://github.com/python/cpython.git
synced 2025-11-11 19:12:05 +00:00
GH-94398: TaskGroup: Fail create_task() during shutdown (GH-94400) (GH-94463)
Once the task group is shutting down, it should not be possible to create a new task.
Here "shutting down" means `self._aborting` is set, indicating that at least one task
has failed and we have cancelled all others.
Co-authored-by: Łukasz Langa <lukasz@langa.pl>
(cherry picked from commit 594c369949)
Co-authored-by: Guido van Rossum <guido@python.org>
This commit is contained in:
parent
aed28b7feb
commit
7fe949e5ea
4 changed files with 27 additions and 4 deletions
|
|
@ -122,10 +122,8 @@ async def runner():
|
|||
self.assertTrue(t2.cancelled())
|
||||
|
||||
async def test_cancel_children_on_child_error(self):
|
||||
"""
|
||||
When a child task raises an error, the rest of the children
|
||||
are cancelled and the errors are gathered into an EG.
|
||||
"""
|
||||
# When a child task raises an error, the rest of the children
|
||||
# are cancelled and the errors are gathered into an EG.
|
||||
|
||||
NUM = 0
|
||||
t2_cancel = False
|
||||
|
|
@ -722,6 +720,27 @@ async def coro(val):
|
|||
await t2
|
||||
self.assertEqual(2, ctx.get(cvar))
|
||||
|
||||
async def test_taskgroup_no_create_task_after_failure(self):
|
||||
async def coro1():
|
||||
await asyncio.sleep(0.001)
|
||||
1 / 0
|
||||
async def coro2(g):
|
||||
try:
|
||||
await asyncio.sleep(1)
|
||||
except asyncio.CancelledError:
|
||||
with self.assertRaises(RuntimeError):
|
||||
g.create_task(c1 := coro1())
|
||||
# We still have to await c1 to avoid a warning
|
||||
with self.assertRaises(ZeroDivisionError):
|
||||
await c1
|
||||
|
||||
with self.assertRaises(ExceptionGroup) as cm:
|
||||
async with taskgroups.TaskGroup() as g:
|
||||
g.create_task(coro1())
|
||||
g.create_task(coro2(g))
|
||||
|
||||
self.assertEqual(get_error_types(cm.exception), {ZeroDivisionError})
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue