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() self._loader._tempfiles = set()
def _save_stdin(self): def _save_stdin(self):
self._new_stdin = os.devnull self._new_stdin = None
try: try:
if sys.stdin.isatty() and sys.stdin.fileno() is not None: if sys.stdin.isatty() and sys.stdin.fileno() is not None:
try: try:
self._new_stdin = os.fdopen(os.dup(sys.stdin.fileno())) self._new_stdin = os.fdopen(os.dup(sys.stdin.fileno()))
except OSError: except OSError:
# couldn't dupe stdin, most likely because it's # couldn't dupe stdin, most likely because it's
# not a valid file descriptor, so we just rely on # not a valid file descriptor
# using the one that was passed in
pass pass
except (AttributeError, ValueError): except (AttributeError, ValueError):
# couldn't get stdin's fileno, so we just carry on # couldn't get stdin's fileno
pass pass
if self._new_stdin is None:
self._new_stdin = open(os.devnull)
def start(self): def start(self):
''' '''
multiprocessing.Process replaces the worker's stdin with a new file 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 but we wish to preserve it if it is connected to a terminal.
a terminal. Therefore dup a copy prior to calling the real start(), Therefore dup a copy prior to calling the real start(),
ensuring the descriptor is preserved somewhere in the new child, and ensuring the descriptor is preserved somewhere in the new child, and
make sure it is closed in the parent when start() completes. make sure it is closed in the parent when start() completes.
''' '''
@ -99,8 +101,7 @@ class WorkerProcess(multiprocessing_context.Process):
try: try:
return super(WorkerProcess, self).start() return super(WorkerProcess, self).start()
finally: finally:
if self._new_stdin != os.devnull: self._new_stdin.close()
self._new_stdin.close()
def _hard_exit(self, e): def _hard_exit(self, e):
''' '''