0

I am trying to handle the abrupt connection drops on the server side

I have a flutter client , and when I do websocket.close() on the client side , everything works as intended because on the server side I call disconnect() method accordingly to handle the disconnects , but when there is an abrupt disconnect from the client side like when the app crashes or app is killed , server starts throwing exceptions. how can I handle such cases on the server side ?

here is what my code looks like for handling the connections and sending messages to the clients :

class Loggedin_LobbyManager:
    def __init__(self):
        self.Loggedin_active_connections:   List[WebSocket] = []
        self.Loggedin_disconnected_clients: List[WebSocket] = []
        self.Loggedin_lobbies:              dict[str, List[WebSocket]] = {}

    async def connect(self, websocket: WebSocket, Loggedin_lobby_id: str, token: str):
        try:
            decoded = jwt.decode(token.replace("Bearer ", ""), options={"verify_signature": False})
        except jwt.exceptions.InvalidSignatureError:
            return

        user_id = decoded.get("user_id")
        if not user_id or not db.Users.count_documents({"username": user_id}):
            return

        if websocket in self.Loggedin_disconnected_clients:
            self.Loggedin_disconnected_clients.remove(websocket)
        else:
            await websocket.accept()

        self.Loggedin_active_connections.append(websocket)

        if Loggedin_lobby_id not in self.Loggedin_lobbies:
            self.Loggedin_lobbies[Loggedin_lobby_id] = []
        self.Loggedin_lobbies[Loggedin_lobby_id].append(websocket)

    def disconnect(self, websocket: WebSocket):
        print("***************************called")
        self.Loggedin_active_connections.remove(websocket)
        self.Loggedin_disconnected_clients.append(websocket)
        for Loggedin_lobby_id, clients in self.Loggedin_lobbies.items():
            if websocket in clients:
                clients.remove(websocket)

    async def send_personal_message(self, message: str, websocket: WebSocket):
        if websocket in self.Loggedin_active_connections:
            await websocket.send_json(message)

    async def broadcast(self, message: str):
        for connection in self.Loggedin_active_connections:
            await connection.send_json(message) 
                    
    async def broadcast_to_lobby(self, lobby_id: str, message: str):
        clients = self.Loggedin_lobbies.get(lobby_id)
        if not clients:
            return
        for client in clients:
            try:
                await client.send_json(message)
            except RuntimeError as e:
                if "Connection is closed" in str(e):
                    continue
                else:
                    raise e

The issue is mostly related to the broadcast_to_lobby() method , when a client disconnects abruptly , server will still try and broadcast the message to that client as well , which understandably will throw errors and exceptions

Arad
  • 1
  • 3

0 Answers0