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.
diff --git a/Lib/asyncio/tasks.py b/Lib/asyncio/tasks.py
index 7ecec96..8b05434 100644
--- a/Lib/asyncio/tasks.py
+++ b/Lib/asyncio/tasks.py
@@ -465,9 +465,12 @@
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()