Implement workaround for stdout deadlock in multiprocessing shutdown (#74099)

This commit is contained in:
Matt Martz 2021-04-05 08:51:12 -05:00 committed by GitHub
parent 4e2da0f894
commit 33185c29ab
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 16 additions and 0 deletions

View file

@ -0,0 +1,3 @@
bugfixes:
- WorkerProcess - Implement workaround for stdout deadlock in multiprocessing shutdown
to avoid process hangs.

View file

@ -134,6 +134,19 @@ class WorkerProcess(multiprocessing_context.Process):
return self._run()
except BaseException as e:
self._hard_exit(e)
finally:
# This is a hack, pure and simple, to work around a potential deadlock
# in ``multiprocessing.Process`` when flushing stdout/stderr during process
# shutdown. We have various ``Display`` calls that may fire from a fork
# so we cannot do this early. Instead, this happens at the very end
# to avoid that deadlock, by simply side stepping it. This should not be
# treated as a long term fix. Additionally this behavior only presents itself
# on Python3. Python2 does not exhibit the deadlock behavior.
# TODO: Evaluate overhauling ``Display`` to not write directly to stdout
# and evaluate migrating away from the ``fork`` multiprocessing start method.
if sys.version_info[0] >= 3:
sys.stdout = os.devnull
sys.stderr = os.devnull
def _run(self):
'''