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

Incorrect traceback when future's exception is raised multiple times #90082

Closed
iritkatriel opened this issue Nov 29, 2021 · 8 comments
Closed
Labels
3.10 only security fixes 3.11 only security fixes 3.12 bugs and security fixes topic-asyncio type-bug An unexpected behavior, bug, or error

Comments

@iritkatriel
Copy link
Member

BPO 45924
Nosy @gvanrossum, @asvetlov, @lilydjwg, @1st1, @iritkatriel, @kumaraditya303, @vishalpandeyvip
PRs
  • bpo-45924: Fix asyncio incorrect traceback when future's exception is raised multiple times #30274
  • 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 = None
    created_at = <Date 2021-11-29.11:55:26.419>
    labels = ['easy', 'type-bug', '3.9', '3.10', '3.11', 'expert-asyncio']
    title = "Incorrect traceback when future's exception is raised multiple times"
    updated_at = <Date 2022-03-18.09:58:44.181>
    user = 'https://github.com/iritkatriel'

    bugs.python.org fields:

    activity = <Date 2022-03-18.09:58:44.181>
    actor = 'lilydjwg'
    assignee = 'none'
    closed = False
    closed_date = None
    closer = None
    components = ['asyncio']
    creation = <Date 2021-11-29.11:55:26.419>
    creator = 'iritkatriel'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 45924
    keywords = ['patch', 'easy']
    message_count = 7.0
    messages = ['407265', '407308', '407485', '407486', '407488', '414813', '415478']
    nosy_count = 7.0
    nosy_names = ['gvanrossum', 'asvetlov', 'lilydjwg', 'yselivanov', 'iritkatriel', 'kumaraditya', 'vishalpandeyvip']
    pr_nums = ['30274']
    priority = 'normal'
    resolution = None
    stage = 'patch review'
    status = 'open'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue45924'
    versions = ['Python 3.9', 'Python 3.10', 'Python 3.11']

    @iritkatriel
    Copy link
    Member Author

    Background:
    #29780 (comment)
    #29780 (comment)

    ######################################################

    import asyncio, traceback
    
    async def raise_after(fut, delay):
        await asyncio.sleep(delay)
        fut.set_exception(TypeError(42))
    
    async def main():
        loop = asyncio.get_running_loop()
        fut = loop.create_future()
        loop.create_task(
            raise_after(fut, 1))
    
        print('hello ...')
    
        for i in range(3):
            try:
                print(await fut)
            except Exception as e:
                traceback.print_exception(e)
    
    asyncio.run(main())
    ######################################################

    Output (traceback accumulates a frame each time):

    hello ...
    Traceback (most recent call last):
      File "/Users/iritkatriel/src/cpython-1/as.py", line 17, in main
        print(await fut)
              ^^^^^^^^^
    TypeError: 42
    Traceback (most recent call last):
      File "/Users/iritkatriel/src/cpython-1/as.py", line 17, in main
        print(await fut)
              ^^^^^^^^^
      File "/Users/iritkatriel/src/cpython-1/as.py", line 17, in main
        print(await fut)
              ^^^^^^^^^
    TypeError: 42
    Traceback (most recent call last):
      File "/Users/iritkatriel/src/cpython-1/as.py", line 17, in main
        print(await fut)
              ^^^^^^^^^
      File "/Users/iritkatriel/src/cpython-1/as.py", line 17, in main
        print(await fut)
              ^^^^^^^^^
      File "/Users/iritkatriel/src/cpython-1/as.py", line 17, in main
        print(await fut)
              ^^^^^^^^^
    TypeError: 42

    @iritkatriel iritkatriel added 3.9 only security fixes 3.10 only security fixes 3.11 only security fixes topic-asyncio type-bug An unexpected behavior, bug, or error labels Nov 29, 2021
    @gvanrossum
    Copy link
    Member

    There's a similar issue with concurrent.futures.Future, and really, anything that stores exceptions and later raises them can get in trouble if there's a way to get the exception raised multiple times. This is rarely noticed because usually the exception isn't raised more than once.

    We need separate bpo issues for the other cases.

    The fix is easy enough (separately store a traceback and raise using e.with_traceback(tb)) so I'm marking this as an easy issue.

    @vishalpandeyvip
    Copy link
    Mannequin

    vishalpandeyvip mannequin commented Dec 1, 2021

    hey Irit, Is this issue platform-dependent? I am on ubuntu 20.04, and running python 3.10. I am not getting the same error that you have mentioned below.
    This is what I am getting.
    ###################################

    hello ...
    Traceback (most recent call last):
      File "error.py", line 17, in main
        print(await fut)
    TypeError: 42
    
    During handling of the above exception, another exception occurred:
    
    Traceback (most recent call last):
      File "error.py", line 21, in <module>
        asyncio.run(main())
      File "/usr/lib/python3.8/asyncio/runners.py", line 44, in run
        return loop.run_until_complete(main)
      File "/usr/lib/python3.8/asyncio/base_events.py", line 616, in run_until_complete
        return future.result()
      File "error.py", line 19, in main
        traceback.print_exception(e)
    TypeError: print_exception() missing 2 required positional arguments: 'value' and 'tb'

    ###########################################################
    and if I replace traceback.print_exception method with the print method, It runs perfectly fine, printing

    hello ...
    42
    42
    42

    Can you please help me with this issue?

    @iritkatriel
    Copy link
    Member Author

    Vishal, your output shows you’re running 3.8, not 3.10.

    traceback.print_exception() used to require 3 parameters, now it can make do with just one. See the docs for the details.

    @vishalpandeyvip
    Copy link
    Mannequin

    vishalpandeyvip mannequin commented Dec 1, 2021

    thanks, It was my mistake, bug reproduced.

    @iritkatriel
    Copy link
    Member Author

    Closed bpo-46954 as a duplicate of this.

    @iritkatriel
    Copy link
    Member Author

    Closed bpo-42682 as duplicate of this.

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    @iritkatriel iritkatriel added 3.12 bugs and security fixes and removed 3.9 only security fixes labels Jun 6, 2022
    @kumaraditya303
    Copy link
    Contributor

    Fixed by #30274

    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    3.10 only security fixes 3.11 only security fixes 3.12 bugs and security fixes topic-asyncio type-bug An unexpected behavior, bug, or error
    Projects
    None yet
    Development

    No branches or pull requests

    3 participants