Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: handle socket connection closed error in _signal_exit #355

Merged
merged 2 commits into from
Apr 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading