Hardening against bad plugins
We don't want on_after_startup or on_event handling to get blocked by some rogue plugin that decides to block or outright kill the thread.
This commit is contained in:
1 parent
ff4ed38329
commit
0c4c4a9d18
3 files changed
+31
-11
No files matched your search
@@ -165,10 +165,13 @@ 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)
|
||||
initialized=True,
|
||||
_async=True)
|
||||
self._logger.info("Event loop shut down")
|
||||
except:
|
||||
self._logger.exception("Ooops, the event bus worker loop crashed")
|
||||
|
||||
@@ -21,6 +21,7 @@ __copyright__ = "Copyright (C) 2014 The OctoPrint Project - Released under terms
|
||||
|
||||
import os
|
||||
import logging
|
||||
import threading
|
||||
|
||||
from octoprint.settings import settings as s
|
||||
from octoprint.plugin.core import (PluginInfo, PluginManager, Plugin)
|
||||
@@ -168,7 +169,8 @@ def plugin_settings_for_settings_plugin(plugin_key, instance, settings=None):
|
||||
return plugin_settings(plugin_key, get_preprocessors=get_preprocessors, set_preprocessors=set_preprocessors, settings=settings)
|
||||
|
||||
|
||||
def call_plugin(types, method, args=None, kwargs=None, callback=None, error_callback=None, sorting_context=None, initialized=True):
|
||||
def call_plugin(types, method, args=None, kwargs=None, callback=None, error_callback=None, sorting_context=None,
|
||||
initialized=True, _async=False):
|
||||
"""
|
||||
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
|
||||
@@ -206,6 +208,7 @@ 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.
|
||||
|
||||
"""
|
||||
|
||||
@@ -222,14 +225,22 @@ def call_plugin(types, method, args=None, kwargs=None, callback=None, error_call
|
||||
continue
|
||||
|
||||
if hasattr(plugin, method):
|
||||
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)
|
||||
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()
|
||||
|
||||
|
||||
class PluginSettings(object):
|
||||
|
||||
@@ -716,9 +716,15 @@ 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")
|
||||
sorting_context="StartupPlugin.on_after_startup",
|
||||
_async=True)
|
||||
|
||||
def call_on_after_startup(name, plugin):
|
||||
implementation = plugin.get_implementation(octoprint.plugin.StartupPlugin)
|
||||
|
||||
Reference in new issue
Block a user