Lawouach/WebSocket-for-Python — Issue #23: infinite loop in threadedclient.py when server closes websocket
Author: tweber
Created: 2012-02-13 21:21:28+00:00
State: open

I am using geventclient.py which subclasses WebSocketClient in threadedclient.py. The client connects and waits for a message. If the server sends a message and immediately closes the websocket, the client goes into an infinite loop. It happens in threadedclient.py in the _receive() method. Basically, there is a socket recv() command which returns "" indicating that the server has closed. But the _receive() method does not close, and calls socket recv() again. Here is the first part of the function with my patch, which fixes the problem in my limited tests.

def _receive(self):
next_size = 2
try:
self.sock.setblocking(1)
while self.running:
if self.__buffer:
bytes, self.__buffer = self.__buffer[:next_size], self.__buffer[next_size:]
else:
bytes = self.read_from_connection(next_size)

######### patch start
if bytes is None or len(bytes) == 0:
self.server_terminated = True
self.running = False
break
######### patch end

with self._lock:
s = self.stream
next_size = s.parser.send(bytes)