From 93e2a2424c1423587f31df2190c57efca2bd5477 Mon Sep 17 00:00:00 2001 From: Timofey Ivankov Date: Sat, 21 Nov 2026 20:54:35 +0300 Subject: [PATCH 1/2] gh-162395: Fix awaited-by links for asyncio.shield() --- Lib/asyncio/tasks.py | 17 ++++-------- Lib/test/test_asyncio/test_graph.py | 27 +++++++++++++++++-- ...-11-21-20-51-29.gh-issue-162395.l8Vlxo.rst | 2 ++ 3 files changed, 32 insertions(+), 14 deletions(-) create mode 100644 Misc/NEWS.d/next/Library/2026-11-21-20-51-29.gh-issue-162395.l8Vlxo.rst diff --git a/Lib/asyncio/tasks.py b/Lib/asyncio/tasks.py index 86b64880b37c9bd..e2400170ef2e646 100644 --- a/Lib/asyncio/tasks.py +++ b/Lib/asyncio/tasks.py @@ -993,13 +993,10 @@ def shield(arg): loop = futures._get_loop(inner) outer = loop.create_future() - if loop is not None and (cur_task := current_task(loop)) is not None: - futures.future_add_to_awaited_by(inner, cur_task) - else: - cur_task = None + futures.future_add_to_awaited_by(inner, outer) def _clear_awaited_by_callback(inner): - futures.future_discard_from_awaited_by(inner, cur_task) + futures.future_discard_from_awaited_by(inner, outer) def _inner_done_callback(inner): if outer.cancelled(): @@ -1017,14 +1014,10 @@ def _inner_done_callback(inner): def _outer_done_callback(outer): if not inner.done(): inner.remove_done_callback(_inner_done_callback) - if cur_task is not None: - inner.remove_done_callback(_clear_awaited_by_callback) - futures.future_discard_from_awaited_by(inner, cur_task) - - if cur_task is not None: - inner.add_done_callback(_clear_awaited_by_callback) - + inner.remove_done_callback(_clear_awaited_by_callback) + futures.future_discard_from_awaited_by(inner, outer) + inner.add_done_callback(_clear_awaited_by_callback) inner.add_done_callback(_inner_done_callback) outer.add_done_callback(_outer_done_callback) return outer diff --git a/Lib/test/test_asyncio/test_graph.py b/Lib/test/test_asyncio/test_graph.py index 1f1c76e4aef69f8..af9c0a9a030a870 100644 --- a/Lib/test/test_asyncio/test_graph.py +++ b/Lib/test/test_asyncio/test_graph.py @@ -575,10 +575,31 @@ async def main(): 'T', ['s capture_test_stack', 'a deep', 'a c1'], [ - ['T', ['a main', 'a test_stack_shield'], []] + ['F', [], [ + ['T', ['a main', 'a test_stack_shield'], []], + ]], ] ]) + async def test_shield_awaited_by_different_task(self): + async def consume(fut): + await fut + + release = asyncio.Event() + child = asyncio.create_task(release.wait()) + shielded = asyncio.shield(child) + consumer = asyncio.create_task(consume(shielded), 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, shielded) + parent, = waiter.awaited_by + self.assertIs(parent.future, consumer) + self.assertFalse(parent.awaited_by) + async def test_stack_timeout(self): stack_for_inner = None @@ -602,7 +623,9 @@ async def main(): 'T', ['s capture_test_stack', 'a inner', 'a c1'], [ - ['T', ['a main', 'a test_stack_timeout'], []] + ['F', [], [ + ['T', ['a main', 'a test_stack_timeout'], []], + ]], ] ]) diff --git a/Misc/NEWS.d/next/Library/2026-11-21-20-51-29.gh-issue-162395.l8Vlxo.rst b/Misc/NEWS.d/next/Library/2026-11-21-20-51-29.gh-issue-162395.l8Vlxo.rst new file mode 100644 index 000000000000000..ddccced63b61e50 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-11-21-20-51-29.gh-issue-162395.l8Vlxo.rst @@ -0,0 +1,2 @@ +Fix the asyncio call graph to show which task awaits the future returned by +:func:asyncio.shield. Patch by Timofei Ivankov. From b4928096a698ece5a666c313b2bcb850d9c99562 Mon Sep 17 00:00:00 2001 From: Timofey Ivankov Date: Sat, 21 Nov 2026 22:03:32 +0300 Subject: [PATCH 2/2] fix news lint --- .../next/Library/2026-11-21-20-51-29.gh-issue-162395.l8Vlxo.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Misc/NEWS.d/next/Library/2026-11-21-20-51-29.gh-issue-162395.l8Vlxo.rst b/Misc/NEWS.d/next/Library/2026-11-21-20-51-29.gh-issue-162395.l8Vlxo.rst index ddccced63b61e50..563686f8561aa78 100644 --- a/Misc/NEWS.d/next/Library/2026-11-21-20-51-29.gh-issue-162395.l8Vlxo.rst +++ b/Misc/NEWS.d/next/Library/2026-11-21-20-51-29.gh-issue-162395.l8Vlxo.rst @@ -1,2 +1,2 @@ Fix the asyncio call graph to show which task awaits the future returned by -:func:asyncio.shield. Patch by Timofei Ivankov. +:func:`asyncio.shield`. Patch by Timofei Ivankov.