Pithikos / python-websocket-server

A simple fully working websocket-server in Python with no external dependencies
MIT License
1.13k stars 380 forks source link

server freeze during sending messages #77

Closed ghost closed 2 years ago

ghost commented 5 years ago

I like your webscoket server its very helpful to me :] But i have a big problem sometimes when sending a message from the thread "a" the script freeze that happens after calling self.request.send(header + payload) Informations that can help: The server sends about 16packet/s for every client and there is on average 50 clients connected to the server on the same time, the script freeze once every 5minutes-2hours so i need to manualy restart the script everytime that happens and they need to reconnect. tested the script on both windows 10 and ubuntu 18,python 3.6 and python 3.7 and still the same problem.

a = threading.Thread(target=_script)
a.start()
server = WebsocketServer(13355,'0.0.0.0',dir)
server.set_fn_message_received(_read)
server.set_fn_client_left(_close)
server.run_forever()

also added ssl

def __init__(self, port, host, dir, loglevel=logging.WARNING):
        logger.setLevel(loglevel)
        TCPServer.__init__(self, (host, port), WebSocketHandler)
        self.port = self.socket.getsockname()[1]
        key_file = dir+'private.key'
        cert_file = dir+'certificate.crt'
        ca_file = dir+'ca_bundle.crt'
        self.socket = ssl.wrap_socket(self.socket, keyfile=key_file, certfile=cert_file, ca_certs=ca_file, cert_reqs=ssl.CERT_NONE)
NotMedic commented 5 years ago

Hey @ZeRo137 Does your traceback look like this? If so, I've got a similar issue.

----------------------------------------
Exception happened during processing of request from ('x.x.x.x', 65157)
Traceback (most recent call last):
  File "/usr/lib/python2.7/SocketServer.py", line 596, in process_request_thread
    self.finish_request(request, client_address)
  File "/usr/lib/python2.7/SocketServer.py", line 331, in finish_request
    self.RequestHandlerClass(request, client_address, self)
  File "/home/ubuntu/tools/python-websocket-server/websocket_server/websocket_server.py", line 168, in __init__
    StreamRequestHandler.__init__(self, socket, addr, server)
  File "/usr/lib/python2.7/SocketServer.py", line 652, in __init__
    self.handle()
  File "/home/ubuntu/tools/python-websocket-server/websocket_server/websocket_server.py", line 181, in handle
    self.read_next_message()
  File "/home/ubuntu/tools/python-websocket-server/websocket_server/websocket_server.py", line 243, in read_next_message
    opcode_handler(self, message_bytes.decode('utf8'))
  File "/home/ubuntu/tools/python-websocket-server/websocket_server/websocket_server.py", line 127, in _message_received_
    self.message_received(self.handler_to_client(handler), self, msg)
  File "server.py", line 80, in message_received
    server.send_message_to_all(message)
  File "/home/ubuntu/tools/python-websocket-server/websocket_server/websocket_server.py", line 88, in send_message_to_all
    self._multicast_(msg)
  File "/home/ubuntu/tools/python-websocket-server/websocket_server/websocket_server.py", line 156, in _multicast_
    self._unicast_(client, msg)
  File "/home/ubuntu/tools/python-websocket-server/websocket_server/websocket_server.py", line 152, in _unicast_
    to_client['handler'].send_message(msg)
  File "/home/ubuntu/tools/python-websocket-server/websocket_server/websocket_server.py", line 246, in send_message
    self.send_text(message)
  **File "/home/ubuntu/tools/python-websocket-server/websocket_server/websocket_server.py", line 296, in send_text
    self.request.send(header + payload)
error: [Errno 32] Broken pipe**
----------------------------------------
NotMedic commented 5 years ago

This pull request seems to have fixed my issue.

https://github.com/Pithikos/python-websocket-server/pull/40/commits/3ececb3b35e5a04ef55ffc42e8524442a1938b51

Pithikos commented 2 years ago

This has been fix with v0.6.1