Cache sys.stdout instead of assuming it is equal to sys.__stdout__

When running under Mitogen, ipa_server and ipa_replica breaks execution
by overwriting sys.stdout with sys.stdout.

With Mitogen, sys.stdout != sys.stdout at this point in the code, and
changing it in this manner results in access to closed file descriptors
for future invocations. Generally, it is recommended not to use
sys.stdout and instead explicitly cache the current value of sys.stdout.
This commit is contained in:
Jarl Gullberg
2025-01-11 20:32:27 +01:00
parent 9195494f37
commit 902d8b7238
2 changed files with 6 additions and 2 deletions

View File

@@ -208,11 +208,13 @@ def setup_logging():
@contextlib_contextmanager
def redirect_stdout(stream):
old_stdout = sys.stdout
sys.stdout = stream
try:
yield stream
finally:
sys.stdout = sys.__stdout__
sys.stdout = old_stdout
class AnsibleModuleLog():

View File

@@ -241,11 +241,13 @@ def setup_logging():
@contextlib_contextmanager
def redirect_stdout(stream):
old_stdout = sys.stdout
sys.stdout = stream
try:
yield stream
finally:
sys.stdout = sys.__stdout__
sys.stdout = old_stdout
class AnsibleModuleLog():