Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cpython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cpython
Commits
863b6749
Commit
863b6749
authored
May 29, 2018
by
Yury Selivanov
Committed by
GitHub
May 29, 2018
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
bpo-32684: Fix gather to propagate cancel of itself with return_exceptions (GH-7209)
parent
1cee216c
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
46 additions
and
2 deletions
+46
-2
Doc/library/asyncio-task.rst
Doc/library/asyncio-task.rst
+4
-0
Lib/asyncio/tasks.py
Lib/asyncio/tasks.py
+13
-1
Lib/test/test_asyncio/test_tasks.py
Lib/test/test_asyncio/test_tasks.py
+28
-1
Misc/NEWS.d/next/Library/2018-05-29-12-51-18.bpo-32684.ZEIism.rst
...S.d/next/Library/2018-05-29-12-51-18.bpo-32684.ZEIism.rst
+1
-0
No files found.
Doc/library/asyncio-task.rst
View file @
863b6749
...
...
@@ -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>`,
...
...
Lib/asyncio/tasks.py
View file @
863b6749
...
...
@@ -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
():
...
...
@@ -599,6 +600,11 @@ class _GatheringFuture(futures.Future):
for
child
in
self
.
_children
:
if
child
.
cancel
():
ret
=
True
if
ret
:
# 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
...
...
@@ -673,7 +679,13 @@ def gather(*coros_or_futures, loop=None, return_exceptions=False):
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
=
[]
...
...
Lib/test/test_asyncio/test_tasks.py
View file @
863b6749
...
...
@@ -2037,7 +2037,7 @@ class BaseTaskTests:
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
()
...
...
@@ -2067,6 +2067,33 @@ class BaseTaskTests:
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
...
...
Misc/NEWS.d/next/Library/2018-05-29-12-51-18.bpo-32684.ZEIism.rst
0 → 100644
View file @
863b6749
Fix gather to propagate cancellation of itself even with return_exceptions.
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment