From 7ececc77522a5d6bd858e978837aa026d0706975 Mon Sep 17 00:00:00 2001 From: Timofey Ivankov Date: Sat, 21 Nov 2026 13:53:34 +0300 Subject: [PATCH] gh-162061: Fix awaited-by links for asyncio.gather() --- Lib/asyncio/tasks.py | 17 ++++------- Lib/test/test_asyncio/test_graph.py | 30 ++++++++++++++++++- Lib/test/test_external_inspection.py | 9 ++++-- ...-11-21-13-50-09.gh-issue-162061.UbvZxJ.rst | 2 ++ 4 files changed, 44 insertions(+), 14 deletions(-) create mode 100644 Misc/NEWS.d/next/Library/2026-11-21-13-50-09.gh-issue-162061.UbvZxJ.rst diff --git a/Lib/asyncio/tasks.py b/Lib/asyncio/tasks.py index 86b64880b37c9bd..9eb7569b8786f7d 100644 --- a/Lib/asyncio/tasks.py +++ b/Lib/asyncio/tasks.py @@ -841,17 +841,12 @@ def gather(*coros_or_futures, return_exceptions=False): return outer loop = events._get_running_loop() - if loop is not None: - cur_task = current_task(loop) - else: - cur_task = None - def _done_callback(fut, cur_task=cur_task): + def _done_callback(fut): nonlocal nfinished nfinished += 1 - if cur_task is not None: - futures.future_discard_from_awaited_by(fut, cur_task) + futures.future_discard_from_awaited_by(fut, outer) if outer is None or outer.done(): if not fut.cancelled(): @@ -866,13 +861,13 @@ def _done_callback(fut, cur_task=cur_task): # instead of returning it. exc = fut._make_cancelled_error() # gh-157213: children outliving gather() must lose the edge - _discard_awaited_by(children, cur_task) + _discard_awaited_by(children, outer) outer.set_exception(exc) return else: exc = fut.exception() if exc is not None: - _discard_awaited_by(children, cur_task) + _discard_awaited_by(children, outer) outer.set_exception(exc) return @@ -939,10 +934,10 @@ def _done_callback(fut, cur_task=cur_task): outer = _GatheringFuture(children, loop=loop) # Add the awaited-by edges only after all arguments are validated - if cur_task is not None and len(done_futs) < nfuts: + if len(done_futs) < nfuts: for fut in arg_to_fut.values(): if not fut.done(): - futures.future_add_to_awaited_by(fut, cur_task) + futures.future_add_to_awaited_by(fut, outer) # Run done callbacks after GatheringFuture created so any post-processing # can be performed at this point # optimization: in the special case that *all* futures finished eagerly, diff --git a/Lib/test/test_asyncio/test_graph.py b/Lib/test/test_asyncio/test_graph.py index 1f1c76e4aef69f8..93ae56925a5a253 100644 --- a/Lib/test/test_asyncio/test_graph.py +++ b/Lib/test/test_asyncio/test_graph.py @@ -1,4 +1,5 @@ import asyncio +import importlib.util import io import sys import types @@ -526,10 +527,32 @@ async def main(): 'T', ['s capture_test_stack', 'a deep', 'a c1'], [ - ['T', ['a main', 'a test_stack_gather'], []] + ['F', [], [ + ['T', ['a main', 'a test_stack_gather'], []], + ]], ] ]) + async def test_gather_awaited_by_different_task(self): + # gh-162061 + async def consume(fut): + await fut + + release = asyncio.Event() + child = asyncio.create_task(release.wait()) + gathered = asyncio.gather(child) + consumer = asyncio.create_task(consume(gathered), eager_start=True) + graph = asyncio.capture_call_graph(child, limit=0) + release.set() + await consumer + self.assertFalse(asyncio.capture_call_graph(child, limit=0).awaited_by) + + waiter, = graph.awaited_by + self.assertIs(waiter.future, gathered) + parent, = waiter.awaited_by + self.assertIs(parent.future, consumer) + self.assertFalse(parent.awaited_by) + async def test_stack_gather_survivor(self): # gh-157213: a child that outlives gather() must not be shown as awaited @@ -1048,6 +1071,11 @@ def setUp(self): self._Future = asyncio.Future asyncio.Future = futures.Future = futures._PyFuture + py_tasks = importlib.util.module_from_spec(tasks.__spec__) + tasks.__spec__.loader.exec_module(py_tasks) + self.enterContext(mock.patch.object(tasks, '_GatheringFuture', + py_tasks._GatheringFuture)) + self._Task = asyncio.Task asyncio.Task = tasks.Task = tasks._PyTask diff --git a/Lib/test/test_external_inspection.py b/Lib/test/test_external_inspection.py index 47be0f2286e792c..b763f395bb24eb1 100644 --- a/Lib/test/test_external_inspection.py +++ b/Lib/test/test_external_inspection.py @@ -1391,11 +1391,14 @@ async def main(): relationships = self._get_awaited_by_relationships( stack_trace ) + future_name, = (node.task_name for node in stack_trace[0].awaited_by + if node.is_future) self.assertEqual( relationships, { "Task-1": set(), - "Task-2": {"Task-1"}, + "Task-2": {future_name}, + future_name: {"Task-1"}, }, ) @@ -1409,6 +1412,7 @@ async def main(): coroutine_stacks, { "Task-1": [((script_name, 21, "main", None),)], + future_name: [()], "Task-2": [ ( (script_name, 11, "deep", None), @@ -1436,7 +1440,8 @@ async def main(): awaited_by_coroutine_stacks, { "Task-1": [], - "Task-2": [ + "Task-2": [(future_name, ())], + future_name: [ ("Task-1", ((script_name, 21, "main", None),)) ], }, diff --git a/Misc/NEWS.d/next/Library/2026-11-21-13-50-09.gh-issue-162061.UbvZxJ.rst b/Misc/NEWS.d/next/Library/2026-11-21-13-50-09.gh-issue-162061.UbvZxJ.rst new file mode 100644 index 000000000000000..db5b2e79b5b2db0 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-11-21-13-50-09.gh-issue-162061.UbvZxJ.rst @@ -0,0 +1,2 @@ +Fix the asyncio call graph to show which task awaits :func:`asyncio.gather`. +Patch by Timofei Ivankov.