From e323263795a0af4c9c61992bd7a3347d8928db39 Mon Sep 17 00:00:00 2001 From: Xin Wang Date: Thu, 14 Sep 2023 16:52:54 +0800 Subject: [PATCH] Fix getattr AttributeError in multi-thread scenario When multi-thread is used, the plugin loader may raise AttributeError while getting "ActionModule" from an action plugin. The reason is that cache is used while loading module. Before a module is fully loaded, it is already put into the sys.modules cache. When another thread tries to load the same module, it would load an incompletely loaded module from the sys.modules cache. Then while getting "ActionModule" attribute from this module, exception AttributeError could be raised. Signed-off-by: Xin Wang --- lib/ansible/plugins/loader.py | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/lib/ansible/plugins/loader.py b/lib/ansible/plugins/loader.py index 74bdeb5719..251531b62e 100644 --- a/lib/ansible/plugins/loader.py +++ b/lib/ansible/plugins/loader.py @@ -801,15 +801,14 @@ class PluginLoader: warnings.simplefilter("ignore", RuntimeWarning) spec = importlib.util.spec_from_file_location(to_native(full_name), to_native(path)) module = importlib.util.module_from_spec(spec) - - # mimic import machinery; make the module-being-loaded available in sys.modules during import - # and remove if there's a failure... - sys.modules[full_name] = module - try: spec.loader.exec_module(module) + # mimic import machinery; make the module-being-loaded available in sys.modules during import + # and remove if there's a failure... + sys.modules[full_name] = module except Exception: - del sys.modules[full_name] + if full_name in sys.modules: + del sys.modules[full_name] raise return module -- 2.25.1