diff --git a/src/agents/voice/result.py b/src/agents/voice/result.py index 2f4b24433b..8dd27ce1c6 100644 --- a/src/agents/voice/result.py +++ b/src/agents/voice/result.py @@ -194,8 +194,10 @@ async def _stream_audio( ) logger.error("Error streaming audio: %s", e) - # Signal completion for whole session because of error - await local_queue.put(VoiceStreamEventLifecycle(event="session_ended")) + await self._add_error(e) + await local_queue.put(None) + if self.text_generation_task is not None and not self.text_generation_task.done(): + self.text_generation_task.cancel() raise e async def _add_text(self, text: str): @@ -292,8 +294,11 @@ def _cleanup_tasks(self): self.text_generation_task.cancel() def _check_errors(self): - for task in self._tasks: - if task.done(): + all_tasks = list(self._tasks) + if self.text_generation_task is not None: + all_tasks.append(self.text_generation_task) + for task in all_tasks: + if task.done() and not task.cancelled(): if task.exception(): self._stored_exception = task.exception() break