summaryrefslogtreecommitdiff
path: root/Lib/multiprocessing
diff options
context:
space:
mode:
authorAntoine Pitrou <pitrou@free.fr>2017-11-03 13:34:22 +0100
committerGitHub <noreply@github.com>2017-11-03 13:34:22 +0100
commitfc6b348b12ad401cab0261b7b71a65c60a08c0a8 (patch)
tree13920c1484d6d65b19a2e07101ce7416bc10db77 /Lib/multiprocessing
parent4f57409a2f7bdf8fb559cddc7c6533ca2c471c67 (diff)
downloadcpython-git-fc6b348b12ad401cab0261b7b71a65c60a08c0a8.tar.gz
bpo-31308: If multiprocessing's forkserver dies, launch it again when necessary (#3246)
* bpo-31308: If multiprocessing's forkserver dies, launch it again when necessary. * Fix test on Windows * Add NEWS entry * Adopt a different approach: ignore SIGINT and SIGTERM, as in semaphore tracker. * Fix comment * Make sure the test doesn't muck with process state * Also test previously-started processes * Update 2017-08-30-17-59-36.bpo-31308.KbexyC.rst * Avoid masking SIGTERM in forkserver. It's not necessary and causes a race condition in test_many_processes.
Diffstat (limited to 'Lib/multiprocessing')
-rw-r--r--Lib/multiprocessing/forkserver.py21
1 files changed, 16 insertions, 5 deletions
diff --git a/Lib/multiprocessing/forkserver.py b/Lib/multiprocessing/forkserver.py
index 7a952e2ba6..040b46e66a 100644
--- a/Lib/multiprocessing/forkserver.py
+++ b/Lib/multiprocessing/forkserver.py
@@ -34,6 +34,7 @@ class ForkServer(object):
def __init__(self):
self._forkserver_address = None
self._forkserver_alive_fd = None
+ self._forkserver_pid = None
self._inherited_fds = None
self._lock = threading.Lock()
self._preload_modules = ['__main__']
@@ -90,8 +91,17 @@ class ForkServer(object):
'''
with self._lock:
semaphore_tracker.ensure_running()
- if self._forkserver_alive_fd is not None:
- return
+ if self._forkserver_pid is not None:
+ # forkserver was launched before, is it still running?
+ pid, status = os.waitpid(self._forkserver_pid, os.WNOHANG)
+ if not pid:
+ # still alive
+ return
+ # dead, launch it again
+ os.close(self._forkserver_alive_fd)
+ self._forkserver_address = None
+ self._forkserver_alive_fd = None
+ self._forkserver_pid = None
cmd = ('from multiprocessing.forkserver import main; ' +
'main(%d, %d, %r, **%r)')
@@ -127,6 +137,7 @@ class ForkServer(object):
os.close(alive_r)
self._forkserver_address = address
self._forkserver_alive_fd = alive_w
+ self._forkserver_pid = pid
#
#
@@ -157,11 +168,11 @@ def main(listener_fd, alive_r, preload, main_path=None, sys_path=None):
# Dummy signal handler, doesn't do anything
pass
- # letting SIGINT through avoids KeyboardInterrupt tracebacks
- # unblocking SIGCHLD allows the wakeup fd to notify our event loop
handlers = {
+ # unblocking SIGCHLD allows the wakeup fd to notify our event loop
signal.SIGCHLD: sigchld_handler,
- signal.SIGINT: signal.SIG_DFL,
+ # protect the process from ^C
+ signal.SIGINT: signal.SIG_IGN,
}
old_handlers = {sig: signal.signal(sig, val)
for (sig, val) in handlers.items()}