diff options
author | Tin Tvrtković <tinchester@gmail.com> | 2022-02-26 16:18:48 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-02-26 16:18:48 (GMT) |
commit | edbee56d698ebb4489aa68311f44d104a23f5eb7 (patch) | |
tree | a893557a3c45e059d09c0de457a7fb18da63a294 /Lib/asyncio | |
parent | 41ddcd3f40f8171a396e57b841a74fcc83884eab (diff) | |
download | cpython-edbee56d698ebb4489aa68311f44d104a23f5eb7.zip cpython-edbee56d698ebb4489aa68311f44d104a23f5eb7.tar.gz cpython-edbee56d698ebb4489aa68311f44d104a23f5eb7.tar.bz2 |
Taskgroup tweaks (GH-31559)
Now uses .cancel()/.uncancel(), for even fewer broken edge cases.
Diffstat (limited to 'Lib/asyncio')
-rw-r--r-- | Lib/asyncio/taskgroups.py | 50 |
1 files changed, 23 insertions, 27 deletions
diff --git a/Lib/asyncio/taskgroups.py b/Lib/asyncio/taskgroups.py index 756fc55..c3ce94a 100644 --- a/Lib/asyncio/taskgroups.py +++ b/Lib/asyncio/taskgroups.py @@ -66,31 +66,28 @@ class TaskGroup: self._base_error is None): self._base_error = exc - if et is exceptions.CancelledError: - if self._parent_cancel_requested: - # Only if we did request task to cancel ourselves - # we mark it as no longer cancelled. - self._parent_task.uncancel() - else: - propagate_cancellation_error = et - - if et is not None and not self._aborting: - # Our parent task is being cancelled: - # - # async with TaskGroup() as g: - # g.create_task(...) - # await ... # <- CancelledError - # + if et is not None: if et is exceptions.CancelledError: - propagate_cancellation_error = et - - # or there's an exception in "async with": - # - # async with TaskGroup() as g: - # g.create_task(...) - # 1 / 0 - # - self._abort() + if self._parent_cancel_requested and not self._parent_task.uncancel(): + # Do nothing, i.e. swallow the error. + pass + else: + propagate_cancellation_error = exc + + if not self._aborting: + # Our parent task is being cancelled: + # + # async with TaskGroup() as g: + # g.create_task(...) + # await ... # <- CancelledError + # + # or there's an exception in "async with": + # + # async with TaskGroup() as g: + # g.create_task(...) + # 1 / 0 + # + self._abort() # We use while-loop here because "self._on_completed_fut" # can be cancelled multiple times if our parent task @@ -118,7 +115,6 @@ class TaskGroup: self._on_completed_fut = None assert self._unfinished_tasks == 0 - self._on_completed_fut = None # no longer needed if self._base_error is not None: raise self._base_error @@ -199,8 +195,7 @@ class TaskGroup: }) return - self._abort() - if not self._parent_task.cancelling(): + if not self._aborting and not self._parent_cancel_requested: # If parent task *is not* being cancelled, it means that we want # to manually cancel it to abort whatever is being run right now # in the TaskGroup. But we want to mark parent task as @@ -219,5 +214,6 @@ class TaskGroup: # pass # await something_else # this line has to be called # # after TaskGroup is finished. + self._abort() self._parent_cancel_requested = True self._parent_task.cancel() |