From 4c6b38a6e23c2ea20f1de51360e3166f27db6cfc Mon Sep 17 00:00:00 2001 From: Fabio Manganiello Date: Wed, 22 Aug 2018 02:11:51 +0200 Subject: [PATCH] Double lock check to make get_plugin thread-safe --- platypush/context/__init__.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/platypush/context/__init__.py b/platypush/context/__init__.py index 7e6e001d..e701a0c6 100644 --- a/platypush/context/__init__.py +++ b/platypush/context/__init__.py @@ -71,8 +71,12 @@ def get_plugin(plugin_name, reload=False): global plugins global plugins_init_locks - if plugin_name in plugins and not reload: - return plugins[plugin_name] + 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: + return plugins[plugin_name] try: plugin = importlib.import_module('platypush.plugins.' + plugin_name) @@ -91,13 +95,9 @@ def get_plugin(plugin_name, reload=False): try: 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]: plugin = plugin_class(**plugin_conf) - - plugins[plugin_name] = plugin except AttributeError as e: logger.warning('No such class in {}: {}'.format(plugin_name, cls_name)) raise RuntimeError(e)