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.
This commit is contained in:
Gina Häußge committed 2018-11-21 16:14:06 +01:00
1 parent bd241e8e6c
commit 89979f1bee
4 files changed
+21 -34

No files matched your search

+1 -4
View File
@@ -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")
+12 -19
View File
@@ -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):
+7 -4
View File
@@ -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 <sec-events-available_events>`
for possible values
+1 -7
View File
@@ -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)