@@ -784,7 +784,7 @@ def cancel(self, msg=None):
784784 return ret
785785
786786
787- def _discard_awaited_by (children , waiter , outer ):
787+ def _discard_awaited_by (children , waiter ):
788788 for fut in children :
789789 futures .future_discard_from_awaited_by (fut , waiter )
790790
@@ -850,11 +850,14 @@ def _done_callback(fut, cur_task=cur_task):
850850 # 'fut.exception()' will *raise* a CancelledError
851851 # instead of returning it.
852852 exc = fut ._make_cancelled_error ()
853+ # gh-157213: children outliving gather() must lose the edge
854+ _discard_awaited_by (children , cur_task )
853855 outer .set_exception (exc )
854856 return
855857 else :
856858 exc = fut .exception ()
857859 if exc is not None :
860+ _discard_awaited_by (children , cur_task )
858861 outer .set_exception (exc )
859862 return
860863
@@ -922,10 +925,6 @@ def _done_callback(fut, cur_task=cur_task):
922925 children .append (fut )
923926
924927 outer = _GatheringFuture (children , loop = loop )
925- if cur_task is not None :
926- # gh-157213: a child outliving gather() must lose the awaited-by edge
927- outer .add_done_callback (
928- functools .partial (_discard_awaited_by , children , cur_task ))
929928 # Run done callbacks after GatheringFuture created so any post-processing
930929 # can be performed at this point
931930 # optimization: in the special case that *all* futures finished eagerly,
0 commit comments