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.
This commit is contained in:
Pedro Gimeno 2013-07-01 07:36:22 +02:00
parent bfdb04716a
commit 95a1afb84b

View File

@ -66,7 +66,10 @@ class receiveDataThread(threading.Thread):
shared.printLock.release() shared.printLock.release()
while True: while True:
try: try:
dataLen = len(self.data)
self.data += self.sock.recv(4096) 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: except socket.timeout:
shared.printLock.acquire() shared.printLock.acquire()
print 'Timeout occurred waiting for data from', self.HOST + '. Closing receiveData thread. (ID:', str(id(self)) + ')' print 'Timeout occurred waiting for data from', self.HOST + '. Closing receiveData thread. (ID:', str(id(self)) + ')'