Search code examples
pythonsocketssocketserver

Can't reuse socket in SocketServer.TCPServer


I've got a problem with SocketServer.TCPServer. I'm running a server in a thread. Watching a directory tree with Watchdog. When "on_any_event" runs I need to take down the server and start it again. Unfortunately I can't get SocketServer.TCPServer to reuse the address. I've checked the SocketServer.py file and if allow_reuse_address is True it is supposed to set socket.SO_REUSEADDR to 1. It still fails with error: [Errno 98] Address already in use though. Sleeping for 10 secs before retrying doesn't help either. Any help?

class Server(SocketServer.TCPServer):
    allow_reuse_address = True

class ChangeHandler(FileSystemEventHandler):
    def __init__(self):
        FileSystemEventHandler.__init__(self)
        self.rebuild()

    def on_any_event(self, event):
        print event
        self.httpd.shutdown()
        self.t.join()
        self.rebuild()

    def rebuild(self):
        self.t, self.httpd = runserver()

def runserver():
    handler = SimpleHTTPServer.SimpleHTTPRequestHandler
    httpd = Server((HOST, PORT), handler, bind_and_activate=False)
    httpd.server_bind()
    httpd.server_activate()
    t = threading.Thread(target=httpd.serve_forever)
    t.daemon = True
    t.start()
    print "Live at http://{0}:{1}".format(HOST, PORT)
    return t, httpd

if __name__ == "__main__":
    handler = ChangeHandler()
    observer = Observer()
    observer.schedule(handler, path=ROOT, recursive=True)
    observer.start()

    try:
        while True:
            time.sleep(1)
    except KeyboardInterrupt:
        observer.stop()
    observer.join()

Solution

  • Adding a self.httpd.server_close() after self.httpd.shutdown() did the trick.