Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

create_task does not pass name parameter to event loop #112622

Closed
ordinary-jamie opened this issue Dec 2, 2023 · 1 comment
Closed

create_task does not pass name parameter to event loop #112622

ordinary-jamie opened this issue Dec 2, 2023 · 1 comment
Labels
stdlib Python modules in the Lib dir topic-asyncio type-bug An unexpected behavior, bug, or error

Comments

@ordinary-jamie
Copy link
Contributor

ordinary-jamie commented Dec 2, 2023

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

@ordinary-jamie ordinary-jamie added the type-bug An unexpected behavior, bug, or error label Dec 2, 2023
@AlexWaygood AlexWaygood added topic-asyncio stdlib Python modules in the Lib dir labels Dec 2, 2023
@ordinary-jamie
Copy link
Contributor Author

Created a quick PR to address this: #112623

gvanrossum pushed a commit that referenced this issue Dec 13, 2023
This affects task creation through either `asyncio.create_task()` or `TaskGroup.create_task()` -- the redundant call to `task.set_name()` is skipped. We still call `set_name()` when a task factory is involved, because the task factory call signature (unfortunately) doesn't take a `name` argument.
aisk pushed a commit to aisk/cpython that referenced this issue Feb 11, 2024
This affects task creation through either `asyncio.create_task()` or `TaskGroup.create_task()` -- the redundant call to `task.set_name()` is skipped. We still call `set_name()` when a task factory is involved, because the task factory call signature (unfortunately) doesn't take a `name` argument.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stdlib Python modules in the Lib dir topic-asyncio type-bug An unexpected behavior, bug, or error
Projects
Status: Done
Development

No branches or pull requests

3 participants