From c6b18a46730b58f14d337e0985f88f1809c44a81 Mon Sep 17 00:00:00 2001 From: Claudio-Daniel Freire Date: Thu, 8 Sep 2011 12:52:18 +0200 Subject: [PATCH] Make servers able to launch when a stale ctrl.sock from a pervious server remains. ctrl.sock sockets are usually left behind when servers are killed or die, since they're not cleaned up automatically by the OS like other sockets --- src/nepi/util/server.py | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/src/nepi/util/server.py b/src/nepi/util/server.py index 704984c2..0a952751 100644 --- a/src/nepi/util/server.py +++ b/src/nepi/util/server.py @@ -24,6 +24,7 @@ import functools import collections CTRL_SOCK = "ctrl.sock" +CTRL_PID = "ctrl.pid" STD_ERR = "stderr.log" MAX_FD = 1024 @@ -198,8 +199,35 @@ class Server(object): # create control socket self._ctrl_sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) - self._ctrl_sock.bind(CTRL_SOCK) + try: + self._ctrl_sock.bind(CTRL_SOCK) + except socket.error: + # Address in use, check pidfile + pid = None + try: + pidfile = open(CTRL_PID, "r") + pid = pidfile.read() + pidfile.close() + pid = int(pid) + except: + # no pidfile + pass + + if pid is not None: + # Check process liveliness + if not os.path.exists("/proc/%d" % (pid,)): + # Ok, it's dead, clean the socket + os.remove(CTRL_SOCK) + + # try again + self._ctrl_sock.bind(CTRL_SOCK) + self._ctrl_sock.listen(0) + + # Save pidfile + pidfile = open(CTRL_PID, "w") + pidfile.write(str(os.getpid())) + pidfile.close() # let the parent process know that the daemonization is finished os.write(w, "\n") -- 2.47.0