Yield after create_task to ensure timer tasks are scheduled
Add `await asyncio.sleep(0)` after `create_task()` calls in UserIdleController, SpeechTimeoutUserTurnStopStrategy, TurnAnalyzerUserTurnStopStrategy, and UserTurnCompletionLLMServiceMixin so the event loop schedules the newly created timer tasks before the caller continues.
This commit is contained in:
@@ -143,6 +143,8 @@ class UserIdleController(BaseObject):
|
||||
self._idle_timer_expired(),
|
||||
f"{self}::idle_timer",
|
||||
)
|
||||
# Make sure the task is scheduled.
|
||||
await asyncio.sleep(0)
|
||||
|
||||
async def _cancel_idle_timer(self):
|
||||
"""Cancel the idle timer if running."""
|
||||
|
||||
@@ -153,6 +153,8 @@ class SpeechTimeoutUserTurnStopStrategy(BaseUserTurnStopStrategy):
|
||||
self._timeout_task = self.task_manager.create_task(
|
||||
self._timeout_handler(timeout), f"{self}::_timeout_handler"
|
||||
)
|
||||
# Make sure the task is scheduled.
|
||||
await asyncio.sleep(0)
|
||||
|
||||
async def _handle_transcription(self, frame: TranscriptionFrame):
|
||||
"""Handle user transcription."""
|
||||
@@ -174,6 +176,8 @@ class SpeechTimeoutUserTurnStopStrategy(BaseUserTurnStopStrategy):
|
||||
self._timeout_task = self.task_manager.create_task(
|
||||
self._timeout_handler(timeout), f"{self}::_timeout_handler"
|
||||
)
|
||||
# Make sure the task is scheduled.
|
||||
await asyncio.sleep(0)
|
||||
|
||||
def _calculate_timeout(self) -> float:
|
||||
"""Calculate the timeout value based on current state.
|
||||
|
||||
@@ -193,6 +193,8 @@ class TurnAnalyzerUserTurnStopStrategy(BaseUserTurnStopStrategy):
|
||||
self._timeout_task = self.task_manager.create_task(
|
||||
self._timeout_handler(timeout), f"{self}::_timeout_handler"
|
||||
)
|
||||
# Make sure the task is scheduled.
|
||||
await asyncio.sleep(0)
|
||||
|
||||
async def _handle_transcription(self, frame: TranscriptionFrame):
|
||||
"""Handle user transcription."""
|
||||
@@ -217,6 +219,8 @@ class TurnAnalyzerUserTurnStopStrategy(BaseUserTurnStopStrategy):
|
||||
self._timeout_task = self.task_manager.create_task(
|
||||
self._timeout_handler(timeout), f"{self}::_timeout_handler"
|
||||
)
|
||||
# Make sure the task is scheduled.
|
||||
await asyncio.sleep(0)
|
||||
|
||||
async def _handle_prediction_result(self, result: Optional[MetricsData]):
|
||||
"""Handle a prediction result event from the turn analyzer."""
|
||||
|
||||
@@ -254,6 +254,8 @@ class UserTurnCompletionLLMServiceMixin:
|
||||
self._incomplete_timeout_handler(incomplete_type, timeout),
|
||||
f"_incomplete_timeout_{incomplete_type}",
|
||||
)
|
||||
# Make sure the task is scheduled.
|
||||
await asyncio.sleep(0)
|
||||
|
||||
async def _cancel_incomplete_timeout(self):
|
||||
"""Cancel any pending incomplete timeout task."""
|
||||
|
||||
Reference in New Issue
Block a user