Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ModuleResponder sends ansible.cli package, and many others, probably unnecessarily #1124

Open
Tracked by #1125
moreati opened this issue Sep 21, 2024 · 2 comments · May be fixed by #1126
Open
Tracked by #1125

ModuleResponder sends ansible.cli package, and many others, probably unnecessarily #1124

moreati opened this issue Sep 21, 2024 · 2 comments · May be fixed by #1126
Labels
affects-0.3 Issues related to 0.3.X Mitogen releases bug Code feature that hinders desired execution outcome importer Issues impacting core.py, parent.py, and master.py's import client and servers performance Performance problems

Comments

@moreati
Copy link
Member

moreati commented Sep 21, 2024

Performing an Ansible ping, with -vvv shows that Mitogen is sending the package ansible.cli to the target, and many other parts of the Ansible codebase that I would expect to be unneeded and irrelevant on a target.

[mux  19249] 09:50:45.309897 D mitogen.responder: sending ansible.cli (16.54 KiB) to local.19253

Adding some instrumentation to ModuleFinder.find_related() shows that ansible.cli is being incuded as a related import of ansible.module_utils.basic, which imports __main__. import __main__ has been present in ansible.module_utils.basic since Ansible 2.7. I t was introduced in ansible/ansible@52449cc

diff --git a/mitogen/master.py b/mitogen/master.py
index b1e0a1de..3a7a72ad 100644
--- a/mitogen/master.py
+++ b/mitogen/master.py
@@ -1063,12 +1063,14 @@ class ModuleFinder(object):
             for which source code can be retrieved
         :type fullname: str
         """
+        LOG.info('find_related(%r)', fullname)
         stack = [fullname]
         found = set()
 
         while stack:
             name = stack.pop(0)
             names = self.find_related_imports(name)
+            LOG.info('find_related(%r) -> %r -> %r', fullname, name, names)
             stack.extend(set(names).difference(set(found).union(stack)))
             found.update(names)
 
[mux  19249] 09:50:44.800698 I mitogen: find_related('ansible')
[mux  19249] 09:50:44.801085 I mitogen: find_related('ansible') -> 'ansible' -> []
[mux  19249] 09:50:44.801424 D mitogen.responder: sending ansible (0.60 KiB) to local.19253
[mux  19249] 09:50:44.802015 D mitogen.responder: ansible.module_utils is a package at /nix/store/sd4jwfhmz53g9srmvgzkm2rz512v4lpf-python3.11-ansible-core-2.16.4/lib/python3.11/site-packages/ansible/module_utils/__init__.py with submodules ['_text', 'ansible_release', 'api', 'basic', 'common', 'compat', 'connection', 'csharp', 'distro', 'errors', 'facts', 'json_utils', 'parsing', 'powershell', 'pycompat24', 'service', 'six', 'splitter', 'urls', 'yumdnf']
[mux  19249] 09:50:44.802312 I mitogen: find_related('ansible.module_utils')
[mux  19249] 09:50:44.802639 I mitogen: find_related('ansible.module_utils') -> 'ansible.module_utils' -> ['ansible']
[mux  19249] 09:50:44.802923 I mitogen: find_related('ansible.module_utils') -> 'ansible' -> []
[mux  19249] 09:50:44.803223 D mitogen.responder: sending ansible.module_utils (0.54 KiB) to local.19253
[mux  19249] 09:50:44.813366 I mitogen: find_related('ansible.module_utils.basic')
[mux  19249] 09:50:44.821895 I mitogen: find_related('ansible.module_utils.basic') -> 'ansible.module_utils.basic' -> ['__main__', 'ansible', 'ansible.module_utils', 'ansible.module_utils._text', 'ansible.module_utils.common._json_compat', 'ansible.module_utils.common._utils', 'ansible.module_utils.common.arg_spec', 'ansible.module_utils.common.file', 'ansible.module_utils.common.locale', 'ansible.module_utils.common.parameters', 'ansible.module_utils.common.process', 'ansible.module_utils.common.sys_info', 'ansible.module_utils.common.text.converters', 'ansible.module_utils.common.text.formatters', 'ansible.module_utils.common.validation', 'ansible.module_utils.common.warnings', 'ansible.module_utils.compat', 'ansible.module_utils.errors', 'ansible.module_utils.parsing.convert_bool', 'ansible.module_utils.pycompat24', 'ansible.module_utils.six']
[mux  19249] 09:50:44.822577 I mitogen: find_related('ansible.module_utils.basic') -> '__main__' -> ['ansible.cli.playbook']
@moreati moreati added bug Code feature that hinders desired execution outcome importer Issues impacting core.py, parent.py, and master.py's import client and servers performance Performance problems affects-0.3 Issues related to 0.3.X Mitogen releases labels Sep 21, 2024
@moreati
Copy link
Member Author

moreati commented Sep 21, 2024

PoC reduces localhost ping time from 0.98 seconds to 0.66

diff --git a/mitogen/master.py b/mitogen/master.py
index b1e0a1de..cbd8a9f2 100644
--- a/mitogen/master.py
+++ b/mitogen/master.py
@@ -1048,6 +1048,7 @@ class ModuleFinder(object):
                 if sys.modules.get(name) is not None
                 and not is_stdlib_name(name)
                 and u'six.moves' not in name  # TODO: crap
+                and name != u'__main__'
             )
         ))
 

@moreati
Copy link
Member Author

moreati commented Sep 22, 2024

A code search for import __main__ shows it's not unique in Ansible, so it may need better handling than just the above PoC

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
affects-0.3 Issues related to 0.3.X Mitogen releases bug Code feature that hinders desired execution outcome importer Issues impacting core.py, parent.py, and master.py's import client and servers performance Performance problems
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant