🐛 fix(manager.py): catch and log RuntimeError when closing websocket connection to prevent unexpected ASGI message error

🔒 chore(manager.py): improve error handling when closing websocket connection to prevent unexpected ASGI message error
This commit is contained in:
Gabriel Luiz Freitas Almeida 2023-07-09 10:31:46 -03:00
commit 99be9830ad

View file

@ -104,8 +104,14 @@ class ChatManager:
async def close_connection(self, client_id: str, code: int, reason: str):
if websocket := self.active_connections[client_id]:
await websocket.close(code=code, reason=reason)
self.disconnect(client_id)
try:
await websocket.close(code=code, reason=reason)
self.disconnect(client_id)
except RuntimeError as exc:
# This is to catch the following error:
# Unexpected ASGI message 'websocket.close', after sending 'websocket.close'
if "after sending" in str(exc):
logger.error(exc)
async def process_message(
self, client_id: str, payload: Dict, langchain_object: Any