Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

asyncio.wait_for should reraise future exception even if timeout expires #84787

Closed
romasku mannequin opened this issue May 12, 2020 · 6 comments
Closed

asyncio.wait_for should reraise future exception even if timeout expires #84787

romasku mannequin opened this issue May 12, 2020 · 6 comments
Labels
3.9 only security fixes topic-asyncio type-bug An unexpected behavior, bug, or error

Comments

@romasku
Copy link
Mannequin

romasku mannequin commented May 12, 2020

BPO 40607
Nosy @njsmith, @asvetlov, @cjerdonek, @1st1, @romasku
PRs
  • bpo-40607: Reraise exception during task cancelation in asyncio.wait_for() #20054
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = None
    closed_at = <Date 2020-05-18.06:09:08.489>
    created_at = <Date 2020-05-12.11:33:07.250>
    labels = ['type-bug', '3.9', 'expert-asyncio']
    title = 'asyncio.wait_for should reraise future exception even if timeout expires'
    updated_at = <Date 2020-05-18.06:09:08.487>
    user = 'https://github.com/romasku'

    bugs.python.org fields:

    activity = <Date 2020-05-18.06:09:08.487>
    actor = 'chris.jerdonek'
    assignee = 'none'
    closed = True
    closed_date = <Date 2020-05-18.06:09:08.489>
    closer = 'chris.jerdonek'
    components = ['asyncio']
    creation = <Date 2020-05-12.11:33:07.250>
    creator = 'Roman Skurikhin'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 40607
    keywords = ['patch']
    message_count = 6.0
    messages = ['368723', '368760', '368761', '368793', '368967', '369183']
    nosy_count = 6.0
    nosy_names = ['njs', 'asvetlov', 'chris.jerdonek', 'python-dev', 'yselivanov', 'Roman Skurikhin']
    pr_nums = ['20054']
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue40607'
    versions = ['Python 3.9']

    @romasku
    Copy link
    Mannequin Author

    romasku mannequin commented May 12, 2020

    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.

    @romasku romasku mannequin added 3.8 only security fixes topic-asyncio type-bug An unexpected behavior, bug, or error labels May 12, 2020
    @cjerdonek
    Copy link
    Member

    Also adding Nathaniel since he's the one that filed bpo-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?

    @njsmith
    Copy link
    Contributor

    njsmith commented May 13, 2020

    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 bpo-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\>


    @1st1
    Copy link
    Member

    1st1 commented May 13, 2020

    I think that in case inner task cancelation fails with some error, asyncio.wait_for should reraise it instead of silently losing it.

    +1.

    @1st1
    Copy link
    Member

    1st1 commented May 15, 2020

    New changeset 382a563 by romasku in branch 'master':
    bpo-40607: Reraise exception during task cancelation in asyncio.wait_for() (GH-20054)
    382a563

    @cjerdonek
    Copy link
    Member

    Thank you again, Roman and all.

    @cjerdonek cjerdonek added 3.9 only security fixes and removed 3.8 only security fixes labels May 18, 2020
    @cjerdonek cjerdonek added 3.9 only security fixes and removed 3.8 only security fixes labels May 18, 2020
    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    3.9 only security fixes topic-asyncio type-bug An unexpected behavior, bug, or error
    Projects
    None yet
    Development

    No branches or pull requests

    3 participants