From 2ca6dbdab7571a58d61fd1ff96fb2589103feb17 Mon Sep 17 00:00:00 2001 From: Rohan Mukherjee Date: Fri, 5 Apr 2024 16:08:51 +0530 Subject: [PATCH 1/2] fix: handle socket connection closed error in _signal_exit --- deepgram/clients/live/v1/async_client.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/deepgram/clients/live/v1/async_client.py b/deepgram/clients/live/v1/async_client.py index 4edce0ae..f677035a 100644 --- a/deepgram/clients/live/v1/async_client.py +++ b/deepgram/clients/live/v1/async_client.py @@ -485,7 +485,11 @@ 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 Exception as e: + self.logger.error("Exception in AsyncLiveClient._signal_exit, %s", e) await asyncio.sleep(0.5) From 4f78e8ed1d30a352f5c647d6644606e8f5d9ed7a Mon Sep 17 00:00:00 2001 From: Rohan Mukherjee Date: Fri, 5 Apr 2024 22:25:05 +0530 Subject: [PATCH 2/2] fix: update error handling in async_client.py and client.py --- deepgram/clients/live/v1/async_client.py | 6 +++++- deepgram/clients/live/v1/client.py | 10 +++++++++- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/deepgram/clients/live/v1/async_client.py b/deepgram/clients/live/v1/async_client.py index f677035a..6efaa822 100644 --- a/deepgram/clients/live/v1/async_client.py +++ b/deepgram/clients/live/v1/async_client.py @@ -488,8 +488,12 @@ async def _signal_exit(self) -> None: 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("Exception in AsyncLiveClient._signal_exit, %s", 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)