Skip to content
Merged
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
20 changes: 7 additions & 13 deletions Lib/asyncio/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand All @@ -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

Expand Down Expand Up @@ -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,
Expand Down
36 changes: 35 additions & 1 deletion Lib/test/test_asyncio/test_graph.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import asyncio
import io
import unittest
from unittest import mock


# To prevent a warning "test altered the execution environment"
Expand Down Expand Up @@ -198,10 +199,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 @@ -562,6 +585,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

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 @@ -793,6 +793,8 @@ async def main():
]
for task_name in ["Task-1", "Task-2"]:
self.assertIn(task_name, tasks_names)
future_name, = (node.task_name for node in stack_trace[0].awaited_by
if node.is_future)

# Now ensure that the awaited_by_relationships are correct
id_to_task = {
Expand All @@ -809,7 +811,8 @@ async def main():
task_name_to_awaited_by,
{
"Task-1": set(),
"Task-2": {"Task-1"},
"Task-2": {future_name},
future_name: {"Task-1"},
},
)

Expand All @@ -825,6 +828,7 @@ async def main():
coroutine_stacks,
{
"Task-1": [(tuple([script_name, 21, "main"]),)],
future_name: [()],
"Task-2": [
(
tuple([script_name, 11, "deep"]),
Expand All @@ -849,7 +853,8 @@ async def main():
awaited_by_coroutine_stacks,
{
"Task-1": [],
"Task-2": [
"Task-2": [(future_name, ())],
future_name: [
("Task-1", (tuple([script_name, 21, "main"]),))
],
},
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