Skip to content

Commit

Permalink
Merge pull request #480 from dvonthenen/close-socket-on-server-termin…
Browse files Browse the repository at this point in the history
…ation

Trigger WSClient Disconnect When WS Connection Closed
  • Loading branch information
davidvonthenen authored Nov 4, 2024
2 parents a840467 + 25fa3bd commit aff6110
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
8 changes: 7 additions & 1 deletion deepgram/clients/common/v1/abstract_async_websocket.py
Original file line number Diff line number Diff line change
Expand Up @@ -254,12 +254,18 @@ async def _listening(self) -> None:
self._logger.debug("AbstractAsyncWebSocketClient._listening LEAVE")

except websockets.exceptions.ConnectionClosedOK as e:
# signal exit and close
await self._signal_exit()

self._logger.notice(f"_listening({e.code}) exiting gracefully")
self._logger.debug("AbstractAsyncWebSocketClient._listening LEAVE")
return

except websockets.exceptions.ConnectionClosed as e:
if e.code in [1000, 1001]:
# signal exit and close
await self._signal_exit()

self._logger.notice(f"_listening({e.code}) exiting gracefully")
self._logger.debug("AbstractAsyncWebSocketClient._listening LEAVE")
return
Expand Down Expand Up @@ -466,7 +472,7 @@ async def finish(self) -> bool:
except asyncio.CancelledError as e:
self._logger.error("tasks cancelled error: %s", e)
self._logger.debug("AbstractAsyncWebSocketClient.finish LEAVE")
return False
return True

async def _signal_exit(self) -> None:
# send close event
Expand Down
6 changes: 6 additions & 0 deletions deepgram/clients/common/v1/abstract_sync_websocket.py
Original file line number Diff line number Diff line change
Expand Up @@ -256,12 +256,18 @@ def _listening(
self._logger.debug("AbstractSyncWebSocketClient._listening LEAVE")

except websockets.exceptions.ConnectionClosedOK as e:
# signal exit and close
self._signal_exit()

self._logger.notice(f"_listening({e.code}) exiting gracefully")
self._logger.debug("AbstractSyncWebSocketClient._listening LEAVE")
return

except websockets.exceptions.ConnectionClosed as e:
if e.code in [1000, 1001]:
# signal exit and close
self._signal_exit()

self._logger.notice(f"_listening({e.code}) exiting gracefully")
self._logger.debug("AbstractSyncWebSocketClient._listening LEAVE")
return
Expand Down

0 comments on commit aff6110

Please sign in to comment.