Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FIX: Ensure closing log files on reload #2435

Merged
merged 3 commits into from
Dec 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading