gh-115957: Close coroutine if TaskGroup.create_task() raises an error (#116009)

This commit is contained in:
Jason Zhang 2024-03-06 20:20:26 +00:00 committed by GitHub
parent 7114cf20c0
commit ce0ae1d784
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 42 additions and 16 deletions

View file

@ -154,10 +154,13 @@ class TaskGroup:
Similar to `asyncio.create_task`.
"""
if not self._entered:
coro.close()
raise RuntimeError(f"TaskGroup {self!r} has not been entered")
if self._exiting and not self._tasks:
coro.close()
raise RuntimeError(f"TaskGroup {self!r} is finished")
if self._aborting:
coro.close()
raise RuntimeError(f"TaskGroup {self!r} is shutting down")
if context is None:
task = self._loop.create_task(coro, name=name)