From 012264dce169b8ae9a18a599371bcb9685ec3570 Mon Sep 17 00:00:00 2001 From: Yaroslav Halchenko Date: Thu, 21 Feb 2013 20:58:27 -0500 Subject: [PATCH 1/2] BF: safeguard closing of log handlers + close in reverse order otherwise there might be "stuck" handler in the queue. and closing exceptions can occur -- even stock logging guards in recent versions --- server/server.py | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/server/server.py b/server/server.py index 3889c491..dea303db 100644 --- a/server/server.py +++ b/server/server.py @@ -367,11 +367,20 @@ class Server: logSys.error("Unable to log to " + target) logSys.info("Logging to previous target " + self.__logTarget) return False - # Removes previous handlers - for handler in logging.getLogger("fail2ban").handlers: - # Closes the handler. + # Removes previous handlers -- in reverse order since removeHandler + # alter the list in-place and that can confuses the iterable + for handler in logging.getLogger("fail2ban").handlers[::-1]: + # Remove the handler. logging.getLogger("fail2ban").removeHandler(handler) - handler.close() + # And try to close -- it might be closed already + try: + handler.flush() + handler.close() + except ValueError: + if sys.version_info >= (2,6): + raise + # is known to be thrown after logging was shutdown once + # with older Pythons -- seems to be safe to ignore there # tell the handler to use this format hdlr.setFormatter(formatter) logging.getLogger("fail2ban").addHandler(hdlr) From 154aa38e3f8174baee6593c3022c53f25b8aa364 Mon Sep 17 00:00:00 2001 From: Yaroslav Halchenko Date: Thu, 21 Feb 2013 20:59:46 -0500 Subject: [PATCH 2/2] BF: do not shutdown logging until all jails stop -- so move into Server.quit() Together with previous commit it should resolve failures with the server tests on python < 2.6 --- server/server.py | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/server/server.py b/server/server.py index dea303db..0e1b6c97 100644 --- a/server/server.py +++ b/server/server.py @@ -97,12 +97,6 @@ class Server: except OSError, e: logSys.error("Unable to remove PID file: %s" % e) logSys.info("Exiting Fail2ban") - # Shutdowns the logging. - try: - self.__loggingLock.acquire() - logging.shutdown() - finally: - self.__loggingLock.release() def quit(self): # Stop communication first because if jail's unban action @@ -112,8 +106,17 @@ class Server: # are exiting) # See https://github.com/fail2ban/fail2ban/issues/7 self.__asyncServer.stop() + # Now stop all the jails self.stopAllJail() + + # Only now shutdown the logging. + try: + self.__loggingLock.acquire() + logging.shutdown() + finally: + self.__loggingLock.release() + def addJail(self, name, backend): self.__jails.add(name, backend)