Issue #13812: When a multiprocessing Process child raises an exception, flush stderr after printing the exception traceback.
diff --git a/Lib/multiprocessing/forking.py b/Lib/multiprocessing/forking.py
index cc7c326..4e24d6a 100644
--- a/Lib/multiprocessing/forking.py
+++ b/Lib/multiprocessing/forking.py
@@ -124,8 +124,6 @@
                     import random
                     random.seed()
                 code = process_obj._bootstrap()
-                sys.stdout.flush()
-                sys.stderr.flush()
                 os._exit(code)
 
         def poll(self, flag=os.WNOHANG):
diff --git a/Lib/multiprocessing/process.py b/Lib/multiprocessing/process.py
index 5987af9..2b61ee9 100644
--- a/Lib/multiprocessing/process.py
+++ b/Lib/multiprocessing/process.py
@@ -275,16 +275,17 @@
                 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
 
 #
diff --git a/Lib/test/test_multiprocessing.py b/Lib/test/test_multiprocessing.py
index de894f3..8edb420 100644
--- a/Lib/test/test_multiprocessing.py
+++ b/Lib/test/test_multiprocessing.py
@@ -367,6 +367,29 @@
         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
+
+
 #
 #
 #