@@ -526,10 +526,32 @@ async def main():
526526 'T<anon>' ,
527527 ['s capture_test_stack' , 'a deep' , 'a c1' ],
528528 [
529- ['T<anon>' , ['a main' , 'a test_stack_gather' ], []]
529+ ['F' , [], [
530+ ['T<anon>' , ['a main' , 'a test_stack_gather' ], []],
531+ ]],
530532 ]
531533 ])
532534
535+ async def test_gather_awaited_by_different_task (self ):
536+ # gh-162061
537+ async def consume (fut ):
538+ await fut
539+
540+ release = asyncio .Event ()
541+ child = asyncio .create_task (release .wait ())
542+ gathered = asyncio .gather (child )
543+ consumer = asyncio .create_task (consume (gathered ), eager_start = True )
544+ graph = asyncio .capture_call_graph (child , limit = 0 )
545+ release .set ()
546+ await consumer
547+ self .assertFalse (asyncio .capture_call_graph (child , limit = 0 ).awaited_by )
548+
549+ waiter , = graph .awaited_by
550+ self .assertIs (waiter .future , gathered )
551+ parent , = waiter .awaited_by
552+ self .assertIs (parent .future , consumer )
553+ self .assertFalse (parent .awaited_by )
554+
533555 async def test_stack_gather_survivor (self ):
534556 # gh-157213: a child that outlives gather() must not be shown as awaited
535557
@@ -1048,6 +1070,17 @@ def setUp(self):
10481070 self ._Future = asyncio .Future
10491071 asyncio .Future = futures .Future = futures ._PyFuture
10501072
1073+ # The real _GatheringFuture subclasses the C Future, which the
1074+ # pure-Python awaited-by helpers ignore.
1075+ class _PyGatheringFuture (futures ._PyFuture ):
1076+ def __init__ (self , children , * , loop ):
1077+ super ().__init__ (loop = loop )
1078+ self ._children = children
1079+ self ._cancel_requested = False
1080+ cancel = tasks ._GatheringFuture .cancel
1081+ self .enterContext (mock .patch .object (tasks , '_GatheringFuture' ,
1082+ _PyGatheringFuture ))
1083+
10511084 self ._Task = asyncio .Task
10521085 asyncio .Task = tasks .Task = tasks ._PyTask
10531086
0 commit comments