]> granicus.if.org Git - python/commitdiff
[3.6] bpo-31804: Fix multiprocessing.Process with broken standard streams (GH-6079...
authorAntoine Pitrou <pitrou@free.fr>
Sun, 11 Mar 2018 19:09:20 +0000 (20:09 +0100)
committerGitHub <noreply@github.com>
Sun, 11 Mar 2018 19:09:20 +0000 (20:09 +0100)
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..
(cherry picked from commit e756f66c83786ee82f5f7d45931ae50a6931dd7f)

Lib/multiprocessing/popen_fork.py
Lib/multiprocessing/process.py
Lib/multiprocessing/util.py
Lib/test/_test_multiprocessing.py
Misc/NEWS.d/next/Library/2018-03-11-19-03-52.bpo-31804.i8KUMp.rst [new file with mode: 0644]

index 5d0fa569f12eaaa3e9789539fdea820229f90f8e..6396db45b136b694b32863521c9c58a03efe4b0c 100644 (file)
@@ -14,14 +14,7 @@ class Popen(object):
     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._launch(process_obj)
 
index 1d26b5e521e7e7ceef71d62dd1229c685826ff7d..c6157b6046a0a6a6fcf4d1e0d6f7c73f41fcc330 100644 (file)
@@ -274,8 +274,7 @@ class BaseProcess(object):
             traceback.print_exc()
         finally:
             util.info('process exiting with exitcode %d' % exitcode)
-            sys.stdout.flush()
-            sys.stderr.flush()
+            util._flush_std_streams()
 
         return exitcode
 
index b490caa7e64333955ba1bb26058f5d2bb6697a0b..24573764fab16a3dd2d9fa8bad331ed6bc63f4fd 100644 (file)
@@ -388,6 +388,20 @@ def _close_stdin():
     except (OSError, ValueError):
         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
 #
index e4d60f8804269d4614df14e2cde863a5e4819454..dd0a9d7a862a466f1348f6c84fee4b7d4dd5e8f0 100644 (file)
@@ -427,10 +427,19 @@ class _TestProcess(BaseTestCase):
         close_queue(q)
 
     @classmethod
-    def _test_error_on_stdio_flush(self, evt):
+    def _test_error_on_stdio_flush(self, evt, break_std_streams={}):
+        for stream_name, action in break_std_streams.items():
+            if action == 'close':
+                stream = io.StringIO()
+                stream.close()
+            else:
+                assert action == 'remove'
+                stream = None
+            setattr(sys, stream_name, None)
         evt.set()
 
-    def test_error_on_stdio_flush(self):
+    def test_error_on_stdio_flush_1(self):
+        # Check that Process works with broken standard streams
         streams = [io.StringIO(), None]
         streams[0].close()
         for stream_name in ('stdout', 'stderr'):
@@ -444,6 +453,24 @@ class _TestProcess(BaseTestCase):
                     proc.start()
                     proc.join()
                     self.assertTrue(evt.is_set())
+                    self.assertEqual(proc.exitcode, 0)
+                finally:
+                    setattr(sys, stream_name, old_stream)
+
+    def test_error_on_stdio_flush_2(self):
+        # Same as test_error_on_stdio_flush_1(), but standard streams are
+        # broken by the child process
+        for stream_name in ('stdout', 'stderr'):
+            for action in ('close', 'remove'):
+                old_stream = getattr(sys, stream_name)
+                try:
+                    evt = self.Event()
+                    proc = self.Process(target=self._test_error_on_stdio_flush,
+                                        args=(evt, {stream_name: action}))
+                    proc.start()
+                    proc.join()
+                    self.assertTrue(evt.is_set())
+                    self.assertEqual(proc.exitcode, 0)
                 finally:
                     setattr(sys, stream_name, old_stream)
 
diff --git a/Misc/NEWS.d/next/Library/2018-03-11-19-03-52.bpo-31804.i8KUMp.rst b/Misc/NEWS.d/next/Library/2018-03-11-19-03-52.bpo-31804.i8KUMp.rst
new file mode 100644 (file)
index 0000000..7fcede2
--- /dev/null
@@ -0,0 +1,2 @@
+Avoid failing in multiprocessing.Process if the standard streams are closed
+or None at exit.