From 89979f1bee0f432861533d02dd3e7a5b235a7ed6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gina=20H=C3=A4u=C3=9Fge?= Date: Wed, 21 Nov 2018 16:14:06 +0100 Subject: [PATCH] Revert "Hardening against bad plugins" Making on_after_startup asynchronous unearths some odd issue that causes subsequent callback calls to duplicate. It might just be PyCharm's debugger (disabling it makes the problem go away and I so far couldn't reproduce it without it), but just in the case that I introduced some weird race condition with this _async approach I'm removing it. I'd rather have badly implemented plugins break the startup then the startup being inherently broken from the get go. Added a couple more warnings in the docs though. --- src/octoprint/events.py | 5 +---- src/octoprint/plugin/__init__.py | 31 ++++++++++++------------------- src/octoprint/plugin/types.py | 11 +++++++---- src/octoprint/server/__init__.py | 8 +------- 4 files changed, 21 insertions(+), 34 deletions(-) diff --git a/src/octoprint/events.py b/src/octoprint/events.py index c4256bca5..4b2e2a12b 100644 --- a/src/octoprint/events.py +++ b/src/octoprint/events.py @@ -165,13 +165,10 @@ class EventManager(object): except: self._logger.exception("Got an exception while sending event %s (Payload: %r) to %s" % (event, payload, listener)) - # We don't want plugins to block or outright kill this thread by accident, so let's call on_event - # asynchronously. Not ideal, but too much coding with severe side effects out there... octoprint.plugin.call_plugin(octoprint.plugin.types.EventHandlerPlugin, "on_event", args=(event, payload), - initialized=True, - _async=True) + initialized=True) self._logger.info("Event loop shut down") except: self._logger.exception("Ooops, the event bus worker loop crashed") diff --git a/src/octoprint/plugin/__init__.py b/src/octoprint/plugin/__init__.py index eac2e6cd0..9bcdcd625 100644 --- a/src/octoprint/plugin/__init__.py +++ b/src/octoprint/plugin/__init__.py @@ -170,7 +170,7 @@ def plugin_settings_for_settings_plugin(plugin_key, instance, settings=None): def call_plugin(types, method, args=None, kwargs=None, callback=None, error_callback=None, sorting_context=None, - initialized=True, _async=False): + initialized=True): """ Helper method to invoke the indicated ``method`` on all registered plugin implementations implementing the indicated ``types``. Allows providing method arguments and registering callbacks to call in case of success @@ -208,8 +208,6 @@ def call_plugin(types, method, args=None, kwargs=None, callback=None, error_call identifier, ``plugin`` the plugin implementation instance itself and ``exc`` the caught exception. initialized (boolean): Whether the plugin needs to be initialized (True) or not (False). Initialization status is determined be presence of injected ``_identifier`` property. - _async (boolean): Whether the plugins are called asynronously in individual threads, or consecutively. - """ if not isinstance(types, (list, tuple)): @@ -219,28 +217,23 @@ def call_plugin(types, method, args=None, kwargs=None, callback=None, error_call if kwargs is None: kwargs = dict() + logger = logging.getLogger(__name__) + plugins = plugin_manager().get_implementations(*types, sorting_context=sorting_context) for plugin in plugins: if initialized and not hasattr(plugin, "_identifier"): continue if hasattr(plugin, method): - def run(): - try: - result = getattr(plugin, method)(*args, **kwargs) - if callback: - callback(plugin._identifier, plugin, result) - except Exception as exc: - logging.getLogger(__name__).exception("Error while calling plugin %s" % plugin._identifier) - if error_callback: - error_callback(plugin._identifier, plugin, exc) - - if _async: - thread = threading.Thread(target=run) - thread.daemon = True - thread.start() - else: - run() + logger.debug("Calling {} on {}".format(method, plugin._identifier)) + try: + result = getattr(plugin, method)(*args, **kwargs) + if callback: + callback(plugin._identifier, plugin, result) + except Exception as exc: + logger.exception("Error while calling plugin %s" % plugin._identifier) + if error_callback: + error_callback(plugin._identifier, plugin, exc) class PluginSettings(object): diff --git a/src/octoprint/plugin/types.py b/src/octoprint/plugin/types.py index 5e433417d..2bfd5063f 100644 --- a/src/octoprint/plugin/types.py +++ b/src/octoprint/plugin/types.py @@ -158,7 +158,7 @@ class StartupPlugin(OctoPrintPlugin, SortablePlugin): .. warning:: - Do not perform long-running or even blocking operations in your implementation or you **will** block the server. + Do not perform long-running or even blocking operations in your implementation or you **will** block and break the server. The relevant sorting context is ``StartupPlugin.on_startup``. @@ -174,8 +174,7 @@ class StartupPlugin(OctoPrintPlugin, SortablePlugin): .. warning:: - Do not perform long-running or even blocking operations in your implementation or you **will** block the server - in OctoPrint versions earlier than 1.3.10. + Do not perform long-running or even blocking operations in your implementation or you **will** block and break the server. The relevant sorting context is ``StartupPlugin.on_after_startup``. """ @@ -199,7 +198,7 @@ class ShutdownPlugin(OctoPrintPlugin, SortablePlugin): .. warning:: - Do not perform long-running or even blocking operations in your implementation or you **will** block the server. + Do not perform long-running or even blocking operations in your implementation or you **will** block and break the server. The relevant sorting context is ``ShutdownPlugin.on_shutdown``. """ @@ -1812,6 +1811,10 @@ class EventHandlerPlugin(OctoPrintPlugin): """ Called by OctoPrint upon processing of a fired event on the platform. + .. warning:: + + Do not perform long-running or even blocking operations in your implementation or you **will** block and break the server. + Arguments: event (str): The type of event that got fired, see :ref:`the list of events ` for possible values diff --git a/src/octoprint/server/__init__.py b/src/octoprint/server/__init__.py index b6f27d6b8..c751b59dd 100644 --- a/src/octoprint/server/__init__.py +++ b/src/octoprint/server/__init__.py @@ -716,15 +716,9 @@ class Server(object): # create a single use thread in which to perform our after-startup-tasks, start that and hand back # control to the ioloop def work(): - # Since some plugin authors out there apparently will happily block or outright kill this thread - # in on_after_startup, we spawn individual threads. Yes, I'm now happy with this either and would - # have preferred to use a ThreadPoolExecutor here, but since I can't set a timeout for the workers - # on that (only for the future result) everything might _still_ be blocked. So individual threads - # via _async it is. Meh. octoprint.plugin.call_plugin(octoprint.plugin.StartupPlugin, "on_after_startup", - sorting_context="StartupPlugin.on_after_startup", - _async=True) + sorting_context="StartupPlugin.on_after_startup") def call_on_after_startup(name, plugin): implementation = plugin.get_implementation(octoprint.plugin.StartupPlugin)