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

send_message_to_all sporadicaly causes exception #108

Open baryluk opened 1 year ago

baryluk commented 1 year ago
#!/usr/bin/env python3

import logging
import random
import time

import websocket_server

def new_client(client, server):
    server.send_message_to_all("Hey all, a new client has joined us")

server = websocket_server.WebsocketServer(host='127.0.0.1', port=13254, loglevel=logging.INFO)
server.set_fn_new_client(new_client)
server.run_forever(threaded=True)
m = f"foo {random.randint(1, 10000000)} {'x'*100000}"
while True:
    server.send_message_to_all(m)

Connected by custom client written in D programing language (dlang), and after receiving either 39104727 or 79208747 or 149915025 bytes of payload (application data length sum), server does this:

$ ./server_example.py 
INFO:websocket_server.websocket_server:Listening on port 13254 for clients..
INFO:websocket_server.websocket_server:Starting WebsocketServer on thread Thread-1 (serve_forever).
INFO:websocket_server.websocket_server:Client closed connection.
Traceback (most recent call last):
  File "/home/user/vps4/home/baryluk/Projects/prd-ng/./server_example.py", line 20, in <module>
    server.send_message_to_all(m)
  File "/home/user/.local/lib/python3.10/site-packages/websocket_server/websocket_server.py", line 81, in send_message_to_all
    self._multicast(msg)
  File "/home/user/.local/lib/python3.10/site-packages/websocket_server/websocket_server.py", line 199, in _multicast
    self._unicast(client, msg)
  File "/home/user/.local/lib/python3.10/site-packages/websocket_server/websocket_server.py", line 195, in _unicast
    receiver_client['handler'].send_message(msg)
  File "/home/user/.local/lib/python3.10/site-packages/websocket_server/websocket_server.py", line 344, in send_message
    self.send_text(message)
  File "/home/user/.local/lib/python3.10/site-packages/websocket_server/websocket_server.py", line 413, in send_text
    self.request.send(header + payload)
BrokenPipeError: [Errno 32] Broken pipe

Technically there was a bug in my client (now fixed), but it should not cause the server to crash like this. Instead a connection should be closed, and message delivered to other clients, and send_message_to_all should not fail.

aaronnewsome commented 1 year ago

Hi baryluk. I'm seeing a similar issue with a simple websocket server I'm making. My server does a send_message_to_all about once per second. I've got 4 or 5 clients, all on my local network. I've been testing for about 5 days and sometimes things work great for as much as 24 hours. However, if a client loses WiFi or otherwise disappears without a graceful disconnect, the send_message_to_all seems to hang.

Did you ever figure out a way to avoid having flaky clients disrupt the server? I'd like a way to be a bit more aggressive with disregarding flaky clients from the server side.