Anonymous avatar Anonymous committed 96c1de5 Merge

Issue #13812: When a multiprocessing Process child raises an exception, flush stderr after printing the exception traceback.

Comments (0)

Files changed (4)

Lib/multiprocessing/forking.py

                     import random
                     random.seed()
                 code = process_obj._bootstrap()
-                sys.stdout.flush()
-                sys.stderr.flush()
                 os._exit(code)
 
             # `w` will be closed when the child exits, at which point `r`

Lib/multiprocessing/process.py

                 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)
+            traceback.print_exc()
+        finally:
+            util.info('process exiting with exitcode %d' % exitcode)
+            sys.stdout.flush()
             sys.stderr.flush()
-            traceback.print_exc()
 
-        util.info('process exiting with exitcode %d' % exitcode)
         return exitcode
 
 #

Lib/test/test_multiprocessing.py

         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
+
+
 #
 #
 #
 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.
 
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.