From 88a25b7fdc2ba7317f526e6e951e4e3de1d1bf27 Mon Sep 17 00:00:00 2001 From: Matt Martz Date: Tue, 9 May 2023 14:25:54 -0500 Subject: Reconfigure stdout/stderr to replace invalid UTF-8 characters. Fixes #80258 (#80552) --- changelogs/fragments/80258-defensive-display-non-utf8.yml | 3 +++ lib/ansible/utils/display.py | 6 ++++++ 2 files changed, 9 insertions(+) create mode 100644 changelogs/fragments/80258-defensive-display-non-utf8.yml diff --git a/changelogs/fragments/80258-defensive-display-non-utf8.yml b/changelogs/fragments/80258-defensive-display-non-utf8.yml new file mode 100644 index 0000000000..b99b287a66 --- /dev/null +++ b/changelogs/fragments/80258-defensive-display-non-utf8.yml @@ -0,0 +1,3 @@ +bugfixes: +- Display - Defensively configure writing to stdout and stderr with the replace encoding error handler that will replace invalid characters + (https://github.com/ansible/ansible/issues/80258) diff --git a/lib/ansible/utils/display.py b/lib/ansible/utils/display.py index e521f2a401..7d98ad47e9 100644 --- a/lib/ansible/utils/display.py +++ b/lib/ansible/utils/display.py @@ -228,6 +228,12 @@ class Display(metaclass=Singleton): except Exception as ex: self.warning(f"failed to patch stdout/stderr for fork-safety: {ex}") + try: + sys.stdout.reconfigure(errors='replace') + sys.stderr.reconfigure(errors='replace') + except Exception as ex: + self.warning(f"failed to reconfigure stdout/stderr with the replace error handler: {ex}") + def set_queue(self, queue): """Set the _final_q on Display, so that we know to proxy display over the queue instead of directly writing to stdout/stderr from forks -- cgit v1.2.1