@@ -775,7 +775,7 @@ def cancel(self, msg=None):
775775 return ret
776776
777777
778- def _discard_awaited_by (children , waiter , outer ):
778+ def _discard_awaited_by (children , waiter ):
779779 for fut in children :
780780 futures .future_discard_from_awaited_by (fut , waiter )
781781
@@ -841,11 +841,14 @@ def _done_callback(fut, cur_task=cur_task):
841841 # 'fut.exception()' will *raise* a CancelledError
842842 # instead of returning it.
843843 exc = fut ._make_cancelled_error ()
844+ # gh-157213: children outliving gather() must lose the edge
845+ _discard_awaited_by (children , cur_task )
844846 outer .set_exception (exc )
845847 return
846848 else :
847849 exc = fut .exception ()
848850 if exc is not None :
851+ _discard_awaited_by (children , cur_task )
849852 outer .set_exception (exc )
850853 return
851854
@@ -913,10 +916,6 @@ def _done_callback(fut, cur_task=cur_task):
913916 children .append (fut )
914917
915918 outer = _GatheringFuture (children , loop = loop )
916- if cur_task is not None :
917- # gh-157213: a child outliving gather() must lose the awaited-by edge
918- outer .add_done_callback (
919- functools .partial (_discard_awaited_by , children , cur_task ))
920919 # Run done callbacks after GatheringFuture created so any post-processing
921920 # can be performed at this point
922921 # optimization: in the special case that *all* futures finished eagerly,
0 commit comments