python / python/cpython

`asyncio.print_call_graph()` loses the call stack at asynchronous generators

Open
#156,980 7 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

stdlib topic-asyncio type-bug
Dominant language
Python
Stars
77.2k
Forks
35.9k
PR merge metrics
PR metrics pending

Description

Bug report

Bug description:

asyncio.print_call_graph() stops at the first async gen in the await chain and silently drops all of the call stack below it

Repro:

import asyncio

async def deep():
    await asyncio.Event().wait()

async def producer():
    await deep()
    yield 1

async def consumer():
    async for _ in producer():
        pass

async def main():
    t = asyncio.create_task(consumer(), name='consumer')
    await asyncio.sleep(0.05)
    print(asyncio.format_call_graph(t))

asyncio.run(main())

Expected:

* Task(name='consumer', id=0x105be89b0)
  + Call stack:
  |   File '/Users/timofeiivankov/cpython/Lib/asyncio/locks.py', line 210, in async Event.wait()
  |   File '/Users/timofeiivankov/cpython/repro.py', line 4, in async deep()
  |   File '/Users/timofeiivankov/cpython/repro.py', line 7, in async generator producer()
  |   File '/Users/timofeiivankov/cpython/repro.py', line 11, in async consumer()

Actual:

* Task(name='consumer', id=0x105b089b0)
  + Call stack:
  |   File '/Users/timofeiivankov/cpython/repro.py', line 11, in async consumer()

Proposed fix: expose the wrapped generator as ag_gen and step over the wrapper through it

Have a fix ready for that

CPython versions tested on:

CPython main branch

Operating systems tested on:

macOS

Linked PRs
  • gh-156984

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with asyncio.format_call_graph() and the reproducer in the issue, including the async generator producer() and the consumer task. Check how the call graph traverses the await chain and compare its output with the expected stack. Done means the output retains frames below the async generator; linked PR gh-156984 indicates work is already underway.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
devtools
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.