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.
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").
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.
Activity
kthreadd commented on Apr 17, 2025
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").
sergiosacj commented on Apr 17, 2025
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()"
That's right, this code assumed that set_name was called inside create_task. I made this example based on this test here.
kthreadd commented on Apr 17, 2025
@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.
picnixz commented on May 9, 2025
We tracked the more general issue in #133745.