Skip to content

Commit

Permalink
Merge pull request #355 from roerohan/fix-signal-exit-exception
Browse files Browse the repository at this point in the history
fix: handle socket connection closed error in _signal_exit
  • Loading branch information
davidvonthenen authored Apr 5, 2024
2 parents d2c4334 + 4f78e8e commit 5dee108
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
10 changes: 9 additions & 1 deletion deepgram/clients/live/v1/async_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
10 changes: 9 additions & 1 deletion deepgram/clients/live/v1/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down

0 comments on commit 5dee108

Please sign in to comment.