fix(chat_manager.py): handle case where client is not in active_connections dictionary and remove duplicate code in finally block

This commit is contained in:
Gabriel Almeida 2023-05-10 12:17:52 -03:00
commit 7fde19b36a

View file

@ -4,6 +4,7 @@ from collections import defaultdict
from typing import Dict, List from typing import Dict, List
from fastapi import WebSocket, status from fastapi import WebSocket, status
from fastapi.websockets import WebSocketState
from langflow.api.schemas import ChatMessage, ChatResponse, FileResponse from langflow.api.schemas import ChatMessage, ChatResponse, FileResponse
from langflow.cache import cache_manager from langflow.cache import cache_manager
@ -179,11 +180,13 @@ class ChatManager:
await self.active_connections[client_id].close( await self.active_connections[client_id].close(
code=status.WS_1011_INTERNAL_ERROR, reason=str(e) code=status.WS_1011_INTERNAL_ERROR, reason=str(e)
) )
self.disconnect(client_id)
finally: finally:
try: try:
await self.active_connections[client_id].close( connection = self.active_connections.get(client_id)
code=1000, reason="Client disconnected" if connection:
) await connection.close(code=1000, reason="Client disconnected")
self.disconnect(client_id)
except Exception as e: except Exception as e:
logger.exception(e) logger.exception(e)
self.disconnect(client_id) self.disconnect(client_id)