Fix fallback to devnull when trying to preserve stdin in worker (#74192)

ci_complete
This commit is contained in:
Martin Krizek 2021-04-15 10:14:48 +02:00 committed by GitHub
parent ee38202fc0
commit 3cbe16fa7a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -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,7 +101,6 @@ class WorkerProcess(multiprocessing_context.Process):
try:
return super(WorkerProcess, self).start()
finally:
if self._new_stdin != os.devnull:
self._new_stdin.close()
def _hard_exit(self, e):