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

Mitogen fails when executing certain custom modules #1214

Open
Nihlus opened this issue Jan 9, 2025 · 1 comment
Open

Mitogen fails when executing certain custom modules #1214

Nihlus opened this issue Jan 9, 2025 · 1 comment
Labels
affects-0.3 Issues related to 0.3.X Mitogen releases bug Code feature that hinders desired execution outcome

Comments

@Nihlus
Copy link
Contributor

Nihlus commented Jan 9, 2025

I'm currently trying to use Mitogen 0.3.20 on with a number of playbooks I use. For the most part, things are working well, but certain custom modules fail when running under Mitogen. Among these are ansible_freeipa.ipaserver and ondrejhome.ha_cluster.pcs_resource. In particular, they fail with the following backtrace:

TASK [freeipa.ansible_freeipa.ipaserver : Install - Server installation test] ************************************************************************************************************************************************************************************************************************************************
Thursday 09 January 2025  20:06:33 +0100 (0:00:00.015)       0:07:38.306 ****** 
Thursday 09 January 2025  20:06:33 +0100 (0:00:00.015)       0:07:38.305 ****** 
An exception occurred during task execution. To see the full traceback, use -vvv. The error was:                                     ^^^^^^^^^^^^^^^^^^^
fatal: [auth1.intra.algiz.nu]: FAILED! => {"msg": "Unexpected failure during module execution: builtins.AttributeError: '_io.TextIOWrapper' object has no attribute 'getvalue'\n  File \"<stdin>\", line 3864, in _dispatch_one\n  File \"master:/home/jarl/Programming/system-projects/homelab-ansible/ansible_strategies/mitogen-0.3.20/ansible_mitogen/target.py\", line 402, in run_module\n    return impl.run()\n           ^^^^^^^^^^\n  File \"master:/home/jarl/Programming/system-projects/homelab-ansible/ansible_strategies/mitogen-0.3.20/ansible_mitogen/runner.py\", line 434, in run\n    return self._run()\n           ^^^^^^^^^^^\n  File \"master:/home/jarl/Programming/system-projects/homelab-ansible/ansible_strategies/mitogen-0.3.20/ansible_mitogen/runner.py\", line 1053, in _run\n    u'stdout': mitogen.core.to_text(sys.stdout.getvalue()),\n                                    ^^^^^^^^^^^^^^^^^^^\n", "stdout": ""}

As far as I can tell, Mitogen is assuming that stdout/stderr is a BytesIO or StringIO, which apparently is not true in these cases.

Originally, I suggested using read as an alternative to getvalue, which does more or less the same thing save for advancing the stream position. However, in local testing, I found that sys.stdout and sys.stderr refer to closed files when we hit the problematic line in runner.py, and I'm not familiar enough with Ansible's architecture to make any headway past that.

Ansible versions:

ansible [core 2.16.3]
  config file = None
  configured module search path = ['/home/jarl/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python3/dist-packages/ansible
  ansible collection location = /home/jarl/.ansible/collections:/usr/share/ansible/collections
  executable location = /usr/bin/ansible
  python version = 3.12.3 (main, Nov  6 2024, 18:32:19) [GCC 13.2.0] (/usr/bin/python3)
  jinja version = 3.1.2
  libyaml = True
@Nihlus
Copy link
Contributor Author

Nihlus commented Jan 11, 2025

I've dug further into this issue and found that it is caused by the problem modules reassigning sys.stdout with sys.__stdout__, which is breaking Mitogen since it overrides sys.stdout with a line-buffered variant in core.py.

sys.stdout = os.fdopen(1, 'w', 1)

I've reported both issues to their respective repositories, but a fix in Mitogen that preserves Ansible-compatible behaviour would be nice.

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
Projects
None yet
Development

No branches or pull requests

1 participant