I'm trying to track down a mystery exception from socket.recv() that has no message to provide a clue as to where it's coming from, or why. This is on Windows 7, Python 2.7.
I create a blocking socket like this:
self.client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.client.settimeout(5)
I connect to a piece of equipment which streams data out. I never send anything through this socket, I only receive. So I spin in a loop calling select:
while(True):
# Process commands from the GUI. These can include requests to
# connect, disconnect, or quit
quitting = self.processCmds()
if quitting == True:
print 'exiting'
return
try:
rlist, wlist, errlist = select.select([self.client], [], [self.client], 5)
if self.client in errlist:
self.reconnect()
continue
elif self.client in rlist:
# We have a message.
msg = recvall(self.client, MESSAGE_LENGTH)
if msg == None:
self.reconnect()
continue
So far, so good. rcvall() spins in a loop the way you'd expect, trying to read the requested number of bytes:
def recvall(conn, count):
buf = b''
numIterations = 0
while count:
try:
recvString = conn.recv(count)
except:
# OH NO SOMETHING BAD HAPPENED!
e = sys.exc_info()[0]
return None
if not recvString:
# the socket has been closed by the remote end
return None
if (len(recvString) > 0):
buf = buf + recvString
count = count - len(recvString)
else:
# If the other guy has sent no bytes, don't wait forever
numIterations += 1
if numIterations > 100:
return None
return buf
So if anything goes wrong, recvall() returns None, which tells the caller to close the existing connection and open a new one.
This works nicely for about 8 hours or so, but then I see a stream of reconnection attempts in my log, none successful. After some hunting and pecking with breakpoints I learn that we're hitting the exception in rcvall() where the comment OH NO SOMETHING BAD HAPPENED! is. But when I attempt to examine the exception object in the debugger, its message member is None. In fact, all its members are None, so there's no clue about who's raising it or why.
There are a couple of weird things about this. The exception is happening AFTER select() has already told me that the socket is error-free and contains data. Can something go wrong between the call to select() and the call to recvall()? It's possible, but hardly seems likely.
second, has anyone ever seen such a weird exception coming from socket.recv? Any clue where to look for it?