From 382a5635bd10c237c3e23e346b21cde27e48d7fa Mon Sep 17 00:00:00 2001 From: romasku Date: Fri, 15 May 2020 23:12:05 +0300 Subject: bpo-40607: Reraise exception during task cancelation in asyncio.wait_for() (GH-20054) Currently, if asyncio.wait_for() timeout expires, it cancels inner future and then always raises TimeoutError. In case those future is task, it can handle cancelation mannually, and those process can lead to some other exception. Current implementation silently loses thoses exception. To resolve this, wait_for will check was the cancelation successfull or not. In case there was exception, wait_for will reraise it. Co-authored-by: Roman Skurikhin --- Lib/asyncio/tasks.py | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) (limited to 'Lib/asyncio/tasks.py') diff --git a/Lib/asyncio/tasks.py b/Lib/asyncio/tasks.py index 717837d856..f5de1a2eea 100644 --- a/Lib/asyncio/tasks.py +++ b/Lib/asyncio/tasks.py @@ -496,7 +496,15 @@ async def wait_for(fut, timeout, *, loop=None): # after wait_for() returns. # See https://bugs.python.org/issue32751 await _cancel_and_wait(fut, loop=loop) - raise exceptions.TimeoutError() + # In case task cancellation failed with some + # exception, we should re-raise it + # See https://bugs.python.org/issue40607 + try: + fut.result() + except exceptions.CancelledError as exc: + raise exceptions.TimeoutError() from exc + else: + raise exceptions.TimeoutError() finally: timeout_handle.cancel() -- cgit v1.2.1