Menu

#7 Possible infinite loop in __recvall

open
nobody
None
5
2010-05-18
2010-05-18
Mario Vilas
No

The recv() method of socket objects return an empty string when the connection is closed. Therefore the following code:

def __recvall(self, bytes):
"""__recvall(bytes) -> data
Receive EXACTLY the number of bytes requested from the socket.
Blocks until the required number of bytes have been received.
"""
data = ""
while len(data) < bytes:
data = data + self.recv(bytes-len(data))
return data

will loop indefinitely if the remote peer closes the connection before all the expected bytes are received. A fixed version could be:

def __recvall(self, bytes):
"""__recvall(bytes) -> data
Receive EXACTLY the number of bytes requested from the socket.
Blocks until the required number of bytes have been received.
"""
data = ""
while len(data) < bytes:
d = self.recv(bytes-len(data))
if not d: raise GeneralProxyError("connection closed unexpectedly")
data = data + d
return data

Discussion


Log in to post a comment.

MongoDB Logo MongoDB