From 95a1afb84b22884b8581a3e45825d9d4d9348d60 Mon Sep 17 00:00:00 2001 From: Pedro Gimeno Date: Mon, 1 Jul 2013 07:36:22 +0200 Subject: [PATCH] Fix issue #183 (CPU 100% usage) As per http://docs.python.org/2/howto/sockets.html#using-a-socket it's possible that a socket recv() call returns 0 bytes if the remote closes the connection. In that case, recv() does not obey settimeout(): it just doesn't block and returns zero bytes immediately, which in this case results in an infinite loop if the transmission was incomplete. --- src/class_receiveDataThread.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/class_receiveDataThread.py b/src/class_receiveDataThread.py index e5293fe8..a023f80b 100644 --- a/src/class_receiveDataThread.py +++ b/src/class_receiveDataThread.py @@ -66,7 +66,10 @@ class receiveDataThread(threading.Thread): shared.printLock.release() while True: try: + dataLen = len(self.data) self.data += self.sock.recv(4096) + if len(self.data) == dataLen: # recv returns 0 bytes when the remote closes the connection + raise Exception("Remote closed the connection") except socket.timeout: shared.printLock.acquire() print 'Timeout occurred waiting for data from', self.HOST + '. Closing receiveData thread. (ID:', str(id(self)) + ')' -- 2.45.1