Skip to content

Commit 5dee108

Browse files
Merge pull request #355 from roerohan/fix-signal-exit-exception
fix: handle socket connection closed error in _signal_exit
2 parents d2c4334 + 4f78e8e commit 5dee108

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

deepgram/clients/live/v1/async_client.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -485,7 +485,15 @@ async def _signal_exit(self) -> None:
485485
self.logger.verbose("closing socket...")
486486
if self._socket is not None:
487487
self.logger.verbose("send CloseStream...")
488-
await self._socket.send(json.dumps({"type": "CloseStream"}))
488+
try:
489+
# if the socket connection is closed, the following line might throw an error
490+
await self._socket.send(json.dumps({"type": "CloseStream"}))
491+
except websockets.exceptions.ConnectionClosedOK as e:
492+
self.logger.notice(f"_signal_exit - connection closed: {e.code}")
493+
except websockets.exceptions.WebSocketException as e:
494+
self.logger.error(f"_signal_exit - WebSocketException: {str(e)}")
495+
except Exception as e:
496+
self.logger.error(f"_signal_exit - Exception: {str(e)}")
489497

490498
await asyncio.sleep(0.5)
491499

deepgram/clients/live/v1/client.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -473,7 +473,15 @@ def _signal_exit(self) -> None:
473473
self.logger.notice("closing socket...")
474474
if self._socket is not None:
475475
self.logger.notice("sending CloseStream...")
476-
self.send(json.dumps({"type": "CloseStream"}))
476+
try:
477+
# if the socket connection is closed, the following line might throw an error
478+
self._socket.send(json.dumps({"type": "CloseStream"}))
479+
except websockets.exceptions.ConnectionClosedOK as e:
480+
self.logger.notice(f"_signal_exit - connection closed: {e.code}")
481+
except websockets.exceptions.WebSocketException as e:
482+
self.logger.error(f"_signal_exit - WebSocketException: {str(e)}")
483+
except Exception as e:
484+
self.logger.error(f"_signal_exit - Exception: {str(e)}")
477485

478486
time.sleep(0.5)
479487

0 commit comments

Comments
 (0)