Skip to content

bpo-32684: Fix gather to propagate cancel of itself with return_exceptions #7209

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 29, 2018
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
4 changes: 4 additions & 0 deletions Doc/library/asyncio-task.rst
Original file line number Diff line number Diff line change
Expand Up @@ -640,6 +640,10 @@ Task functions
outer Future is *not* cancelled in this case. (This is to prevent the
cancellation of one child to cause other children to be cancelled.)

.. versionchanged:: 3.7.0
If the *gather* itself is cancelled, the cancellation is propagated
regardless of *return_exceptions*.

.. function:: iscoroutine(obj)

Return ``True`` if *obj* is a :ref:`coroutine object <coroutine>`,
Expand Down
14 changes: 13 additions & 1 deletion Lib/asyncio/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -591,6 +591,7 @@ class _GatheringFuture(futures.Future):
def __init__(self, children, *, loop=None):
super().__init__(loop=loop)
self._children = children
self._cancel_requested = False

def cancel(self):
if self.done():
Expand All @@ -599,6 +600,11 @@ def cancel(self):
for child in self._children:
if child.cancel():
ret = True
if ret:
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't follow why future cancellation should be propagated but if children are tasks nothing should be done.

Copy link
Member Author

Choose a reason for hiding this comment

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

See https://bugs.python.org/issue26923 and, specifically, Task.cancel(): if the task is done/cancelled, we don't raise CancelledError. Similarly for gather(), if all of its tasks are already done, we don't raise it.

This seems to be a super rare edge case.

Copy link
Contributor

Choose a reason for hiding this comment

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

Got it.

# If any child tasks were actually cancelled, we should
# propagate the cancellation request regardless of
# *return_exceptions* argument. See issue 32684.
self._cancel_requested = True
return ret


Expand Down Expand Up @@ -673,7 +679,13 @@ def _done_callback(fut):
res = fut.result()
results.append(res)

outer.set_result(results)
if outer._cancel_requested:
# If gather is being cancelled we must propagate the
# cancellation regardless of *return_exceptions* argument.
# See issue 32684.
outer.set_exception(futures.CancelledError())
else:
outer.set_result(results)

arg_to_fut = {}
children = []
Expand Down
29 changes: 28 additions & 1 deletion Lib/test/test_asyncio/test_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -2037,7 +2037,7 @@ def test_cancel_blocking_wait_for(self):
def test_cancel_wait_for(self):
self._test_cancel_wait_for(60.0)

def test_cancel_gather(self):
def test_cancel_gather_1(self):
"""Ensure that a gathering future refuses to be cancelled once all
children are done"""
loop = asyncio.new_event_loop()
Expand Down Expand Up @@ -2067,6 +2067,33 @@ def cancelling_callback(_):
self.assertFalse(gather_task.cancelled())
self.assertEqual(gather_task.result(), [42])

def test_cancel_gather_2(self):
loop = asyncio.new_event_loop()
self.addCleanup(loop.close)

async def test():
time = 0
while True:
time += 0.05
await asyncio.gather(asyncio.sleep(0.05),
return_exceptions=True,
loop=loop)
if time > 1:
return

async def main():
qwe = asyncio.Task(test())
await asyncio.sleep(0.2)
qwe.cancel()
try:
await qwe
except asyncio.CancelledError:
pass
else:
self.fail('gather did not propagate the cancellation request')

loop.run_until_complete(main())

def test_exception_traceback(self):
# See http://bugs.python.org/issue28843

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix gather to propagate cancellation of itself even with return_exceptions.