Skip to content

Commit

Permalink
Explicitly prevent module reloading (PR #7784)
Browse files Browse the repository at this point in the history
# Description

Explicitly prevent reloads, as this still caused issues

# Self Check:

Strike through any lines that are not applicable (`~~line~~`) then check the box

- [ ] Attached issue to pull request
- [x] Changelog entry
- [x] Type annotations are present
- [x] Code is clear and sufficiently documented
- [x] No (preventable) type errors (check using make mypy or make mypy-diff)
- [ ] Sufficient test cases (reproduces the bug/tests the requested feature)
- [x] Correct, in line with design
- [x] End user documentation is included or an issue is created for end-user documentation (add ref to issue here: )
- [ ] If this PR fixes a race condition in the test suite, also push the fix to the relevant stable branche(s) (see [test-fixes](https://internal.inmanta.com/development/core/tasks/build-master.html#test-fixes) for more info)
  • Loading branch information
wouterdb authored and inmantaci committed Jun 28, 2024
1 parent 30df02c commit 7992123
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 5 deletions.
3 changes: 3 additions & 0 deletions changelogs/unreleased/agent_executor_6_4.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
description: Explicitly prevent module reloading
change-type: patch
destination-branches: [master]
3 changes: 2 additions & 1 deletion src/inmanta/agent/forking_executor.py
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,8 @@ async def call(self, context: ExecutorContext) -> typing.Sequence[inmanta.loader
for module_source in in_place:
try:
await loop.run_in_executor(
context.threadpool, functools.partial(loader._load_module, module_source.name, module_source.hash_value)
context.threadpool,
functools.partial(loader._load_module, module_source.name, module_source.hash_value, require_reload=False),
)
except Exception:
logger.info("Failed to load sources: %s", module_source, exc_info=True)
Expand Down
14 changes: 10 additions & 4 deletions src/inmanta/loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -280,22 +280,28 @@ def __check_dir(self, clean: bool = False) -> None:
if not os.path.exists(os.path.join(self.__code_dir, MODULE_DIR)):
os.makedirs(os.path.join(self.__code_dir, MODULE_DIR), exist_ok=True)

def _load_module(self, mod_name: str, hv: str) -> None:
def _load_module(self, mod_name: str, hv: str, require_reload: bool = True) -> None:
"""
Load or reload a module
:arg require_reload: if set to true, we will explcitly reload modules, otherwise we keep them as is
"""

# Importing a module -> only the first import loads the code
# cache of loaded modules mechanism -> starts afresh when agent is restarted
try:
if mod_name in self.__modules:
mod = importlib.reload(self.__modules[mod_name][1])
if require_reload:
mod = importlib.reload(self.__modules[mod_name][1])
else:
LOGGER.debug("Not reloading module %s", mod_name)
return
else:
mod = importlib.import_module(mod_name)
self.__modules[mod_name] = (hv, mod)
LOGGER.info("Loaded module %s" % mod_name)
LOGGER.info("Loaded module %s", mod_name)
except ImportError:
LOGGER.exception("Unable to load module %s" % mod_name)
LOGGER.exception("Unable to load module %s", mod_name)

def install_source(self, module_source: ModuleSource) -> bool:
"""
Expand Down

0 comments on commit 7992123

Please sign in to comment.