@@ -66,31 +66,28 @@ async def __aexit__(self, et, exc, tb):
6666 self ._base_error is None ):
6767 self ._base_error = exc
6868
69- if et is exceptions .CancelledError :
70- if self ._parent_cancel_requested :
71- # Only if we did request task to cancel ourselves
72- # we mark it as no longer cancelled.
73- self ._parent_task .uncancel ()
74- else :
75- propagate_cancellation_error = et
76-
77- if et is not None and not self ._aborting :
78- # Our parent task is being cancelled:
79- #
80- # async with TaskGroup() as g:
81- # g.create_task(...)
82- # await ... # <- CancelledError
83- #
69+ if et is not None :
8470 if et is exceptions .CancelledError :
85- propagate_cancellation_error = et
86-
87- # or there's an exception in "async with":
88- #
89- # async with TaskGroup() as g:
90- # g.create_task(...)
91- # 1 / 0
92- #
93- self ._abort ()
71+ if self ._parent_cancel_requested and not self ._parent_task .uncancel ():
72+ # Do nothing, i.e. swallow the error.
73+ pass
74+ else :
75+ propagate_cancellation_error = exc
76+
77+ if not self ._aborting :
78+ # Our parent task is being cancelled:
79+ #
80+ # async with TaskGroup() as g:
81+ # g.create_task(...)
82+ # await ... # <- CancelledError
83+ #
84+ # or there's an exception in "async with":
85+ #
86+ # async with TaskGroup() as g:
87+ # g.create_task(...)
88+ # 1 / 0
89+ #
90+ self ._abort ()
9491
9592 # We use while-loop here because "self._on_completed_fut"
9693 # can be cancelled multiple times if our parent task
@@ -118,7 +115,6 @@ async def __aexit__(self, et, exc, tb):
118115 self ._on_completed_fut = None
119116
120117 assert self ._unfinished_tasks == 0
121- self ._on_completed_fut = None # no longer needed
122118
123119 if self ._base_error is not None :
124120 raise self ._base_error
@@ -199,8 +195,7 @@ def _on_task_done(self, task):
199195 })
200196 return
201197
202- self ._abort ()
203- if not self ._parent_task .cancelling ():
198+ if not self ._aborting and not self ._parent_cancel_requested :
204199 # If parent task *is not* being cancelled, it means that we want
205200 # to manually cancel it to abort whatever is being run right now
206201 # in the TaskGroup. But we want to mark parent task as
@@ -219,5 +214,6 @@ def _on_task_done(self, task):
219214 # pass
220215 # await something_else # this line has to be called
221216 # # after TaskGroup is finished.
217+ self ._abort ()
222218 self ._parent_cancel_requested = True
223219 self ._parent_task .cancel ()
0 commit comments