Skip to content

asyncio.print_call_graph() loses the call stack at anext(agen, default) #157032

Closed as duplicate

Description

@deadlovelll

Bug report

Bug description:

Adding a default to anext() hides everything the generator is waiting on

Repro:

import asyncio

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

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

async def worker():
    await anext(rows(), None)

async def main():
    t = asyncio.create_task(worker(), name='worker')
    await asyncio.sleep(0.05)
    asyncio.print_call_graph(t)
    t.cancel()

asyncio.run(main())

Expected:

* Task(name='worker', id=0x103a049b0)
  + 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 rows()
  |   File '/Users/timofeiivankov/cpython/repro.py', line 11, in async worker()

Actual:

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

Proposed fix: expose ag_gen on anext_awaitable like #156984 do

Important: Currently this issue have a blocker from #156984 (issue - #156980) because it depends on the fix of it. I have a fix ready and will open a PR once #156984 is merged

CPython versions tested on:

CPython main branch

Operating systems tested on:

macOS

Activity

  1. VimalN2005 commented on Sep 12, 2026

    @VimalN2005

    Hi!

    This is a great catch. The 2-argument form of anext(agen, default) handles StopAsyncIteration via an internal wrapper, which appears to drop the waiting generator's frame from the call graph traversal.

    I would like to work on fixing the edge propagation in asyncio.print_call_graph() so that the waiting generator's stack is preserved, and add corresponding test coverage under Lib/test/test_asyncio/.

    May I take this up? Please let me know if I can go ahead.

  2. deadlovelll commented on Sep 12, 2026

    @deadlovelll
    ContributorAuthor

    Hi!

    This is a great catch. The 2-argument form of anext(agen, default) handles StopAsyncIteration via an internal wrapper, which appears to drop the waiting generator's frame from the call graph traversal.

    I would like to work on fixing the edge propagation in asyncio.print_call_graph() so that the waiting generator's stack is preserved, and add corresponding test coverage under Lib/test/test_asyncio/.

    May I take this up? Please let me know if I can go ahead.

    Hello, there are disclaimer in the end of issue text

    Important: Currently this issue have a blocker from #156984 (issue - #156980) because it depends on the fix of it. I have a fix ready and will open a PR once #156984 is merged

  3. moved this from Todo to Done in asyncioon Sep 19, 2026
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
      Done

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions