Skip to content

create_task does not pass name parameter to event loop #112622

Closed
@ordinary-jamie

Description

@ordinary-jamie

Bug report

Bug description:

The name parameter in the asyncio.create_task function is never passed to the event loop.

Task names are instead only ever set by the asyncio.create_task function and custom implementations of the event loop will not ever see the task name.

A crude demonstration of this issue is shown below

import asyncio
from unittest import mock


class TestLoop(asyncio.BaseEventLoop):
    def create_task(self, coro, *, name=None, context=None):
        if coro.__name__ == "sleep":
            assert name == "bar"

        return super().create_task(coro, name=name, context=context)


class TestPolicy(asyncio.DefaultEventLoopPolicy):
    def new_event_loop(self) -> asyncio.AbstractEventLoop:
        loop = TestLoop()
        loop._process_events = mock.Mock()
        loop._write_to_self = mock.Mock()
        loop._write_to_self.return_value = None
        loop._selector = mock.Mock()
        loop._selector.select.return_value = ()
        loop.shutdown_ag_run = False
        return loop


async def foo():
    asyncio.create_task(
        asyncio.sleep(0.1),
        name="bar",
    )


def main():
    asyncio.set_event_loop_policy(TestPolicy())

    asyncio.run(foo())


if __name__ == "__main__":
    main()

CPython versions tested on:

3.12

Operating systems tested on:

macOS

Linked PRs

Metadata

Metadata

Assignees

No one assigned

    Labels

    stdlibPython modules in the Lib dirtopic-asynciotype-bugAn unexpected behavior, bug, or error

    Projects

    Status

    Done

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions