Skip to content

Commit f6ed7d7

Browse files
authored
Merge pull request pipecat-ai#3418 from pipecat-ai/mb/speechmatics-task-cleanup
2 parents 2296caf + cd3290d commit f6ed7d7

1 file changed

Lines changed: 11 additions & 3 deletions

File tree

  • src/pipecat/services/speechmatics

src/pipecat/services/speechmatics/stt.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -369,17 +369,14 @@ async def start(self, frame: StartFrame):
369369
"""Called when the new session starts."""
370370
await super().start(frame)
371371
await self._connect()
372-
self._stt_msg_task = self.create_task(self._process_stt_messages())
373372

374373
async def stop(self, frame: EndFrame):
375374
"""Called when the session ends."""
376-
await self.cancel_task(self._stt_msg_task)
377375
await super().stop(frame)
378376
await self._disconnect()
379377

380378
async def cancel(self, frame: CancelFrame):
381379
"""Called when the session is cancelled."""
382-
await self.cancel_task(self._stt_msg_task)
383380
await super().cancel(frame)
384381
await self._disconnect()
385382

@@ -389,6 +386,7 @@ async def _connect(self) -> None:
389386
- Create STT client
390387
- Register handlers for messages
391388
- Connect to the client
389+
- Start message processing task
392390
"""
393391
# Log the event
394392
logger.debug(f"{self} connecting to Speechmatics STT service")
@@ -436,12 +434,22 @@ def add_message(message: dict[str, Any]):
436434
self._client = None
437435
await self.push_error(error_msg=f"Error connecting to STT service: {e}", exception=e)
438436

437+
# Start message processing task
438+
if not self._stt_msg_task:
439+
self._stt_msg_task = self.create_task(self._process_stt_messages())
440+
439441
async def _disconnect(self) -> None:
440442
"""Disconnect from the STT service.
441443
444+
- Cancel message processing task
442445
- Disconnect the client
443446
- Emit on_disconnected event handler for clients
444447
"""
448+
# Cancel the message processing task
449+
if self._stt_msg_task:
450+
await self.cancel_task(self._stt_msg_task)
451+
self._stt_msg_task = None
452+
445453
# Disconnect the client
446454
logger.debug(f"{self} disconnecting from Speechmatics STT service")
447455
try:

0 commit comments

Comments
 (0)