]> granicus.if.org Git - python/commitdiff
Issue #13812: When a multiprocessing Process child raises an exception, flush stderr...
authorAntoine Pitrou <solipsis@pitrou.net>
Fri, 27 Jan 2012 09:52:37 +0000 (10:52 +0100)
committerAntoine Pitrou <solipsis@pitrou.net>
Fri, 27 Jan 2012 09:52:37 +0000 (10:52 +0100)
Lib/multiprocessing/forking.py
Lib/multiprocessing/process.py
Lib/test/test_multiprocessing.py
Misc/NEWS

index cc7c326c077ec8594f28f8e29fcbeaad80bb709b..4e24d6a10fa28dcfde4e9f9e0040108be8940d14 100644 (file)
@@ -124,8 +124,6 @@ if sys.platform != 'win32':
                     import random
                     random.seed()
                 code = process_obj._bootstrap()
-                sys.stdout.flush()
-                sys.stderr.flush()
                 os._exit(code)
 
         def poll(self, flag=os.WNOHANG):
index 5987af9322c1504c8aa36264a83c5be35b5e050e..2b61ee9a0d39a2d154975f2a48f16ae5a2da80f3 100644 (file)
@@ -275,16 +275,17 @@ class Process(object):
                 exitcode = e.args[0]
             else:
                 sys.stderr.write(e.args[0] + '\n')
-                sys.stderr.flush()
                 exitcode = 1
         except:
             exitcode = 1
             import traceback
             sys.stderr.write('Process %s:\n' % self.name)
-            sys.stderr.flush()
             traceback.print_exc()
+        finally:
+            util.info('process exiting with exitcode %d' % exitcode)
+            sys.stdout.flush()
+            sys.stderr.flush()
 
-        util.info('process exiting with exitcode %d' % exitcode)
         return exitcode
 
 #
index de894f3087b254bcf6405d75bb4303e4216ba888..8edb4208640cb065a8cc366963c9c1835a40baeb 100644 (file)
@@ -367,6 +367,29 @@ class _TestSubclassingProcess(BaseTestCase):
         uppercaser.stop()
         uppercaser.join()
 
+    def test_stderr_flush(self):
+        # sys.stderr is flushed at process shutdown (issue #13812)
+        if self.TYPE == "threads":
+            return
+
+        testfn = test.support.TESTFN
+        self.addCleanup(test.support.unlink, testfn)
+        proc = self.Process(target=self._test_stderr_flush, args=(testfn,))
+        proc.start()
+        proc.join()
+        with open(testfn, 'r') as f:
+            err = f.read()
+            # The whole traceback was printed
+            self.assertIn("ZeroDivisionError", err)
+            self.assertIn("test_multiprocessing.py", err)
+            self.assertIn("1/0 # MARKER", err)
+
+    @classmethod
+    def _test_stderr_flush(cls, testfn):
+        sys.stderr = open(testfn, 'w')
+        1/0 # MARKER
+
+
 #
 #
 #
index 8a85fe440943407feb5c6f694dd1b553d4530005..06cefe2642e0c0998a129acf8079fcfb28d79edc 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -111,6 +111,9 @@ Core and Builtins
 Library
 -------
 
+- Issue #13812: When a multiprocessing Process child raises an exception,
+  flush stderr after printing the exception traceback.
+
 - Issue #13885: CVE-2011-3389: the _ssl module would always disable the CBC
   IV attack countermeasure.