Skip to content

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

Closed

Description

@romasku
mannequin
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']

    Activity

    1. romasku commented on May 12, 2020

      romaskumannequin
      MannequinAuthor

      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.

    2. added
      3.8 (EOL)
      type-bug
      on May 12, 2020
    3. cjerdonek commented on May 13, 2020

      @cjerdonek
      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?

    4. njsmith commented on May 13, 2020

      @njsmith
      Contributor

      makes sense to me

      On Tue, May 12, 2020 at 10:14 PM Chris Jerdonek <[email protected]> wrote:

      Chris Jerdonek <[email protected]> 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 <[email protected]>
      <https://bugs.python.org/issue40607\>


    5. 1st1 commented on May 13, 2020

      @1st1
      Member

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

      +1.

    6. 1st1 commented on May 15, 2020

      @1st1
      Member

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

    7. cjerdonek commented on May 18, 2020

      @cjerdonek
      Member

      Thank you again, Roman and all.

    8. added
      3.9 (EOL)
      and removed
      3.8 (EOL)
      on May 18, 2020
    9. added
      3.9 (EOL)
      and removed
      3.8 (EOL)
      on May 18, 2020
    10. transferred this issue fromon Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

    Metadata

    Metadata

    Assignees

    No one assigned

      Labels

      3.9 (EOL)topic-asynciotype-bug

      Projects

      No projects

        Milestone

        No milestone

        Relationships

        None yet

        Development

        No branches or pull requests

        Issue actions