This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: asyncio.wait_for should reraise future exception even if timeout expires
Type: behavior Stage: resolved
Components: asyncio Versions: Python 3.9
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: Roman Skurikhin, asvetlov, chris.jerdonek, njs, python-dev, yselivanov
Priority: normal Keywords: patch

Created on 2020-05-12 11:33 by Roman Skurikhin, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 20054 merged python-dev, 2020-05-12 13:10
Messages (6)
msg368723 - (view) Author: Roman Skurikhin (Roman Skurikhin) * Date: 2020-05-12 11:33
In https://bugs.python.org/issue32751 asyncio.wait_for behaviour was changed that when we use timeout=... and the timeout expires, it waits until task is canceled. However, in some cases inner task can trigger exception while it handles cancellation. Check the following code:


import asyncio


async def ignore_cancel_and_raise():
    try:
        await asyncio.sleep(20)
    except asyncio.CancelledError:
        raise Exception('Cancellation failed')


async def main():
    try:
        await asyncio.wait_for(ignore_cancel_and_raise(), timeout=1)
    except asyncio.TimeoutError:
        print('Timeout')

asyncio.run(main())


It will print "Timeout" and log a warning that "Task exception was never retrieved".

I think that in case inner task cancelation fails with some error, asyncio.wait_for should reraise it instead of silently losing it.
msg368760 - (view) Author: Chris Jerdonek (chris.jerdonek) * (Python committer) Date: 2020-05-13 05:14
Also adding Nathaniel since he's the one that filed #32751. Nathaniel, do you agree that if an exception occurs while waiting for the cancellation, the exception should be what's raised instead of TimeoutError?
msg368761 - (view) Author: Nathaniel Smith (njs) * (Python committer) Date: 2020-05-13 05:45
makes sense to me

On Tue, May 12, 2020 at 10:14 PM Chris Jerdonek <report@bugs.python.org> wrote:
>
>
> Chris Jerdonek <chris.jerdonek@gmail.com> added the comment:
>
> Also adding Nathaniel since he's the one that filed #32751. Nathaniel, do you agree that if an exception occurs while waiting for the cancellation, the exception should be what's raised instead of TimeoutError?
>
> ----------
> nosy: +chris.jerdonek, njs
>
> _______________________________________
> Python tracker <report@bugs.python.org>
> <https://bugs.python.org/issue40607>
> _______________________________________
msg368793 - (view) Author: Yury Selivanov (yselivanov) * (Python committer) Date: 2020-05-13 18:31
> I think that in case inner task cancelation fails with some error, asyncio.wait_for should reraise it instead of silently losing it.

+1.
msg368967 - (view) Author: Yury Selivanov (yselivanov) * (Python committer) Date: 2020-05-15 20:12
New changeset 382a5635bd10c237c3e23e346b21cde27e48d7fa by romasku in branch 'master':
bpo-40607: Reraise exception during task cancelation in asyncio.wait_for() (GH-20054)
https://github.com/python/cpython/commit/382a5635bd10c237c3e23e346b21cde27e48d7fa
msg369183 - (view) Author: Chris Jerdonek (chris.jerdonek) * (Python committer) Date: 2020-05-18 06:09
Thank you again, Roman and all.
History
Date User Action Args
2022-04-11 14:59:30adminsetgithub: 84787
2020-05-18 06:09:08chris.jerdoneksetstatus: open -> closed
versions: + Python 3.9, - Python 3.8
messages: + msg369183

resolution: fixed
stage: patch review -> resolved
2020-05-15 20:12:11yselivanovsetmessages: + msg368967
2020-05-13 18:31:43yselivanovsetmessages: + msg368793
2020-05-13 05:45:49njssetmessages: + msg368761
2020-05-13 05:14:26chris.jerdoneksetnosy: + njs, chris.jerdonek
messages: + msg368760
2020-05-12 13:10:51python-devsetkeywords: + patch
nosy: + python-dev

pull_requests: + pull_request19363
stage: patch review
2020-05-12 11:33:07Roman Skurikhincreate