10000 bpo-38546: multiprocessing tests stop the resource tracker by vstinner · Pull Request #17641 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

bpo-38546: multiprocessing tests stop the resource tracker #17641

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 17, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions Lib/multiprocessing/resource_tracker.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,19 @@ def __init__(self):
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
Expand Down
25 changes: 25 additions & 0 deletions Lib/multiprocessing/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
11 changes: 1 addition & 10 deletions Lib/test/_test_multiprocessing.py
Original file line number Diff line number Diff line change
Expand Up @@ -5695,16 +5695,7 @@ def tearDownModule():
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()
test.support.gc_collect()
multiprocessing.util._cleanup_tests()

remote_globs['setUpModule'] = setUpModule
remote_globs['tearDownModule'] = tearDownModule
12 changes: 1 addition & 11 deletions Lib/test/test_concurrent_futures.py
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -1306,17 +1306,7 @@ def setUpModule():

def tearDownModule():
support.threading_cleanup(*_threads_key)
support.reap_children()

# cleanup multiprocessing
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()
support.gc_collect()
multiprocessing.util._cleanup_tests()


if __name__ == "__main__":
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Multiprocessing and concurrent.futures tests now stop the resource tracker
process when tests complete.
0