@@ -66,31 +66,28 @@ async def __aexit__(self, et, exc, tb):
66
66
self ._base_error is None ):
67
67
self ._base_error = exc
68
68
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 :
84
70
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 ()
94
91
95
92
# We use while-loop here because "self._on_completed_fut"
96
93
# can be cancelled multiple times if our parent task
@@ -118,7 +115,6 @@ async def __aexit__(self, et, exc, tb):
118
115
self ._on_completed_fut = None
119
116
120
117
assert self ._unfinished_tasks == 0
121
- self ._on_completed_fut = None # no longer needed
122
118
123
119
if self ._base_error is not None :
124
120
raise self ._base_error
@@ -199,8 +195,7 @@ def _on_task_done(self, task):
199
195
})
200
196
return
201
197
202
- self ._abort ()
203
- if not self ._parent_task .cancelling ():
198
+ if not self ._aborting and not self ._parent_cancel_requested :
204
199
# If parent task *is not* being cancelled, it means that we want
205
200
# to manually cancel it to abort whatever is being run right now
206
201
# in the TaskGroup. But we want to mark parent task as
@@ -219,5 +214,6 @@ def _on_task_done(self, task):
219
214
# pass
220
215
# await something_else # this line has to be called
221
216
# # after TaskGroup is finished.
217
+ self ._abort ()
222
218
self ._parent_cancel_requested = True
223
219
self ._parent_task .cancel ()
0 commit comments