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: Incorrect traceback when future's exception is raised multiple times
Type: behavior Stage: patch review
Components: asyncio Versions: Python 3.11, Python 3.10, Python 3.9
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: asvetlov, gvanrossum, iritkatriel, kumaraditya, lilydjwg, vishalpandeyvip, yselivanov
Priority: normal Keywords: easy, patch

Created on 2021-11-29 11:55 by iritkatriel, last changed 2022-04-11 14:59 by admin.

Pull Requests
URL Status Linked Edit
PR 30274 open kumaraditya, 2021-12-27 14:38
Messages (7)
msg407265 - (view) Author: Irit Katriel (iritkatriel) * (Python committer) Date: 2021-11-29 11:55
Background: 
https://github.com/python/cpython/pull/29780#issuecomment-981170548
https://github.com/python/cpython/pull/29780#issuecomment-981260365

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

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
msg407308 - (view) Author: Guido van Rossum (gvanrossum) * (Python committer) Date: 2021-11-29 18:21
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.
msg407485 - (view) Author: Vishal Pandey (vishalpandeyvip) * Date: 2021-12-01 20:04
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?
msg407486 - (view) Author: Irit Katriel (iritkatriel) * (Python committer) Date: 2021-12-01 20:12
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.
msg407488 - (view) Author: Vishal Pandey (vishalpandeyvip) * Date: 2021-12-01 20:26
thanks, It was my mistake, bug reproduced.
msg414813 - (view) Author: Irit Katriel (iritkatriel) * (Python committer) Date: 2022-03-09 20:24
Closed issue46954 as a duplicate of this.
msg415478 - (view) Author: Irit Katriel (iritkatriel) * (Python committer) Date: 2022-03-18 09:37
Closed issue42682 as duplicate of this.
History
Date User Action Args
2022-04-11 14:59:52adminsetgithub: 90082
2022-03-18 09:58:44lilydjwgsetnosy: + lilydjwg
2022-03-18 09:37:26iritkatrielsetmessages: + msg415478
2022-03-18 09:37:02iritkatriellinkissue42682 superseder
2022-03-09 20:24:53iritkatrielsetmessages: + msg414813
2022-03-09 20:24:30iritkatriellinkissue46954 superseder
2021-12-27 14:38:42kumaradityasetkeywords: + patch
nosy: + kumaraditya

pull_requests: + pull_request28489
stage: needs patch -> patch review
2021-12-01 20:26:07vishalpandeyvipsetmessages: + msg407488
2021-12-01 20:12:11iritkatrielsetmessages: + msg407486
2021-12-01 20:04:03vishalpandeyvipsetnosy: + vishalpandeyvip
messages: + msg407485
2021-11-29 18:21:26gvanrossumsetkeywords: + easy, - patch

messages: + msg407308
stage: patch review -> needs patch
2021-11-29 12:00:06iritkatrielsetpull_requests: - pull_request28067
2021-11-29 11:57:08iritkatrielsetkeywords: + patch
stage: patch review
pull_requests: + pull_request28067
2021-11-29 11:55:26iritkatrielcreate