From 9d7f800ab36bd996ab1adf5c52d559df86ab118f Mon Sep 17 00:00:00 2001 From: David vonThenen <12752197+dvonthenen@users.noreply.github.com> Date: Mon, 22 Apr 2024 10:37:27 -0700 Subject: [PATCH] Don't Set Thread Pointers to None --- deepgram/clients/live/v1/async_client.py | 21 ++++++++++++++++++--- deepgram/clients/live/v1/client.py | 23 ++++++++++++++++++++--- 2 files changed, 38 insertions(+), 6 deletions(-) diff --git a/deepgram/clients/live/v1/async_client.py b/deepgram/clients/live/v1/async_client.py index b518f927..4117676e 100644 --- a/deepgram/clients/live/v1/async_client.py +++ b/deepgram/clients/live/v1/async_client.py @@ -283,6 +283,11 @@ async def _listening(self) -> None: return except websockets.exceptions.WebSocketException as e: + if e.code == 1000: + self.logger.notice(f"_listening({e.code}) exiting gracefully") + self.logger.debug("AsyncLiveClient._listening LEAVE") + return + self.logger.error( "WebSocketException in AsyncLiveClient._listening: %s", e ) @@ -357,6 +362,11 @@ async def _keep_alive(self) -> None: return except websockets.exceptions.WebSocketException as e: + if e.code == 1000: + self.logger.notice(f"_keep_alive({e.code}) exiting gracefully") + self.logger.debug("AsyncLiveClient._keep_alive LEAVE") + return + self.logger.error( "WebSocketException in AsyncLiveClient._keep_alive: %s", e ) @@ -419,11 +429,18 @@ async def send(self, data: Union[str, bytes]) -> bool: await self._socket.send(data) except websockets.exceptions.ConnectionClosedOK as e: self.logger.notice(f"send() exiting gracefully: {e.code}") - self.logger.debug("AsyncLiveClient._keep_alive LEAVE") + self.logger.debug("AsyncLiveClient.send LEAVE") if self.config.options.get("termination_exception_send") == "true": raise return True except websockets.exceptions.WebSocketException as e: + if e.code == 1000: + self.logger.notice(f"send({e.code}) exiting gracefully") + self.logger.debug("AsyncLiveClient.send LEAVE") + if self.config.options.get("termination_exception_send") == "true": + raise + return True + self.logger.error("send() failed - WebSocketException: %s", str(e)) self.logger.spam("AsyncLiveClient.send LEAVE") if self.config.options.get("termination_exception_send") == "true": @@ -469,8 +486,6 @@ async def finish(self) -> bool: # Use asyncio.gather to wait for tasks to be cancelled await asyncio.gather(*filter(None, tasks), return_exceptions=True) self.logger.notice("threads joined") - self._listen_thread = None - self._keep_alive_thread = None self._socket = None diff --git a/deepgram/clients/live/v1/client.py b/deepgram/clients/live/v1/client.py index f73f0ccd..b76e722c 100644 --- a/deepgram/clients/live/v1/client.py +++ b/deepgram/clients/live/v1/client.py @@ -129,7 +129,7 @@ def start( # keepalive thread if self.config.options.get("keepalive") == "true": - self.logger.notice("keepalive is disabled") + self.logger.notice("keepalive is enabled") self._keep_alive_thread = threading.Thread(target=self._keep_alive) self._keep_alive_thread.start() else: @@ -283,6 +283,11 @@ def _listening(self) -> None: return except websockets.exceptions.WebSocketException as e: + if e.code == 1000: + self.logger.notice(f"_listening({e.code}) exiting gracefully") + self.logger.debug("LiveClient._listening LEAVE") + return + self.logger.error( "WebSocketException in AsyncLiveClient._listening: %s", e ) @@ -357,6 +362,11 @@ def _keep_alive(self) -> None: return except websockets.exceptions.WebSocketException as e: + if e.code == 1000: + self.logger.notice(f"_keep_alive({e.code}) exiting gracefully") + self.logger.debug("LiveClient._keep_alive LEAVE") + return + self.logger.error( "WebSocketException in AsyncLiveClient._keep_alive: %s", e ) @@ -423,6 +433,15 @@ def send(self, data: Union[str, bytes]) -> bool: raise return True except websockets.exceptions.WebSocketException as e: + if e.code == 1000: + self.logger.notice(f"send({e.code}) exiting gracefully") + self.logger.debug("LiveClient.send LEAVE") + if ( + self.config.options.get("termination_exception_send") + == "true" + ): + raise + return True self.logger.error("send() failed - WebSocketException: %s", str(e)) self.logger.spam("LiveClient.send LEAVE") if self.config.options.get("termination_exception_send") == "true": @@ -457,12 +476,10 @@ def finish(self) -> bool: self.logger.verbose("cancelling tasks...") if self._keep_alive_thread is not None: self._keep_alive_thread.join() - self._keep_alive_thread = None self.logger.notice("processing thread joined") if self._listen_thread is not None: self._listen_thread.join() - self._listen_thread = None self.logger.notice("listening thread joined") self._socket = None