From aeec47561d5541c3c9065c351b43fe02a8a57d1d Mon Sep 17 00:00:00 2001 From: fubuloubu <3859395+fubuloubu@users.noreply.github.com> Date: Mon, 8 Apr 2024 17:19:26 -0400 Subject: [PATCH] fix: catch more shutdown issues to resolve shutdown without dropping --- silverback/subscriptions.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/silverback/subscriptions.py b/silverback/subscriptions.py index cee56dce..057cfa38 100644 --- a/silverback/subscriptions.py +++ b/silverback/subscriptions.py @@ -4,6 +4,7 @@ from typing import AsyncGenerator, Dict, List, Optional from ape.logging import logger +from websockets import ConnectionClosedError from websockets import client as ws_client @@ -145,6 +146,12 @@ async def __aexit__(self, exc_type, exc, tb): # Try to gracefully unsubscribe to all events await asyncio.gather(*(self.unsubscribe(sub_id) for sub_id in self._subscriptions)) + except ConnectionClosedError: + pass # Websocket already closed (ctrl+C and patiently waiting) + finally: # Disconnect and release websocket - await self.connection.close() + try: + await self.connection.close() + except RuntimeError: + pass # No running event loop to disconnect from (multiple ctrl+C presses)