python / python/cpython

asyncio eager tasks have no awaited_by edge during their first step

Open
#157,299 0 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:

Task that created by eager task factory runs its first step before the awaited_by edge is registered. During this step the graph shows that this task is awaited by nobody

Repro:

import asyncio

async def child():
    asyncio.print_call_graph()
    await asyncio.sleep(0)

async def main():
    await asyncio.gather(child())

loop = asyncio.new_event_loop()
loop.set_task_factory(asyncio.eager_task_factory)
loop.run_until_complete(main())

Expected:

* Task(name='Task-2', id=0x103544b90)
  + Call stack:
  |   File '/Users/timofeiivankov/cpython/repro.py', line 4, in async child()
  + Awaited by:
    * Task(name='Task-1', id=0x103544d70)
      + Call stack:
      |   File '/Users/timofeiivankov/cpython/repro.py', line 8, in async main()

Actually:

* Task(name='Task-2', id=0x103058b90)
  + Call stack:
  |   File '/Users/timofeiivankov/cpython/repro.py', line 4, in async child()

With the default task factory it works fine as expected

Proposed fix: Register the edge inside the eager start and discard it when the step ends

I have a fix ready

CPython versions tested on:

CPython main branch

Operating systems tested on:

macOS

Linked PRs
  • gh-157300

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

Run the provided asyncio reproduction with eager_task_factory and compare its call graph with the default task factory. Inspect the eager start and awaited_by registration described in the report; done means the first child step shows its parent in the “Awaited by” section without breaking the existing behavior.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.