diff --git a/deepgram/clients/live/v1/async_client.py b/deepgram/clients/live/v1/async_client.py index 4edce0ae..6efaa822 100644 --- a/deepgram/clients/live/v1/async_client.py +++ b/deepgram/clients/live/v1/async_client.py @@ -485,7 +485,15 @@ async def _signal_exit(self) -> None: self.logger.verbose("closing socket...") if self._socket is not None: self.logger.verbose("send CloseStream...") - await self._socket.send(json.dumps({"type": "CloseStream"})) + try: + # if the socket connection is closed, the following line might throw an error + await self._socket.send(json.dumps({"type": "CloseStream"})) + except websockets.exceptions.ConnectionClosedOK as e: + self.logger.notice(f"_signal_exit - connection closed: {e.code}") + except websockets.exceptions.WebSocketException as e: + self.logger.error(f"_signal_exit - WebSocketException: {str(e)}") + except Exception as e: + self.logger.error(f"_signal_exit - Exception: {str(e)}") await asyncio.sleep(0.5) diff --git a/deepgram/clients/live/v1/client.py b/deepgram/clients/live/v1/client.py index f60ef21e..70c5f2f1 100644 --- a/deepgram/clients/live/v1/client.py +++ b/deepgram/clients/live/v1/client.py @@ -473,7 +473,15 @@ def _signal_exit(self) -> None: self.logger.notice("closing socket...") if self._socket is not None: self.logger.notice("sending CloseStream...") - self.send(json.dumps({"type": "CloseStream"})) + try: + # if the socket connection is closed, the following line might throw an error + self._socket.send(json.dumps({"type": "CloseStream"})) + except websockets.exceptions.ConnectionClosedOK as e: + self.logger.notice(f"_signal_exit - connection closed: {e.code}") + except websockets.exceptions.WebSocketException as e: + self.logger.error(f"_signal_exit - WebSocketException: {str(e)}") + except Exception as e: + self.logger.error(f"_signal_exit - Exception: {str(e)}") time.sleep(0.5)