From a2118a14627256197bddcf4fcecad4c264c1e39d Mon Sep 17 00:00:00 2001 From: Elvis Pranskevichus Date: Wed, 26 Aug 2020 09:42:45 -0700 Subject: bpo-37658: Fix asyncio.wait_for() to respect waited task status (#21894) Currently, if `asyncio.wait_for()` itself is cancelled it will always raise `CancelledError` regardless if the underlying task is still running. This is similar to a race with the timeout, which is handled already. --- Lib/test/test_asyncio/test_tasks.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) (limited to 'Lib/test/test_asyncio/test_tasks.py') diff --git a/Lib/test/test_asyncio/test_tasks.py b/Lib/test/test_asyncio/test_tasks.py index 511961c3200..74fc1e4a421 100644 --- a/Lib/test/test_asyncio/test_tasks.py +++ b/Lib/test/test_asyncio/test_tasks.py @@ -1120,6 +1120,22 @@ class BaseTaskTests: res = loop.run_until_complete(task) self.assertEqual(res, "ok") + def test_wait_for_cancellation_race_condition(self): + def gen(): + yield 0.1 + yield 0.1 + yield 0.1 + yield 0.1 + + loop = self.new_test_loop(gen) + + fut = self.new_future(loop) + loop.call_later(0.1, fut.set_result, "ok") + task = loop.create_task(asyncio.wait_for(fut, timeout=1)) + loop.call_later(0.1, task.cancel) + res = loop.run_until_complete(task) + self.assertEqual(res, "ok") + def test_wait_for_waits_for_task_cancellation(self): loop = asyncio.new_event_loop() self.addCleanup(loop.close) -- cgit v1.2.3