[Python-checkins] bpo-37421: multiprocessing tests now stop ForkServer (GH-14601)

Miss Islington (bot) webhook-mailer at python.org
Fri Jul 5 10:35:43 EDT 2019


https://github.com/python/cpython/commit/229f6e85f8b4d57a2e742e0d3fc361c5bd15f1cb
commit: 229f6e85f8b4d57a2e742e0d3fc361c5bd15f1cb
branch: 3.8
author: Miss Islington (bot) <31488909+miss-islington at users.noreply.github.com>
committer: GitHub <noreply at github.com>
date: 2019-07-05T07:35:38-07:00
summary:

bpo-37421: multiprocessing tests now stop ForkServer (GH-14601)


multiprocessing tests now stop the ForkServer instance if it's
running: close the "alive" file descriptor to ask the server to stop
and then remove its UNIX address.
(cherry picked from commit 8fbeb14312b4c1320d31ad86e69749515879d1c3)

Co-authored-by: Victor Stinner <vstinner at redhat.com>

files:
A Misc/NEWS.d/next/Tests/2019-07-05-14-47-55.bpo-37421.n8o2to.rst
M Lib/multiprocessing/forkserver.py
M Lib/test/_test_multiprocessing.py

diff --git a/Lib/multiprocessing/forkserver.py b/Lib/multiprocessing/forkserver.py
index 9b6398671dbc..87ebef6588ac 100644
--- a/Lib/multiprocessing/forkserver.py
+++ b/Lib/multiprocessing/forkserver.py
@@ -39,6 +39,25 @@ def __init__(self):
         self._lock = threading.Lock()
         self._preload_modules = ['__main__']
 
+    def _stop(self):
+        # Method used by unit tests to stop the server
+        with self._lock:
+            self._stop_unlocked()
+
+    def _stop_unlocked(self):
+        if self._forkserver_pid is None:
+            return
+
+        # close the "alive" file descriptor asks the server to stop
+        os.close(self._forkserver_alive_fd)
+        self._forkserver_alive_fd = None
+
+        os.waitpid(self._forkserver_pid, 0)
+        self._forkserver_pid = None
+
+        os.unlink(self._forkserver_address)
+        self._forkserver_address = None
+
     def set_forkserver_preload(self, modules_names):
         '''Set list of module names to try to load in forkserver process.'''
         if not all(type(mod) is str for mod in self._preload_modules):
diff --git a/Lib/test/_test_multiprocessing.py b/Lib/test/_test_multiprocessing.py
index af5b91d17e75..9a39f385952f 100644
--- a/Lib/test/_test_multiprocessing.py
+++ b/Lib/test/_test_multiprocessing.py
@@ -5650,7 +5650,13 @@ def tearDownModule():
         # Sleep 500 ms to give time to child processes to complete.
         if need_sleep:
             time.sleep(0.5)
+
         multiprocessing.process._cleanup()
+
+        # Stop the ForkServer process if it's running
+        from multiprocessing import forkserver
+        forkserver._forkserver._stop()
+
         # bpo-37421: Explicitly call _run_finalizers() to remove immediately
         # temporary directories created by multiprocessing.util.get_temp_dir().
         multiprocessing.util._run_finalizers()
diff --git a/Misc/NEWS.d/next/Tests/2019-07-05-14-47-55.bpo-37421.n8o2to.rst b/Misc/NEWS.d/next/Tests/2019-07-05-14-47-55.bpo-37421.n8o2to.rst
new file mode 100644
index 000000000000..136faa22d47a
--- /dev/null
+++ b/Misc/NEWS.d/next/Tests/2019-07-05-14-47-55.bpo-37421.n8o2to.rst
@@ -0,0 +1,3 @@
+multiprocessing tests now stop the ForkServer instance if it's running: close
+the "alive" file descriptor to ask the server to stop and then remove its UNIX
+address.



More information about the Python-checkins mailing list