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: 6 additions & 11 deletions Lib/asyncio/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand All @@ -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

Expand Down Expand Up @@ -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,
Expand Down
30 changes: 29 additions & 1 deletion Lib/test/test_asyncio/test_graph.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import asyncio
import importlib.util
import io
import sys
import types
Expand Down Expand Up @@ -526,10 +527,32 @@ async def main():
'T<anon>',
['s capture_test_stack', 'a deep', 'a c1'],
[
['T<anon>', ['a main', 'a test_stack_gather'], []]
['F', [], [
['T<anon>', ['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

Expand Down Expand Up @@ -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

Expand Down
9 changes: 7 additions & 2 deletions Lib/test/test_external_inspection.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"},
},
)

Expand All @@ -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),
Expand Down Expand Up @@ -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),))
],
},
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 :func:`asyncio.gather`.
Patch by Timofei Ivankov.
Loading