python: how to close orphaned domain socket from new process -
i planing use domain sockets lock ensure 1 active instance (linux kernel >=2.6).
so, binding domain socket active process , create lock file pid more human-friendly check option in fs.
import socket class processlock(object): def __init__(self, processname): self.processname = processname self.pidfile = os.path.join(self.gettmpdir(),"uid-%s_process-%s.pid" % (os.getuid(), processname) ) ... self.socket.bind('\0' + self.processname) ... atexit.register(self.cleanup) ... def cleanup(self): self.socket.shutdown(socket.shut_rdwr) self.socket.close() self.socketok = false os.remove(self.pidfile)
it working, managed kill process hard did not close socket @ exit , socket kept open beyond process' lifetime:
> lsof condorjob 124485 root 4u unix 0xffff88186c3341c0 0t0 175181414 @testfooname condorjob 124485 root 5w reg 8,6 0 13893673 /var/tmp/uid-0_process-testfooname.pid (deleted)
since have pid file cross-check, close socket (after sanity checking in process tree one). however, cannot bind socket new instance since still bound ~zombie socket.
is there python way close existing unix domain sockets (running pid 0 if necessary)?
it mistake on side. had sent manually process sleep in tab , did not realized despite lsof showing pid. after killing process, kernel closed socket intended. working expected.
Comments
Post a Comment