Skip to content

Eager task's explicitly set name is replaced #143173

@xitop

Description

@xitop

Bug report

Bug description:

In Python 3.13, the name assgined to an eager task is set too late. This is a known issue gh-128308.
However, when the name is being set, another name given to the task in meantime gets overwritten.

import asyncio

async def coro():
    this_task = asyncio.current_task()
    print(this_task.get_name())   # not set yet
    this_task.set_name("BBB")
    print(this_task.get_name())   # new name was set here
    await asyncio.sleep(0)
    print(this_task.get_name())   # name from create_task overwrites

async def main():
    asyncio.get_running_loop().set_task_factory(asyncio.eager_task_factory)
    task = asyncio.create_task(coro(), name="AAA")
    await task

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

This program prints on 3.12 and 3.13 branches (tested with 3.12.12 and 3.13.11):

Task-2
BBB
AAA

(Note: 3.11 did not have eager tasks and on 3.14 is everything fine):

The first line is explained by the mentioned issue #128308. The second one confirms the name was set.
The third line is IMO not correct, because the last modification should win.


A variation of the reproducer without setting the name in create_task behaves a little bit different.

    task = asyncio.create_task(coro())    # removed name="AAA"

It prints an expected output on 3.12:

Task-2
BBB
BBB

but exposes another known issue in 3.13:

Task-2
BBB
None  <-- it's a string

CPython versions tested on:

3.13

Operating systems tested on:

Linux

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