From fa148a8b7e9f4ce3b5a084b6bf33717f1aec80f3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gina=20H=C3=A4u=C3=9Fge?= Date: Mon, 6 Jul 2020 21:56:43 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20Fix=20async=20issue=20with=20log?= =?UTF-8?q?ging=20on=20shutdown?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Asynchronous components might be active after on_shutdown, and AsyncLogHandlerMixin should be able to cope with logs generated by them. Closes #3629 --- src/octoprint/logging/handlers.py | 7 +++++++ src/octoprint/plugins/tracking/__init__.py | 1 + src/octoprint/server/__init__.py | 2 ++ 3 files changed, 10 insertions(+) diff --git a/src/octoprint/logging/handlers.py b/src/octoprint/logging/handlers.py index adc079b56..bf7c47992 100644 --- a/src/octoprint/logging/handlers.py +++ b/src/octoprint/logging/handlers.py @@ -16,6 +16,9 @@ class AsyncLogHandlerMixin(logging.Handler): super(AsyncLogHandlerMixin, self).__init__(*args, **kwargs) def emit(self, record): + if getattr(self._executor, "_shutdown", False): + return + try: self._executor.submit(self._emit, record) except Exception: @@ -25,6 +28,10 @@ class AsyncLogHandlerMixin(logging.Handler): # noinspection PyUnresolvedReferences super(AsyncLogHandlerMixin, self).emit(record) + def close(self): + self._executor.shutdown(wait=True) + super(AsyncLogHandlerMixin, self).close() + class CleaningTimedRotatingFileHandler(logging.handlers.TimedRotatingFileHandler): diff --git a/src/octoprint/plugins/tracking/__init__.py b/src/octoprint/plugins/tracking/__init__.py index ff452f398..368b4749f 100644 --- a/src/octoprint/plugins/tracking/__init__.py +++ b/src/octoprint/plugins/tracking/__init__.py @@ -102,6 +102,7 @@ class TrackingPlugin(octoprint.plugin.SettingsPlugin, if not self._settings.get_boolean(["enabled"]): return self._track_shutdown() + self._executor.shutdown(wait=True) ##~~ EventHandlerPlugin diff --git a/src/octoprint/server/__init__.py b/src/octoprint/server/__init__.py index 24b97f809..b31f11597 100644 --- a/src/octoprint/server/__init__.py +++ b/src/octoprint/server/__init__.py @@ -903,6 +903,8 @@ class Server(object): observer.stop() observer.join() eventManager.fire(events.Events.SHUTDOWN) + + self._logger.info("Calling on_shutdown on plugins") octoprint.plugin.call_plugin(octoprint.plugin.ShutdownPlugin, "on_shutdown", sorting_context="ShutdownPlugin.on_shutdown")