From 3cbe16fa7ab05c67e6a32312fe199347b604a701 Mon Sep 17 00:00:00 2001 From: Martin Krizek Date: Thu, 15 Apr 2021 10:14:48 +0200 Subject: [PATCH] Fix fallback to devnull when trying to preserve stdin in worker (#74192) ci_complete --- lib/ansible/executor/process/worker.py | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/lib/ansible/executor/process/worker.py b/lib/ansible/executor/process/worker.py index 510928c0edc..c8947bfbf5c 100644 --- a/lib/ansible/executor/process/worker.py +++ b/lib/ansible/executor/process/worker.py @@ -72,25 +72,27 @@ class WorkerProcess(multiprocessing_context.Process): self._loader._tempfiles = set() def _save_stdin(self): - self._new_stdin = os.devnull + self._new_stdin = None try: if sys.stdin.isatty() and sys.stdin.fileno() is not None: try: self._new_stdin = os.fdopen(os.dup(sys.stdin.fileno())) except OSError: # couldn't dupe stdin, most likely because it's - # not a valid file descriptor, so we just rely on - # using the one that was passed in + # not a valid file descriptor pass except (AttributeError, ValueError): - # couldn't get stdin's fileno, so we just carry on + # couldn't get stdin's fileno pass + if self._new_stdin is None: + self._new_stdin = open(os.devnull) + def start(self): ''' multiprocessing.Process replaces the worker's stdin with a new file - opened on os.devnull, but we wish to preserve it if it is connected to - a terminal. Therefore dup a copy prior to calling the real start(), + but we wish to preserve it if it is connected to a terminal. + Therefore dup a copy prior to calling the real start(), ensuring the descriptor is preserved somewhere in the new child, and make sure it is closed in the parent when start() completes. ''' @@ -99,8 +101,7 @@ class WorkerProcess(multiprocessing_context.Process): try: return super(WorkerProcess, self).start() finally: - if self._new_stdin != os.devnull: - self._new_stdin.close() + self._new_stdin.close() def _hard_exit(self, e): '''