Skip to content

Commit

Permalink
Merge pull request #2435 from kamil-certat/cleanup_log_files
Browse files Browse the repository at this point in the history
FIX: Ensure closing log files on reload
  • Loading branch information
sebix authored Dec 13, 2023
2 parents 4075768 + e8f1dc8 commit fd0baa0
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down
17 changes: 16 additions & 1 deletion intelmq/lib/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit fd0baa0

Please sign in to comment.