Skip to content

asyncio.as_completed() doesn't record the awaiting task in the call graph #156523

Description

@deadlovelll

Bug report

Bug description:

A task awaited through asyncio.as_completed() has no "Awaited by" section in the call graph.

Repro

import asyncio

async def worker():
    await asyncio.sleep(3600)

async def consumer(t):
    for f in asyncio.as_completed([t]):
        await f

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

if __name__ == "__main__":
    asyncio.run(main())

Actual output:

* Task(name='worker', id=0x10368c9b0)
  + Call stack:
  |   File '/Users/timofeiivankov/cpython/Lib/asyncio/tasks.py', line 703, in async sleep()
  |   File '/Users/timofeiivankov/cpython/repro.py', line 4, in async worker()

Expected:

* Task(name='worker', id=0x105e349b0)
  + Call stack:
  |   File '/Users/timofeiivankov/cpython/Lib/asyncio/tasks.py', line 707, in async sleep()
  |   File '/Users/timofeiivankov/cpython/repro.py', line 4, in async worker()
  + Awaited by:
    * Task(name='consumer', id=0x105ba9610)
      + Call stack:
      |   File '/Users/timofeiivankov/cpython/Lib/asyncio/queues.py', line 186, in async Queue.get()
      |   File '/Users/timofeiivankov/cpython/Lib/asyncio/tasks.py', line 617, in async _AsCompletedIterator._wait_for_one()
      |   File '/Users/timofeiivankov/cpython/repro.py', line 8, in async consumer()

Proposed fix: Register the awaiting task in _AsCompletedIterator

Have a fix ready

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