summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMatt Martz <matt@sivel.net>2023-05-09 14:25:54 -0500
committerGitHub <noreply@github.com>2023-05-09 14:25:54 -0500
commit88a25b7fdc2ba7317f526e6e951e4e3de1d1bf27 (patch)
treebb43624ec15ee04ae14596c5d63d3d2483b4c0be
parent0e58877f35f9fa61365516513a800c6797b9b0cb (diff)
downloadansible-88a25b7fdc2ba7317f526e6e951e4e3de1d1bf27.tar.gz
Reconfigure stdout/stderr to replace invalid UTF-8 characters. Fixes #80258 (#80552)
-rw-r--r--changelogs/fragments/80258-defensive-display-non-utf8.yml3
-rw-r--r--lib/ansible/utils/display.py6
2 files changed, 9 insertions, 0 deletions
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