diff options
author | INADA Naoki <methane@users.noreply.github.com> | 2017-05-11 12:56:42 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-05-11 12:56:42 (GMT) |
commit | 3dc7c52a9f4fb83be3e26e31e2c7cd9dc1cb41a2 (patch) | |
tree | 8b20f9ecf036824ee1e6fbd08b890f4024d7531b /Lib | |
parent | a4465a5bd08f232cf097893006118c82975c3402 (diff) | |
download | cpython-3dc7c52a9f4fb83be3e26e31e2c7cd9dc1cb41a2.zip cpython-3dc7c52a9f4fb83be3e26e31e2c7cd9dc1cb41a2.tar.gz cpython-3dc7c52a9f4fb83be3e26e31e2c7cd9dc1cb41a2.tar.bz2 |
bpo-30048: asyncio: fix Task.cancel() was ignored. (GH-1546)
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
(cherry picked from commit 991adca012f5e106c2d4040ce619c696ba6f9c46)
Diffstat (limited to 'Lib')
-rw-r--r-- | Lib/asyncio/tasks.py | 7 | ||||
-rw-r--r-- | Lib/test/test_asyncio/test_tasks.py | 18 |
2 files changed, 24 insertions, 1 deletions
diff --git a/Lib/asyncio/tasks.py b/Lib/asyncio/tasks.py index f91e70a..d7867d1 100644 --- a/Lib/asyncio/tasks.py +++ b/Lib/asyncio/tasks.py @@ -180,7 +180,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: diff --git a/Lib/test/test_asyncio/test_tasks.py b/Lib/test/test_asyncio/test_tasks.py index 4f05319..5462c80 100644 --- a/Lib/test/test_asyncio/test_tasks.py +++ b/Lib/test/test_asyncio/test_tasks.py @@ -588,6 +588,24 @@ class BaseTaskTests: self.assertFalse(t._must_cancel) # White-box test. self.assertFalse(t.cancel()) + def test_cancel_at_end(self): + """coroutine end right after task is cancelled""" + loop = asyncio.new_event_loop() + self.set_event_loop(loop) + + @asyncio.coroutine + def task(): + t.cancel() + self.assertTrue(t._must_cancel) # White-box test. + return 12 + + t = self.new_task(loop, task()) + self.assertRaises( + asyncio.CancelledError, loop.run_until_complete, t) + self.assertTrue(t.done()) + self.assertFalse(t._must_cancel) # White-box test. + self.assertFalse(t.cancel()) + def test_stop_while_run_in_complete(self): def gen(): |