ansible_mitogen: Speedup startup by not sending `__main__`

On my laptop his reduces the time to execute `ansible -mping ...` by approx
300 ms with `strategy=mitogen_linear`.

Until this commit Mitogen was unnecessarily sending large chunks of Ansible
from the controller to targets, due to `__main__` being identified as a
related module of `ansible.module_utils.basic`, and resolving to something
within `ansible.cli...`.

On Ansible target hosts executing any Ansible Module `__main__` is imported by
`ansible.module_utils.basic` as part of Ansible's module delivery mechanism.
When `mitogen.master.ModuleResponder` (on the controller) processes the
request for `ansible.module_utils.basic` from the target, it scans
`ansible.module_utils.basic` for related imports and finds `__main__`. However
`__main__` on the controller is not the same module as `__main__` on the
target. On the controller it is a module in `ansible.cli...` that implements
one of the ansible commands (e.g. `ansible`, `ansible-playbook`).
pull/1126/head
Alex Willmer 1 week ago
parent f191f050bf
commit 83b6cdb616

@ -29,6 +29,8 @@ In progress (unreleased)
* :gh:issue:`1118` CI: Add OS release coverage: Ubuntu 22.04, Ubuntu 24.04 * :gh:issue:`1118` CI: Add OS release coverage: Ubuntu 22.04, Ubuntu 24.04
* :gh:issue:`1124` :mod:`mitogen`: Log why a module is sent or not sent by * :gh:issue:`1124` :mod:`mitogen`: Log why a module is sent or not sent by
:class:`mitogen.master.ModuleResponder` :class:`mitogen.master.ModuleResponder`
* :gh:issue:`1124` :mod:`ansible_mitogen`: Speedup startup by not sending
``__main__`` as a related module
v0.3.33 (2025-11-22) v0.3.33 (2025-11-22)

@ -843,6 +843,12 @@ class ModuleFinder(object):
related modules likely needed by a child context requesting the original related modules likely needed by a child context requesting the original
module. module.
""" """
# Fullnames of modules that should not be sent as a related module
_related_modules_denylist = frozenset({
'__main__',
})
def __init__(self): def __init__(self):
#: Import machinery is expensive, keep :py:meth`:get_module_source` #: Import machinery is expensive, keep :py:meth`:get_module_source`
#: results around. #: results around.
@ -954,6 +960,9 @@ class ModuleFinder(object):
if 'six.moves' in related_fullname: if 'six.moves' in related_fullname:
return _log_reject('six.moves avoidence') return _log_reject('six.moves avoidence')
if related_fullname in self._related_modules_denylist:
return _log_reject('on denylist')
return False return False
def find_related_imports(self, fullname): def find_related_imports(self, fullname):

Loading…
Cancel
Save