diff --git a/Lib/asyncio/tasks.py b/Lib/asyncio/tasks.py index 86b64880b37c9b..e2400170ef2e64 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 1f1c76e4aef69f..af9c0a9a030a87 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 00000000000000..563686f8561aa7 --- /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.