multithreading - Why does my python socket in a thread not always close properly (i.e. if i run the program many times in a row) -


i have following code (with may debugging messages). if run few times in row (within seconds of each-other), address in use error. doesn't happen every time.

socket.error: [errno 98] address in use 

when don't error here output:

 trying recv ('127.0.0.1', 38041)  recved  client -->  none genuine without seal!  shutting down con  closing con  done closing con  shutting down  running server shutdown  running server close  done closing  done shutting down 

the following code:

import socket threading import thread time import sleep subprocess import popen, pipe, stdout   def shutdown_thread(t):     print "shutting down"     t.shutdown()     while t.isalive():         sleep(.1)     print "done shutting down"   def send_with_netcat(msg):     nc = popen(         ['nc', '127.0.0.1', '8000'],         stdin=pipe,         stdout=pipe,         stderr=stdout)      result = nc.communicate(msg)     return result   class server(thread):     is_shutdown = false      def __init__(self):         super(server, self).__init__()         self.start_server()      def start_server(self):         self.server = socket.socket(             socket.af_inet,             socket.sock_stream)          # "a socket 5 tuple (proto, local addr, local port,         # remote addr, remote port).  so_reuseaddr says         # can reuse local addresses.  5 tuple still must         # unique!"         self.server.setsockopt(             socket.sol_socket,             socket.so_reuseaddr,             1)          self.server = socket.socket(             socket.af_inet,             socket.sock_stream)          # bind socket         # error happens here, how deal deal or avoid?         self.server.bind(('127.0.0.1', 8000))         self.server.listen(5)      def shutdown(self):         self.is_shutdown = true      def run(self):         while not self.is_shutdown:             con, addr = self.server.accept()             print "trying recv", addr             buf = con.recv(128)             print "recved"             if len(buf) > 0:                 print "from client --> ", buf              print "shutting down con"             con.shutdown(socket.shut_rdwr)             print "closing con"             con.close()             print "done closing con"             sleep(.1)          print "running server shutdown"         self.server.shutdown(socket.shut_rdwr)         print "running server close"         self.server.close()         print "done closing"  if __name__ == '__main__':     s = server()     s.start()     send_with_netcat("none genuine without seal!")     shutdown_thread(s)     sleep(5) 

if find github more readable can see same code here

oddly when trying make post took may more times needed. because many more connections happening on machine randomize client nc connection?

thank clarity on issue.

in start_server() method you're opening socket in self.server, you're setting so_reuseaddr on socket, , you're calling socket.socket() again, erasing setsockopt() call. try deleting second socket.socket() call. should put so_reuseaddr flag effect.


Comments

Popular posts from this blog

java - WrongTypeOfReturnValue exception thrown when unit testing using mockito -

php - Magento - Deleted Base url key -

android - How to disable Button if EditText is empty ? -