Skip to content

asyncio.gather() leaves stale awaited_by links when argument validation fails #161083

Open

Description

@deadlovelll

Bug report

A failed asyncio.gather() call can leave stale task nodes

Repro:

import asyncio

async def main():
    task = asyncio.create_task(asyncio.sleep(0))
    try:
        result = await asyncio.gather(task, None)
    except TypeError as e:
        pass

    asyncio.print_call_graph(task)
    await task

asyncio.run(main())

The problem here is that you can't pass None to gather(), it raises TypeError, so nobody awaits task.

Actual:

* Task(name='Task-2', id=0x1038c07d0)
  + Call stack:
  |   File '/Users/timofeiivankov/cpython/Lib/asyncio/tasks.py', line 718, in async sleep()
  + Awaited by:
    * Task(name='Task-1', id=0x1038c05f0)
      + Call stack:
      |   File '/Users/timofeiivankov/cpython/repro.py', line 10, in async main()

Expected:

* Task(name='Task-2', id=0x10306c9b0)
  + Call stack:
  |   File '/Users/timofeiivankov/cpython/Lib/asyncio/tasks.py', line 718, in async sleep()

Proposed fix: register task nodes only after all gather() arguments have been successfully processed.

I'm gonna work on a fix for this

CPython versions tested on:

CPython main branch

Operating systems tested on:

macOS

Linked PRs

Activity

  1. picnixz commented on Nov 1, 2026

    @picnixz
  2. deadlovelll commented on Nov 1, 2026

    @deadlovelll
    Author
  3. picnixz commented on Nov 1, 2026

    @picnixz
  4. kumaraditya303 commented on Nov 2, 2026

    @kumaraditya303
    Contributor

    I'll review this one but I don't encourage working on such issues which no one would hit in real world.

  5. deadlovelll commented on Nov 2, 2026

    @deadlovelll
    ContributorAuthor

    9. I'll review this one but I don't encourage working on such issues which no one would hit in real world.

    Thanks, i'll keep in mind for the future issues

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