Skip to content

asyncio.Task.print_stack() shows an incorrect exception location #161166

Open

Description

@deadlovelll

Bug report

Task.print_stack() shows the line executed in finally instead of the line that raised the exception

Repro:

import asyncio

async def fail():
    try:
        1 / 1
    finally:
        cleaned = True

async def main():
    task = asyncio.create_task(fail())
    await asyncio.sleep(0)
    task.exception()
    task.print_stack()

asyncio.run(main())

Actual:

  File "/Users/timofeiivankov/cpython/repro.py", line 7, in fail
    cleaned = True
ZeroDivisionError: division by zero

Bool assignment is not really a cause of the ZeroDivisionError

Expected:

Traceback for <Task finished name='Task-2' coro=<fail() done, defined at /Users/timofeiivankov/cpython/repro.py:3> exception=ZeroDivisionError('division by zero')> (most recent call last):
  File "/Users/timofeiivankov/cpython/repro.py", line 5, in fail
    1 / 0
ZeroDivisionError: division by zero

Proposed fix: use the traceback line number instead of the frame’s current line number

I'll work on this

CPython versions tested on:

CPython main branch

Operating systems tested on:

macOS

Linked PRs

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

    stdlibStandard Library Python modules in the Lib/ directorytopic-asynciotype-bugAn unexpected behavior, bug, or error

    Projects

    • Status
      Todo

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions