summaryrefslogtreecommitdiffstats
path: root/Lib/asyncio
diff options
context:
space:
mode:
authorINADA Naoki <methane@users.noreply.github.com>2017-05-11 12:18:38 (GMT)
committerGitHub <noreply@github.com>2017-05-11 12:18:38 (GMT)
commit991adca012f5e106c2d4040ce619c696ba6f9c46 (patch)
tree90fa267454273f2e779ceffbeeae2ff84d5f01ca /Lib/asyncio
parentc4750959acbfc3057f12aaec832483ba30898d1c (diff)
downloadcpython-991adca012f5e106c2d4040ce619c696ba6f9c46.zip
cpython-991adca012f5e106c2d4040ce619c696ba6f9c46.tar.gz
cpython-991adca012f5e106c2d4040ce619c696ba6f9c46.tar.bz2
bpo-30048: asyncio: fix Task.cancel() was ignored. (GH-1097)
when there are no more `await` or `yield (from)` before return in coroutine, cancel was ignored. example: async def coro(): asyncio.Task.current_task().cancel() return 42 ... res = await coro() # should raise CancelledError
Diffstat (limited to 'Lib/asyncio')
-rw-r--r--Lib/asyncio/tasks.py7
1 files changed, 6 insertions, 1 deletions
diff --git a/Lib/asyncio/tasks.py b/Lib/asyncio/tasks.py
index 4fbcf3e..e453300 100644
--- a/Lib/asyncio/tasks.py
+++ b/Lib/asyncio/tasks.py
@@ -176,7 +176,12 @@ class Task(futures.Future):
else:
result = coro.throw(exc)
except StopIteration as exc:
- self.set_result(exc.value)
+ if self._must_cancel:
+ # Task is cancelled right before coro stops.
+ self._must_cancel = False
+ self.set_exception(futures.CancelledError())
+ else:
+ self.set_result(exc.value)
except futures.CancelledError:
super().cancel() # I.e., Future.cancel(self).
except Exception as exc: