GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — `create_task(name=...)` silently fails to set name with custom task_factory after gh-128308 · Issue #132593 · python/cpython

create_task(name=...) silently fails to set name with custom task_factory after gh-128308 #132593

New issue
Copy link
New issue
Copy link
Closed as duplicate
Closed as duplicate
create_task(name=...) silently fails to set name with custom task_factory after gh-128308#132593
Copy link
Labels
stdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytopic-asynciotype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error

Description

@sergiosacj
sergiosacj
opened on Apr 16, 2025
Issue body actions

Bug report

Bug description:

During a rebuild of all packages in Debian unstable, the uvloop test suite failed due to a new behavior in asyncio.

This suite runs each test twice: once with uvloop implementation (which passes) and once with Python’s asyncio implementation (which fails).

Since

Commit: 38a99568763604ccec5d5027f0658100ad76876f
PR: https://github.com/python/cpython/pull/128768

asyncio.BaseEventLoop.create_task() no longer calls set_name(name). This breaks compatibility with any factory that does not set the name manually.

Expected behavior

When using create_task(..., name="example name"), the task's name should always be set - even when using a custom task factory - as it was before the regression.

Actual behavior

With a custom factory that returns a subclassed asyncio.Task, the task name is None unless the factory manually calls set_name(name). This is a silent behavioral change that breaks code relying on set_name() being called by the event loop.

My Environment

Python version: 3.13.2
Debian testing

Script to reproduce

import asyncio

async def main():
    names = []

    async def get_name():
        names.append(asyncio.current_task().get_name())

    async with asyncio.TaskGroup() as tg:
        tg.create_task(get_name(), name="example name")

    print(names)

asyncio.run(main())  # Output: ['example name']

def factory():
    loop = asyncio.new_event_loop()
    loop.set_task_factory(asyncio.eager_task_factory)
    return loop

asyncio.run(main(), loop_factory=factory)  # Output: ['example name']

# Fails silently with custom task_factory
result = None

class MyTask(asyncio.Task):
    def set_name(self, name):
        global result
        print(name) # never reached
        result = name + "!"

    def get_name(self):
        global result
        return result

def loop_factory():
    loop = asyncio.new_event_loop()
    loop.set_task_factory(asyncio.create_eager_task_factory(MyTask))
    return loop

asyncio.run(main(), loop_factory=loop_factory)  # Output: ['None']

CPython versions tested on:

3.13

Operating systems tested on:

Linux

Reacted by John

Activity

  1. sergiosacj
    added
    type-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
    on Apr 16, 2025
  2. AlexWaygood
    added
    topic-asyncio
    on Apr 16, 2025
  3. github-project-automation
    added this to asyncioon Apr 16, 2025
  4. github-project-automation
    moved this to Todo in asyncioon Apr 16, 2025
  5. kthreadd commented on Apr 17, 2025

    @kthreadd
    kthreadd
    on Apr 17, 2025
    More actions

    Hi @sergiosacj

    Yes, from #128308 on, the asyncio.BaseEventLoop.create_task() no longer calls set_name().

    The name setting operation is actually done in the C function during calling create_task(..., name="example name"), not set by `set_name.

    And by the way, from the code you supplied, the function get_name overwritten by MyTask, so it seems that it's the reason why names.append(asyncio.current_task().get_name()) gets None.

    So, you could get correct name example name if you have a correct get_name, even you didn't set name manually via set_name(), because the name will be set by calling create_task(..., name="example name").

  6. sergiosacj commented on Apr 17, 2025

    @sergiosacj
    sergiosacj
    on Apr 17, 2025
    Author
    More actions

    The name setting operation is actually done in the C function during calling create_task(..., name="example name"), not set by `set_name.

    Thanks @kthreadd, I misinterpreted then.

    When I read the documentation, I thought set_name should be called in this scenario. Maybe the sentence below should be updated?

    "If name is not None, it is set as the name of the task using Task.set_name()"

    And by the way, from the code you supplied, the function get_name overwritten by MyTask, so it seems that it's the reason why names.append(asyncio.current_task().get_name()) gets None.

    That's right, this code assumed that set_name was called inside create_task. I made this example based on this test here.

  7. kthreadd commented on Apr 17, 2025

    @kthreadd
    kthreadd
    on Apr 17, 2025
    More actions

    @sergiosacj, ok, I also noticed that the out of date description in document asyncio.create_task, for the same, test case is also out of date test_set_task_name

    Both of them need to be updated I believe.

  8. picnixz
    added
    stdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directory
    on Apr 17, 2025
  9. picnixz commented on May 9, 2025

    @picnixz
    picnixz
    on May 9, 2025
    Member
    More actions

    We tracked the more general issue in #133745.

  10. kumaraditya303
    closed this as a duplicateof Breaking API Change in Python 3.13.3: Task Factory Signature Change #133745 on May 16, 2025
  11. github-project-automation
    moved this from Todo to Done in asyncioon May 16, 2025
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/ directoryStandard Library Python modules in the Lib/ directorytopic-asynciotype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error

    Projects

    • asyncio
      Status
      Done
      Show more project fields

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

    You are visitor 01402503 — GeezerVison does not store the pages you look up.
    Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.