Skip to content

Commit 0890d65

Browse files
gh-162061: Fix awaited-by links for asyncio.gather() (#162394)
* gh-162061: Fix awaited-by links for asyncio.gather() * Apply suggestions from code review * Apply suggestions from code review --------- Co-authored-by: Andrew Svetlov <andrew.svetlov@gmail.com>
1 parent 3092f32 commit 0890d65

4 files changed

Lines changed: 50 additions & 16 deletions

File tree

‎Lib/asyncio/tasks.py‎

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -841,17 +841,12 @@ def gather(*coros_or_futures, return_exceptions=False):
841841
return outer
842842

843843
loop = events._get_running_loop()
844-
if loop is not None:
845-
cur_task = current_task(loop)
846-
else:
847-
cur_task = None
848844

849-
def _done_callback(fut, cur_task=cur_task):
845+
def _done_callback(fut):
850846
nonlocal nfinished
851847
nfinished += 1
852848

853-
if cur_task is not None:
854-
futures.future_discard_from_awaited_by(fut, cur_task)
849+
futures.future_discard_from_awaited_by(fut, outer)
855850

856851
if outer is None or outer.done():
857852
if not fut.cancelled():
@@ -866,13 +861,13 @@ def _done_callback(fut, cur_task=cur_task):
866861
# instead of returning it.
867862
exc = fut._make_cancelled_error()
868863
# gh-157213: children outliving gather() must lose the edge
869-
_discard_awaited_by(children, cur_task)
864+
_discard_awaited_by(children, outer)
870865
outer.set_exception(exc)
871866
return
872867
else:
873868
exc = fut.exception()
874869
if exc is not None:
875-
_discard_awaited_by(children, cur_task)
870+
_discard_awaited_by(children, outer)
876871
outer.set_exception(exc)
877872
return
878873

@@ -939,10 +934,9 @@ def _done_callback(fut, cur_task=cur_task):
939934

940935
outer = _GatheringFuture(children, loop=loop)
941936
# Add the awaited-by edges only after all arguments are validated
942-
if cur_task is not None and len(done_futs) < nfuts:
943-
for fut in arg_to_fut.values():
944-
if not fut.done():
945-
futures.future_add_to_awaited_by(fut, cur_task)
937+
for fut in arg_to_fut.values():
938+
if not fut.done():
939+
futures.future_add_to_awaited_by(fut, outer)
946940
# Run done callbacks after GatheringFuture created so any post-processing
947941
# can be performed at this point
948942
# optimization: in the special case that *all* futures finished eagerly,

‎Lib/test/test_asyncio/test_graph.py‎

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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

‎Lib/test/test_external_inspection.py‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1391,11 +1391,14 @@ async def main():
13911391
relationships = self._get_awaited_by_relationships(
13921392
stack_trace
13931393
)
1394+
future_name, = (node.task_name for node in stack_trace[0].awaited_by
1395+
if node.is_future)
13941396
self.assertEqual(
13951397
relationships,
13961398
{
13971399
"Task-1": set(),
1398-
"Task-2": {"Task-1"},
1400+
"Task-2": {future_name},
1401+
future_name: {"Task-1"},
13991402
},
14001403
)
14011404

@@ -1409,6 +1412,7 @@ async def main():
14091412
coroutine_stacks,
14101413
{
14111414
"Task-1": [((script_name, 21, "main", None),)],
1415+
future_name: [()],
14121416
"Task-2": [
14131417
(
14141418
(script_name, 11, "deep", None),
@@ -1436,7 +1440,8 @@ async def main():
14361440
awaited_by_coroutine_stacks,
14371441
{
14381442
"Task-1": [],
1439-
"Task-2": [
1443+
"Task-2": [(future_name, ())],
1444+
future_name: [
14401445
("Task-1", ((script_name, 21, "main", None),))
14411446
],
14421447
},
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix the asyncio call graph to show which task awaits :func:`asyncio.gather`.
2+
Patch by Timofei Ivankov.

0 commit comments

Comments
 (0)