Skip to content

asyncio.print_call_graph() raises AttributeError on a finished task #156408

Description

@deadlovelll

Bug report

Bug description:

asyncio.print_call_graph() crashes instead of printing the graph of a task that has already finished.

Repro:

import asyncio

async def main():
    t = asyncio.create_task(asyncio.sleep(0))
    await t
    asyncio.print_call_graph(t)

asyncio.run(main())

Actual output:

AttributeError: 'NoneType' object has no attribute 'f_generator'

But expected:

* Task(name='Task-2', id=0x103fc49b0)

Proposed fix is to add None checks for cr_frame:

    while coro is not None:
        if hasattr(coro, 'cr_await'):
            # A native coroutine or duck-type compatible iterator
            if coro.cr_frame is not None:
                st.append(FrameCallGraphEntry(coro.cr_frame))
            coro = coro.cr_await
        elif hasattr(coro, 'ag_await'):
            # A native async generator or duck-type compatible iterator
            st.append(FrameCallGraphEntry(coro.ag_frame))
            coro = coro.ag_await
        else:
            break

I have fix for that

CPython versions tested on:

CPython main branch

Operating systems tested on:

macOS

Linked PRs

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