Add eager start to create_task helper
authorMarcel van der Veldt <m.vanderveldt@outlook.com>
Wed, 18 Feb 2026 20:47:32 +0000 (21:47 +0100)
committerMarcel van der Veldt <m.vanderveldt@outlook.com>
Wed, 18 Feb 2026 20:47:32 +0000 (21:47 +0100)
music_assistant/helpers/util.py
music_assistant/mass.py

index 3bd9c3e24aa54f3c56997e859e1f909f35fac61a..ea5b7423925564bd86323600d194c5a70d07d5f9 100644 (file)
@@ -972,6 +972,7 @@ def guard_single_request[ProviderT: "Provider | CoreController", **P, R](
             *args,
             task_id=task_id,
             abort_existing=False,
+            eager_start=True,
             **kwargs,
         )
         return await task
index 1df492eaa3c174f5ca1ba5e95f93a6c8b4efab60..1d1c5a1ec5d05c78ad4426c99577e659f1b219c2 100644 (file)
@@ -458,11 +458,21 @@ class MusicAssistant:
         *args: Any,
         task_id: str | None = None,
         abort_existing: bool = False,
+        eager_start: bool = True,
         **kwargs: Any,
     ) -> asyncio.Task[_R]:
         """Create Task on (main) event loop from Coroutine(function).
 
         Tasks created by this helper will be properly cancelled on stop.
+
+        :param target: Coroutine function or awaitable to run as a task.
+        :param args: Arguments to pass to the coroutine function.
+        :param task_id: Optional ID to track and deduplicate tasks.
+        :param abort_existing: If True, cancel existing task with same task_id.
+        :param eager_start: If True (default), start task immediately without waiting
+                           for next event loop iteration. This ensures proper ordering
+                           when creating multiple tasks in sequence.
+        :param kwargs: Keyword arguments to pass to the coroutine function.
         """
         if task_id and (existing := self._tracked_tasks.get(task_id)) and not existing.done():
             # prevent duplicate tasks if task_id is given and already present
@@ -474,15 +484,18 @@ class MusicAssistant:
 
         if inspect.iscoroutinefunction(target):
             # coroutine function
-            task = self.loop.create_task(target(*args, **kwargs))
+            coro = target(*args, **kwargs)
         elif inspect.iscoroutine(target):
             # coroutine
-            task = self.loop.create_task(target)
+            coro = target
         elif callable(target):
             raise RuntimeError("Function is not a coroutine or coroutine function")
         else:
             raise RuntimeError("Target is missing")
 
+        # Use asyncio.Task directly with eager_start for immediate execution
+        task: asyncio.Task[_R] = asyncio.Task(coro, loop=self.loop, eager_start=eager_start)
+
         if task_id is None:
             task_id = uuid4().hex