From 83b6cdb616ad838f431a7cac94717f6adb4c4698 Mon Sep 17 00:00:00 2001 From: Alex Willmer Date: Wed, 26 Nov 2025 22:16:31 +0000 Subject: [PATCH] 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`). --- docs/changelog.rst | 2 ++ mitogen/master.py | 9 +++++++++ 2 files changed, 11 insertions(+) diff --git a/docs/changelog.rst b/docs/changelog.rst index a0d27c1c..1824f6f9 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -29,6 +29,8 @@ In progress (unreleased) * :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 :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) diff --git a/mitogen/master.py b/mitogen/master.py index 4de32586..e495197d 100644 --- a/mitogen/master.py +++ b/mitogen/master.py @@ -843,6 +843,12 @@ class ModuleFinder(object): related modules likely needed by a child context requesting the original module. """ + + # Fullnames of modules that should not be sent as a related module + _related_modules_denylist = frozenset({ + '__main__', + }) + def __init__(self): #: Import machinery is expensive, keep :py:meth`:get_module_source` #: results around. @@ -954,6 +960,9 @@ class ModuleFinder(object): if 'six.moves' in related_fullname: return _log_reject('six.moves avoidence') + if related_fullname in self._related_modules_denylist: + return _log_reject('on denylist') + return False def find_related_imports(self, fullname):