user1010775 user1010775 - 5 months ago 106
Python Question

python socket stays CLOSE_WAIT

i have an application which listen on sockets. this application is monitored by nagios. the problem is, that nagios opens sockets and closes those directly.My application sockets stays in CLOSE_WAIT. And i dont understand why this happens. It should run into an error and kill the socket.

while request=="":
try:
request = self.client.recv ( 1024 ).rstrip()
except socket.timeout, msg:
log.error( "no request")
self.client.close()
return
except socket.error, msg:
print msg
self.client.close()
return
except msg:
log.error(msg)
self.client.close()
return


Any ideas how to catch this properly?

Answer

CLOSE_WAIT state means that the socket is expected to be closed by the application.

In the code you posted the socket is only closed on exception. Make sure you close the socket when it receives EOF, i.e. check for not request before stripping it.

Comments