summaryrefslogtreecommitdiffstats
path: root/Lib/multiprocessing
diff options
context:
space:
mode:
authorVictor Stinner <vstinner@python.org>2019-12-17 17:37:26 (GMT)
committerGitHub <noreply@github.com>2019-12-17 17:37:26 (GMT)
commit9707e8e22d80ca97bf7a9812816701cecde6d226 (patch)
tree088de81a48039faca3709360325055105b9d9c56 /Lib/multiprocessing
parent630c8df5cf126594f8c1c4579c1888ca80a29d59 (diff)
downloadcpython-9707e8e22d80ca97bf7a9812816701cecde6d226.zip
cpython-9707e8e22d80ca97bf7a9812816701cecde6d226.tar.gz
cpython-9707e8e22d80ca97bf7a9812816701cecde6d226.tar.bz2
bpo-38546: multiprocessing tests stop the resource tracker (GH-17641)
Multiprocessing and concurrent.futures tests now stop the resource tracker process when tests complete. Add ResourceTracker._stop() method to multiprocessing.resource_tracker. Add _cleanup_tests() helper function to multiprocessing.util: share code between multiprocessing and concurrent.futures tests.
Diffstat (limited to 'Lib/multiprocessing')
-rw-r--r--Lib/multiprocessing/resource_tracker.py13
-rw-r--r--Lib/multiprocessing/util.py25
2 files changed, 38 insertions, 0 deletions
diff --git a/Lib/multiprocessing/resource_tracker.py b/Lib/multiprocessing/resource_tracker.py
index 61a6dd6..c9bfa9b 100644
--- a/Lib/multiprocessing/resource_tracker.py
+++ b/Lib/multiprocessing/resource_tracker.py
@@ -50,6 +50,19 @@ class ResourceTracker(object):
self._fd = None
self._pid = None
+ def _stop(self):
+ with self._lock:
+ if self._fd is None:
+ # not running
+ return
+
+ # closing the "alive" file descriptor stops main()
+ os.close(self._fd)
+ self._fd = None
+
+ os.waitpid(self._pid, 0)
+ self._pid = None
+
def getfd(self):
self.ensure_running()
return self._fd
diff --git a/Lib/multiprocessing/util.py b/Lib/multiprocessing/util.py
index 3e640b9..4bc7782 100644
--- a/Lib/multiprocessing/util.py
+++ b/Lib/multiprocessing/util.py
@@ -439,3 +439,28 @@ def close_fds(*fds):
"""Close each file descriptor given as an argument"""
for fd in fds:
os.close(fd)
+
+
+def _cleanup_tests():
+ """Cleanup multiprocessing resources when multiprocessing tests
+ completed."""
+
+ from test import support
+
+ # cleanup multiprocessing
+ process._cleanup()
+
+ # Stop the ForkServer process if it's running
+ from multiprocessing import forkserver
+ forkserver._forkserver._stop()
+
+ # Stop the ResourceTracker process if it's running
+ from multiprocessing import resource_tracker
+ resource_tracker._resource_tracker._stop()
+
+ # bpo-37421: Explicitly call _run_finalizers() to remove immediately
+ # temporary directories created by multiprocessing.util.get_temp_dir().
+ _run_finalizers()
+ support.gc_collect()
+
+ support.reap_children()