python-websocket-server icon indicating copy to clipboard operation
python-websocket-server copied to clipboard

_terminate_client_handlers does not close all clients

Open vzarutskiy opened this issue 2 years ago • 0 comments

_terminate_client_handlers uses "for" cycle for clients disconnecting:

for client in self.clients:
            self._terminate_client_handler(client["handler"])

It executes client_left as finish(), there client is removed form self.clients list. It leads to an index shift and second client in list will be not disconnected. For example:

 def stop_server(self):
        print("before disconnect")
        print(self.server.clients)
        self.server.disconnect_clients_abruptly()
        print(self.server.clients)
        print("after disconnect")

Result: before disconnect

[{'id': 1, 'handler': <__main__.WSWithBinary object at 0x7ff85472c370>, 'address': ('192.168.23.143', 54582)}, {'id': 2, 'handler': <__main__.WSWithBinary object at 0x7ff8547f0a60>, 'address': ('192.168.23.143', 54584)}]
Client(1) disconnected
[{'id': 2, 'handler': <__main__.WSWithBinary object at 0x7ff8547f0a60>, 'address': ('192.168.23.143', 54584)}]
after disconnect

Possible fix:

    def _terminate_client_handlers(self):
        clients_list = self.clients.copy()
        for client in clients_list:
            self._terminate_client_handler(client["handler"])

vzarutskiy avatar Mar 15 '23 07:03 vzarutskiy