From 3d92c4e096dca27b95e485b70594186151e40092 Mon Sep 17 00:00:00 2001 From: Alberto Planas Date: Thu, 12 Mar 2020 16:39:42 +0100 Subject: [PATCH] loader: invalidate the import cachefor extra modules Because we are mangling with importlib, we can find from time to time an invalidation issue with sys.path_importer_cache, that requires the removal of FileFinder that remain None for the extra_module_dirs (cherry picked from commit 0fb8e707a45d5caf40759e8b4943590d6fce5046) --- salt/loader.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/salt/loader.py b/salt/loader.py index 52cb4cfcb5..26b44de511 100644 --- a/salt/loader.py +++ b/salt/loader.py @@ -1506,9 +1506,11 @@ class LazyLoader(salt.utils.lazy.LazyDict): self._clean_module_dirs.append(directory) def __clean_sys_path(self): + invalidate_path_importer_cache = False for directory in self._clean_module_dirs: if directory in sys.path: sys.path.remove(directory) + invalidate_path_importer_cache = True self._clean_module_dirs = [] # Be sure that sys.path_importer_cache do not contains any @@ -1516,6 +1518,16 @@ class LazyLoader(salt.utils.lazy.LazyDict): if USE_IMPORTLIB: importlib.invalidate_caches() + # Because we are mangling with importlib, we can find from + # time to time an invalidation issue with + # sys.path_importer_cache, that requires the removal of + # FileFinder that remain None for the extra_module_dirs + if invalidate_path_importer_cache: + for directory in self.extra_module_dirs: + if directory in sys.path_importer_cache \ + and sys.path_importer_cache[directory] is None: + del sys.path_importer_cache[directory] + def _load_module(self, name): mod = None fpath, suffix = self.file_mapping[name][:2] -- 2.16.4