From 42502a4f3b8417e7164606804680c86d1ad90280 Mon Sep 17 00:00:00 2001 From: Mark Backman Date: Sun, 10 Aug 2025 19:35:05 -0400 Subject: [PATCH] fix: WebsocketService retry logic incorrectly handling ConnectionClosedOK exception --- CHANGELOG.md | 7 ++++++- src/pipecat/services/websocket_service.py | 11 +++++------ 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 46a968fde..48881a0f7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,13 +5,18 @@ All notable changes to **Pipecat** will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). -## Unreleased +## [Unreleased] ### Changed - Updated `pyproject.toml` to once again pin `numba` to `>=0.61.2` in order to resolve package versioning issues. +### Fixed + +- Fixed an issue where retrying a websocket connection error would result in an + error. + ### Other - Updated `15-switch-voices.py` and `15a-switch-languages.py` examples to show diff --git a/src/pipecat/services/websocket_service.py b/src/pipecat/services/websocket_service.py index ff4f67dd3..17a911366 100644 --- a/src/pipecat/services/websocket_service.py +++ b/src/pipecat/services/websocket_service.py @@ -12,6 +12,7 @@ from typing import Awaitable, Callable, Optional import websockets from loguru import logger +from websockets.exceptions import ConnectionClosedOK from websockets.protocol import State from pipecat.frames.frames import ErrorFrame @@ -82,12 +83,10 @@ class WebsocketService(ABC): try: await self._receive_messages() retry_count = 0 # Reset counter on successful message receive - if self._websocket and self._websocket.state is State.CLOSED: - raise websockets.ConnectionClosedOK( - self._websocket.close_rcvd, - self._websocket.close_sent, - self._websocket.close_rcvd_then_sent, - ) + except ConnectionClosedOK as e: + # Normal closure, don't retry + logger.debug(f"{self} connection closed normally: {e}") + break except Exception as e: message = f"{self} error receiving messages: {e}" logger.error(message)