Skip to content

asyncio.shield() incorrectly records its caller as awaiting the shielded task in the call graph #162395

Open

Description

@deadlovelll

Bug report

If another task awaits the future returned by shield(), the call graph still shows the task that called shield()

Same kind of bug as #162061

Repro:

import asyncio

async def consume(fut):
    await fut

async def main():
    release = asyncio.Event()
    child = asyncio.create_task(release.wait(), name="child")
    shielded = asyncio.shield(child)
    consumer = asyncio.create_task(consume(shielded), name="consumer", eager_start=True)

    asyncio.print_call_graph(child, limit=0)

    release.set()
    await consumer

asyncio.run(main())

Actual:

* Task(name='child', id=0x1015b49b0)
  + Awaited by:
    * Task(name='Task-1', id=0x1015b47d0)

Problem here is the same - main doesnt actually awaits child at the moment of graph capture

Expected:

* Task(name='child', id=0x1033789b0)
  + Awaited by:
    * Future(id=0x103012c30)
      + Awaited by:
        * Task(name='consumer', id=0x1033683f0)

Proposed fix - link the shielded task to the future returned by shield() instead of the task that called it

I have a fix ready

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