Double lock check to make get_plugin thread-safe

This commit is contained in:
Fabio Manganiello 2018-08-22 02:11:51 +02:00
parent 105d5bb2fc
commit 4c6b38a6e2

View file

@ -71,6 +71,10 @@ def get_plugin(plugin_name, reload=False):
global plugins global plugins
global plugins_init_locks global plugins_init_locks
if plugin_name not in plugins_init_locks:
plugins_init_locks[plugin_name] = Lock()
with plugins_init_locks[plugin_name]:
if plugin_name in plugins and not reload: if plugin_name in plugins and not reload:
return plugins[plugin_name] return plugins[plugin_name]
@ -91,13 +95,9 @@ def get_plugin(plugin_name, reload=False):
try: try:
plugin_class = getattr(plugin, cls_name) plugin_class = getattr(plugin, cls_name)
if plugin_name not in plugins_init_locks:
plugins_init_locks[plugin_name] = Lock()
with plugins_init_locks[plugin_name]: with plugins_init_locks[plugin_name]:
plugin = plugin_class(**plugin_conf) plugin = plugin_class(**plugin_conf)
plugins[plugin_name] = plugin
except AttributeError as e: except AttributeError as e:
logger.warning('No such class in {}: {}'.format(plugin_name, cls_name)) logger.warning('No such class in {}: {}'.format(plugin_name, cls_name))
raise RuntimeError(e) raise RuntimeError(e)