From 95e783a7890e55bb1b44807d97bfb02f0b5b826d Mon Sep 17 00:00:00 2001 From: Timofei Ivankov <128279579+deadlovelll@users.noreply.github.com> Date: Sun, 22 Nov 2026 21:18:55 +0300 Subject: [PATCH 1/2] gh-162061: Fix awaited-by links for asyncio.gather() (GH-162394) * gh-162061: Fix awaited-by links for asyncio.gather() * Apply suggestions from code review * Apply suggestions from code review --------- (cherry picked from commit 0890d65356f87a521dd2bbc26d04efb0fd3e6353) Co-authored-by: Timofei Ivankov <128279579+deadlovelll@users.noreply.github.com> Co-authored-by: Andrew Svetlov --- Lib/asyncio/tasks.py | 20 ++++------- Lib/test/test_asyncio/test_graph.py | 35 ++++++++++++++++++- Lib/test/test_external_inspection.py | 9 +++-- ...-11-21-13-50-09.gh-issue-162061.UbvZxJ.rst | 2 ++ 4 files changed, 50 insertions(+), 16 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 12d92032b5c92bc..78e39854427c074 100644 --- a/Lib/asyncio/tasks.py +++ b/Lib/asyncio/tasks.py @@ -836,17 +836,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(): @@ -861,13 +856,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 @@ -934,10 +929,9 @@ 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: - for fut in arg_to_fut.values(): - if not fut.done(): - futures.future_add_to_awaited_by(fut, cur_task) + for fut in arg_to_fut.values(): + if not fut.done(): + 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 9a2bca3bf04a7cd..e20753bb2d7e3ef 100644 --- a/Lib/test/test_asyncio/test_graph.py +++ b/Lib/test/test_asyncio/test_graph.py @@ -198,10 +198,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 @@ -562,6 +584,17 @@ def setUp(self): self._Future = asyncio.Future asyncio.Future = futures.Future = futures._PyFuture + # The real _GatheringFuture subclasses the C Future, which the + # pure-Python awaited-by helpers ignore. + class _PyGatheringFuture(futures._PyFuture): + def __init__(self, children, *, loop): + super().__init__(loop=loop) + self._children = children + self._cancel_requested = False + cancel = tasks._GatheringFuture.cancel + self.enterContext(mock.patch.object(tasks, '_GatheringFuture', + _PyGatheringFuture)) + 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 0739c1d331751b7..0295623d2f0af25 100644 --- a/Lib/test/test_external_inspection.py +++ b/Lib/test/test_external_inspection.py @@ -1342,11 +1342,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"}, }, ) @@ -1360,6 +1363,7 @@ async def main(): coroutine_stacks, { "Task-1": [((script_name, 21, "main", None),)], + future_name: [()], "Task-2": [ ( (script_name, 11, "deep", None), @@ -1387,7 +1391,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. From 2dd08a08190664e4ebb387f2318e29943ffe363b Mon Sep 17 00:00:00 2001 From: Andrew Svetlov Date: Sun, 22 Nov 2026 20:02:20 +0100 Subject: [PATCH 2/2] Add missing mock import in test_graph Co-Authored-By: Claude Opus 5.5 --- Lib/test/test_asyncio/test_graph.py | 1 + 1 file changed, 1 insertion(+) diff --git a/Lib/test/test_asyncio/test_graph.py b/Lib/test/test_asyncio/test_graph.py index e20753bb2d7e3ef..404df3f9fbafd6e 100644 --- a/Lib/test/test_asyncio/test_graph.py +++ b/Lib/test/test_asyncio/test_graph.py @@ -1,6 +1,7 @@ import asyncio import io import unittest +from unittest import mock # To prevent a warning "test altered the execution environment"