Merge pull request #2493 from pipecat-ai/aleix/runner-task-asyncio-cancellation

PipelineRunner/PipelineTask: fix asyncio task cancellation
This commit is contained in:
Aleix Conchillo Flaqué
2025-08-22 09:13:58 -07:00
committed by GitHub
5 changed files with 30 additions and 13 deletions

View File

@@ -71,7 +71,10 @@ class PipelineRunner(BaseObject):
logger.debug(f"Runner {self} started running {task}")
self._tasks[task.name] = task
params = PipelineTaskParams(loop=self._loop)
await task.run(params)
try:
await task.run(params)
except asyncio.CancelledError:
await self._cancel()
del self._tasks[task.name]
# Cleanup base object.
@@ -95,6 +98,10 @@ class PipelineRunner(BaseObject):
async def cancel(self):
"""Cancel all running tasks immediately."""
logger.debug(f"Cancelling runner {self}")
await self._cancel()
async def _cancel(self):
"""Cancel all running tasks immediately."""
await asyncio.gather(*[t.cancel() for t in self._tasks.values()])
def _setup_sigint(self):

View File

@@ -370,11 +370,9 @@ class PipelineTask(BasePipelineTask):
# We have already cleaned up the pipeline inside the task.
cleanup_pipeline = False
except asyncio.CancelledError:
# We are awaiting on the push task and it might be cancelled
# (e.g. Ctrl-C). This means we will get a CancelledError here as
# well, because you get a CancelledError in every place you are
# awaiting a task.
pass
# Raise exception back to the pipeline runner so it can cancel this
# task properly.
raise
finally:
await self._cancel_tasks()
await self._cleanup(cleanup_pipeline)