File loader-invalidate-the-import-cachefor-extra-modules.patch of Package salt

From 444e00c6601b878444923f573fdb5f000342be9a Mon Sep 17 00:00:00 2001
From: Alberto Planas <aplanas@suse.com>
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 742b2f8e22..5bd4773645 100644
--- a/salt/loader.py
+++ b/salt/loader.py
@@ -1544,9 +1544,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
@@ -1554,6 +1556,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