Skip to content

asyncio.print_call_graph() shows the wrong stack or fails for finished anext() tasks #162235

Open

Description

@deadlovelll

Bug report

For a finished anext() task, asyncio.print_call_graph() can show another task’s stack or raise AttributeError

Repro:

import asyncio


async def gen():
    yield 1
    await asyncio.sleep(0)
    yield 2


async def main():
    agen = gen()
    done = asyncio.create_task(anext(agen), name="done")
    await done

    other = asyncio.create_task(anext(agen), name="other")
    await asyncio.sleep(0)
    asyncio.print_call_graph(done)

    await other
    await agen.aclose()
    asyncio.print_call_graph(other)


asyncio.run(main())

Actual output:

* Task(name='done', id=0x103c2c7d0)
  + Call stack:
  |   File '/Users/timofeiivankov/cpython/Lib/asyncio/tasks.py', line 718, in generator __sleep0()
  |   File '/Users/timofeiivankov/cpython/Lib/asyncio/tasks.py', line 724, in async sleep()
  |   File '/Users/timofeiivankov/cpython/repro.py', line 6, in async generator gen()
Traceback (most recent call last):
  File "/Users/timofeiivankov/cpython/repro.py", line 24, in <module>
    asyncio.run(main())
    ~~~~~~~~~~~^^^^^^^^
  File "/Users/timofeiivankov/cpython/Lib/asyncio/runners.py", line 211, in run
    return runner.run(main)
           ~~~~~~~~~~^^^^^^
  File "/Users/timofeiivankov/cpython/Lib/asyncio/runners.py", line 130, in run
    return self._loop.run_until_complete(task)
           ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^
  File "/Users/timofeiivankov/cpython/Lib/asyncio/base_events.py", line 730, in run_until_complete
    return future.result()
           ~~~~~~~~~~~~~^^
  File "/Users/timofeiivankov/cpython/repro.py", line 21, in main
    asyncio.print_call_graph(other)
    ~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^
  File "/Users/timofeiivankov/cpython/Lib/asyncio/graph.py", line 292, in print_call_graph
    print(format_call_graph(future, depth=depth + 1, limit=limit), file=file)
          ~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/timofeiivankov/cpython/Lib/asyncio/graph.py", line 275, in format_call_graph
    render_level(graph, buf, 0)
    ~~~~~~~~~~~~^^^^^^^^^^^^^^^
  File "/Users/timofeiivankov/cpython/Lib/asyncio/graph.py", line 232, in render_level
    if f.f_generator is None:
       ^^^^^^^^^^^^^
AttributeError: 'NoneType' object has no attribute 'f_generator'

There are two problems:

  1. done just yielded 1, is doesn't called await asyncio.sleep(0). it was done by other later
  2. print_call_graph(other) raises AttributeError after the generator is closed. it shouldn't traverse it

Expected output:

* Task(name='done', id=0x101c287d0)
* Task(name='other', id=0x101bf45c0)

Proposed fix: skip coroutine traversal for finished tasks in capture_call_graph().

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