diff --git a/CHANGELOG.md b/CHANGELOG.md index 2613d7e96..25c1e6400 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -33,6 +33,7 @@ `intelmq.bots.collectors.stomp.collector` and `intelmq.bots.outputs.stomp.output` (PR#2408 and PR#2414 by Jan Kaliszewski). - `intelmq.lib.upgrades`: Replace deprecated instances of `url2fqdn` experts by the new `url` expert in runtime configuration (PR#2432 by Sebastian Wagner). +- `intelmq.lib.bot`: Ensure closing log files on reloading (PR#2435 by Kamil Mankowski). ### Development - Makefile: Add codespell and test commands (PR#2425 by Sebastian Wagner). diff --git a/intelmq/lib/bot.py b/intelmq/lib/bot.py index 31b6847c1..d04b24b7c 100644 --- a/intelmq/lib/bot.py +++ b/intelmq/lib/bot.py @@ -308,10 +308,25 @@ def __handle_sighup(self): self.shutdown() # disconnects, stops threads etc except Exception: self.logger.exception('Error during shutdown of bot.') - self.logger.handlers = [] # remove all existing handlers + self.__cleanup_logging_handlers() self.__sighup.clear() self.__init__(self.__bot_id_full, sighup_event=self.__sighup, standalone=self._standalone) + def __cleanup_logging_handlers(self): + # thread-safe removing of handlers and closing opened files + handlers_list = self.logger.handlers[:] + for handler in handlers_list: + try: + self.logger.removeHandler(handler) + # ensure all log files are closed to prevent caching them in RAM + if isinstance(handler, logging.FileHandler): + handler.close() + except: + # Logger should still be safe to use even without handlers + # In addition, we do not want any side issue to break execution + # - we are about to reinitialize logging. + self.logger.exception("Error while cleaning up logging handlers") + def init(self): pass