Skip to content

Commit fd6a602

Browse files
graingertkumaraditya303gvanrossum
authored
[3.13] gh-133745: Fix asyncio task factory name/context kwarg breaks (#133948)
In 3.13.3 we accidentally broke the interface for custom task factory. Factory authors added workarounds. This PR (for 3.13.4) unbreaks task factories that haven't made a workaround yet while also supporting those that have. NOTE: The custom task factory API will change to what we accidentally released in 3.13.3. Co-authored-by: Kumar Aditya <[email protected]> Co-authored-by: Guido van Rossum <[email protected]>
1 parent ebe54d7 commit fd6a602

File tree

4 files changed

+32
-18
lines changed

4 files changed

+32
-18
lines changed

Doc/library/asyncio-eventloop.rst

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -355,7 +355,7 @@ Creating Futures and Tasks
355355

356356
.. versionadded:: 3.5.2
357357

358-
.. method:: loop.create_task(coro, *, name=None, context=None)
358+
.. method:: loop.create_task(coro, *, name=None, context=None, **kwargs)
359359

360360
Schedule the execution of :ref:`coroutine <coroutine>` *coro*.
361361
Return a :class:`Task` object.
@@ -364,6 +364,11 @@ Creating Futures and Tasks
364364
for interoperability. In this case, the result type is a subclass
365365
of :class:`Task`.
366366

367+
The full function signature is largely the same as that of the
368+
:class:`Task` constructor (or factory) - all of the keyword arguments to
369+
this function are passed through to that interface, except *name*,
370+
or *context* if it is ``None``.
371+
367372
If the *name* argument is provided and not ``None``, it is set as
368373
the name of the task using :meth:`Task.set_name`.
369374

@@ -377,6 +382,13 @@ Creating Futures and Tasks
377382
.. versionchanged:: 3.11
378383
Added the *context* parameter.
379384

385+
.. versionchanged:: 3.13.3
386+
Added ``kwargs`` which passes on arbitrary extra parameters, including ``name`` and ``context``.
387+
388+
.. versionchanged:: 3.13.4
389+
Rolled back the change that passes on *name* and *context* (if it is None),
390+
while still passing on other arbitrary keyword arguments (to avoid breaking backwards compatibility with 3.13.3).
391+
380392
.. method:: loop.set_task_factory(factory)
381393

382394
Set a task factory that will be used by
@@ -388,6 +400,13 @@ Creating Futures and Tasks
388400
event loop, and *coro* is a coroutine object. The callable
389401
must pass on all *kwargs*, and return a :class:`asyncio.Task`-compatible object.
390402

403+
.. versionchanged:: 3.13.3
404+
Required that all *kwargs* are passed on to :class:`asyncio.Task`.
405+
406+
.. versionchanged:: 3.13.4
407+
*name* is no longer passed to task factories. *context* is no longer passed
408+
to task factories if it is ``None``.
409+
391410
.. method:: loop.get_task_factory()
392411

393412
Return a task factory or ``None`` if the default one is in use.

Lib/asyncio/base_events.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -458,18 +458,24 @@ def create_future(self):
458458
"""Create a Future object attached to the loop."""
459459
return futures.Future(loop=self)
460460

461-
def create_task(self, coro, **kwargs):
461+
def create_task(self, coro, *, name=None, context=None, **kwargs):
462462
"""Schedule a coroutine object.
463463
464464
Return a task object.
465465
"""
466466
self._check_closed()
467467
if self._task_factory is not None:
468-
return self._task_factory(self, coro, **kwargs)
468+
if context is not None:
469+
kwargs["context"] = context
470+
471+
task = self._task_factory(self, coro, **kwargs)
472+
task.set_name(name)
473+
474+
else:
475+
task = tasks.Task(coro, loop=self, name=name, context=context, **kwargs)
476+
if task._source_traceback:
477+
del task._source_traceback[-1]
469478

470-
task = tasks.Task(coro, loop=self, **kwargs)
471-
if task._source_traceback:
472-
del task._source_traceback[-1]
473479
try:
474480
return task
475481
finally:

Lib/test/test_asyncio/test_taskgroups.py

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1081,18 +1081,6 @@ async def throw_error():
10811081
# cancellation happens here and error is more understandable
10821082
await asyncio.sleep(0)
10831083

1084-
async def test_name(self):
1085-
name = None
1086-
1087-
async def asyncfn():
1088-
nonlocal name
1089-
name = asyncio.current_task().get_name()
1090-
1091-
async with asyncio.TaskGroup() as tg:
1092-
tg.create_task(asyncfn(), name="example name")
1093-
1094-
self.assertEqual(name, "example name")
1095-
10961084

10971085
class TestTaskGroup(BaseTestTaskGroup, unittest.IsolatedAsyncioTestCase):
10981086
loop_factory = asyncio.EventLoop
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
In 3.13.3 we accidentally changed the signature of the asyncio ``create_task()`` family of methods and how it calls a custom task factory in a backwards incompatible way. Since some 3rd party libraries have already made changes to work around the issue that might break if we simply reverted the changes, we're instead changing things to be backwards compatible with 3.13.2 while still supporting those workarounds for 3.13.3. In particular, the special-casing of ``name`` and ``context`` is back (until 3.14) and consequently eager tasks may still find that their name hasn't been set before they execute their first yielding await.

0 commit comments

Comments
 (0)