From 068dea8971ac2f35cc25e9e50e49b6a39eae653f Mon Sep 17 00:00:00 2001 From: Cyril Jaquier Date: Sun, 10 Sep 2006 20:50:07 +0000 Subject: [PATCH] - Fixed the shutdown issue with timeout on socket git-svn-id: https://fail2ban.svn.sourceforge.net/svnroot/fail2ban/trunk@344 a942ae1a-1317-0410-a47c-b1dcaea8d605 --- server/ssocket.py | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/server/ssocket.py b/server/ssocket.py index 78330afc..4146082c 100644 --- a/server/ssocket.py +++ b/server/ssocket.py @@ -55,7 +55,10 @@ class SSocket(Thread): self.ssock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) #self.ssock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) #self.ssock.setblocking(False) - self.ssock.setblocking(True) + # Do not use a blocking socket as there is problem at shutdown. + # Use a timeout instead. Daemon exits at most 'timeout' seconds + # after the command. + self.ssock.settimeout(1) # Bind the socket to a public host and a well-known port #self.ssock.bind(("localhost", 2222)) self.ssock.bind(SSocket.SOCKET_FILE) @@ -65,11 +68,13 @@ class SSocket(Thread): def run(self): self.isRunning = True while self.isRunning: - # TODO Fix shutdown. A new request is required because accept() - # is blocking. - (csock, address) = self.ssock.accept() - thread = SocketWorker(csock, self.transmit) - thread.start() + try: + (csock, address) = self.ssock.accept() + thread = SocketWorker(csock, self.transmit) + thread.start() + except socket.timeout: + # Do nothing here + pass self.ssock.close() # Remove socket if os.path.exists(SSocket.SOCKET_FILE): @@ -101,6 +106,7 @@ class SocketWorker(Thread): msg = self.transmit.proceed(msg) self.send(self.csock, msg) self.csock.close() + logSys.debug("Connection closed") def send(self, socket, msg): obj = pickle.dumps(msg)