From 117d5acbf5ef376a9d56917d2783ad6e9c74e35c Mon Sep 17 00:00:00 2001 From: Gabriel Luiz Freitas Almeida Date: Thu, 28 Sep 2023 17:17:59 -0300 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20fix(manager.py):=20change=20erro?= =?UTF-8?q?r=20message=20from=20"Could=20not=20find=20a=20LangChain=20obje?= =?UTF-8?q?ct=20for=20client=5Fid"=20to=20"Could=20not=20find=20a=20build?= =?UTF-8?q?=20result=20for=20client=5Fid"=20for=20better=20clarity=20?= =?UTF-8?q?=F0=9F=90=9B=20fix(manager.py):=20handle=20WebSocketState.DISCO?= =?UTF-8?q?NNECTED=20state=20in=20the=20finally=20block=20to=20properly=20?= =?UTF-8?q?disconnect=20the=20client=20if=20the=20connection=20is=20alread?= =?UTF-8?q?y=20closed?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/backend/langflow/services/chat/manager.py | 27 ++++++++++++------- 1 file changed, 17 insertions(+), 10 deletions(-) diff --git a/src/backend/langflow/services/chat/manager.py b/src/backend/langflow/services/chat/manager.py index b1f030f83..2a1bb5694 100644 --- a/src/backend/langflow/services/chat/manager.py +++ b/src/backend/langflow/services/chat/manager.py @@ -1,6 +1,7 @@ from collections import defaultdict import uuid from fastapi import WebSocket, status +from starlette.websockets import WebSocketState from langflow.api.v1.schemas import ChatMessage, ChatResponse, FileResponse from langflow.interface.utils import pil_to_base64 from langflow.services.base import Service @@ -218,23 +219,29 @@ class ChatService(Service): else: raise RuntimeError( - f"Could not find a LangChain object for client_id {client_id}" + f"Could not find a build result for client_id {client_id}" ) except Exception as exc: # Handle any exceptions that might occur logger.exception(f"Error handling websocket: {exc}") - await self.close_connection( - client_id=client_id, - code=status.WS_1011_INTERNAL_ERROR, - reason=str(exc)[:120], - ) - finally: - try: + if websocket.client_state == WebSocketState.CONNECTED: await self.close_connection( client_id=client_id, - code=status.WS_1000_NORMAL_CLOSURE, - reason="Client disconnected", + code=status.WS_1011_INTERNAL_ERROR, + reason=str(exc)[:120], ) + elif websocket.client_state == WebSocketState.DISCONNECTED: + self.disconnect(client_id) + + finally: + try: + # first check if the connection is still open + if websocket.client_state == WebSocketState.CONNECTED: + await self.close_connection( + client_id=client_id, + code=status.WS_1000_NORMAL_CLOSURE, + reason="Client disconnected", + ) except Exception as exc: logger.error(f"Error closing connection: {exc}") self.disconnect(client_id)