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: catch more shutdown issues to resolve shutdown without dropping #65

Merged
merged 1 commit into from
Apr 10, 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
5 changes: 4 additions & 1 deletion silverback/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,10 @@ async def run(self):
if len(tasks) == 0:
raise Halt("No tasks to execute")

await asyncio.gather(*tasks)
try:
await asyncio.gather(*tasks)
except Exception as e:
logger.error(f"Fatal error detected, shutting down: '{e}'")

# Execute Silverback shutdown task before shutting down the broker
if shutdown_handler := self.app.get_shutdown_handler():
Expand Down
9 changes: 8 additions & 1 deletion silverback/subscriptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -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)
Loading