Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 5 additions & 12 deletions Lib/asyncio/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When outer is consumed by wait(), gather() or add_done_callback(), nothing records an awaiter for it. The graph now ends at an orphan F, where before it pointed at the task that called shield(). Example: await asyncio.wait([asyncio.shield(t)]). Could we keep a current_task() fallback for that case, and add a test for it?


def _clear_awaited_by_callback(inner):

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: since outer is always set now, this can be a single discard call at the top of _inner_done_callback.

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():
Expand All @@ -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
Expand Down
27 changes: 25 additions & 2 deletions Lib/test/test_asyncio/test_graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -575,10 +575,31 @@ async def main():
'T<anon>',
['s capture_test_stack', 'a deep', 'a c1'],
[
['T<anon>', ['a main', 'a test_stack_shield'], []]
['F', [], [
['T<anon>', ['a main', 'a test_stack_shield'], []],
]],
]
])

async def test_shield_awaited_by_different_task(self):

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a test where the consumer is cancelled while inner is still pending, then check that capture_call_graph(child).awaited_by is empty.

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
Expand All @@ -602,7 +623,9 @@ async def main():
'T<anon>',
['s capture_test_stack', 'a inner', 'a c1'],
[
['T<anon>', ['a main', 'a test_stack_timeout'], []]
['F', [], [
['T<anon>', ['a main', 'a test_stack_timeout'], []],
]],
]
])

Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Loading