bpo-31804: Fix multiprocessing.Process with broken standard streams (#6079)
In some conditions the standard streams will be None or closed in the child process (for example if using "pythonw" instead of "python" on Windows). Avoid failing with a non-0 exit code in those conditions.
Report and initial patch by poxthegreat.
diff --git a/Lib/multiprocessing/popen_fork.py b/Lib/multiprocessing/popen_fork.py
index b0fc013..008b97b 100644
--- a/Lib/multiprocessing/popen_fork.py
+++ b/Lib/multiprocessing/popen_fork.py
@@ -14,14 +14,7 @@
method = 'fork'
def __init__(self, process_obj):
- try:
- sys.stdout.flush()
- except (AttributeError, ValueError):
- pass
- try:
- sys.stderr.flush()
- except (AttributeError, ValueError):
- pass
+ util._flush_std_streams()
self.returncode = None
self.finalizer = None
self._launch(process_obj)
diff --git a/Lib/multiprocessing/process.py b/Lib/multiprocessing/process.py
index 8fff3e1..cd592d0 100644
--- a/Lib/multiprocessing/process.py
+++ b/Lib/multiprocessing/process.py
@@ -314,8 +314,7 @@
finally:
threading._shutdown()
util.info('process exiting with exitcode %d' % exitcode)
- sys.stdout.flush()
- sys.stderr.flush()
+ util._flush_std_streams()
return exitcode
diff --git a/Lib/multiprocessing/util.py b/Lib/multiprocessing/util.py
index f0827f0..0c4eb24 100644
--- a/Lib/multiprocessing/util.py
+++ b/Lib/multiprocessing/util.py
@@ -392,6 +392,20 @@
pass
#
+# Flush standard streams, if any
+#
+
+def _flush_std_streams():
+ try:
+ sys.stdout.flush()
+ except (AttributeError, ValueError):
+ pass
+ try:
+ sys.stderr.flush()
+ except (AttributeError, ValueError):
+ pass
+
+#
# Start a program with only specified fds kept open
#