[Python-checkins] bpo-37658: Fix asyncio.wait_for() to respect waited task status (GH-21894) (#21965)
Miss Islington (bot)
webhook-mailer at python.org
Wed Aug 26 14:26:38 EDT 2020
https://github.com/python/cpython/commit/6e1954cd8286e083e7f8d09516d91b6b15769a4e
commit: 6e1954cd8286e083e7f8d09516d91b6b15769a4e
branch: 3.8
author: Miss Islington (bot) <31488909+miss-islington at users.noreply.github.com>
committer: GitHub <noreply at github.com>
date: 2020-08-26T11:26:28-07:00
summary:
bpo-37658: Fix asyncio.wait_for() to respect waited task status (GH-21894) (#21965)
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.
(cherry picked from commit a2118a14627256197bddcf4fcecad4c264c1e39d)
Co-authored-by: Elvis Pranskevichus <elvis at magic.io>
files:
A Misc/NEWS.d/next/Library/2020-08-15-15-21-40.bpo-37658.f9nivB.rst
M Lib/asyncio/tasks.py
M Lib/test/test_asyncio/test_tasks.py
diff --git a/Lib/asyncio/tasks.py b/Lib/asyncio/tasks.py
index 66e81f9200a36..373be37fb4bdd 100644
--- a/Lib/asyncio/tasks.py
+++ b/Lib/asyncio/tasks.py
@@ -475,9 +475,12 @@ async def wait_for(fut, timeout, *, loop=None):
try:
await waiter
except exceptions.CancelledError:
- fut.remove_done_callback(cb)
- fut.cancel()
- raise
+ if fut.done():
+ return fut.result()
+ else:
+ fut.remove_done_callback(cb)
+ fut.cancel()
+ raise
if fut.done():
return fut.result()
diff --git a/Lib/test/test_asyncio/test_tasks.py b/Lib/test/test_asyncio/test_tasks.py
index c21b06938fe3f..a70dc0a87e1c4 100644
--- a/Lib/test/test_asyncio/test_tasks.py
+++ b/Lib/test/test_asyncio/test_tasks.py
@@ -861,6 +861,22 @@ def gen():
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)
diff --git a/Misc/NEWS.d/next/Library/2020-08-15-15-21-40.bpo-37658.f9nivB.rst b/Misc/NEWS.d/next/Library/2020-08-15-15-21-40.bpo-37658.f9nivB.rst
new file mode 100644
index 0000000000000..694fbbbf346dc
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2020-08-15-15-21-40.bpo-37658.f9nivB.rst
@@ -0,0 +1,2 @@
+:meth:`asyncio.wait_for` now properly handles races between cancellation of
+itself and the completion of the wrapped awaitable.
More information about the Python-checkins
mailing list