2019-05-10 17:59:08 -03:00
|
|
|
###############################################################################
|
|
|
|
# Server process to keep track of unlinked resources (like shared memory
|
|
|
|
# segments, semaphores etc.) and clean them.
|
2013-08-14 11:35:41 -03:00
|
|
|
#
|
|
|
|
# On Unix we run a server process which keeps track of unlinked
|
2019-05-10 17:59:08 -03:00
|
|
|
# resources. The server ignores SIGINT and SIGTERM and reads from a
|
2013-08-14 11:35:41 -03:00
|
|
|
# pipe. Every other process of the program has a copy of the writable
|
|
|
|
# end of the pipe, so we get EOF when all other processes have exited.
|
2019-05-10 17:59:08 -03:00
|
|
|
# Then the server process unlinks any remaining resource names.
|
2013-08-14 11:35:41 -03:00
|
|
|
#
|
2019-05-10 17:59:08 -03:00
|
|
|
# This is important because there may be system limits for such resources: for
|
|
|
|
# instance, the system only supports a limited number of named semaphores, and
|
|
|
|
# shared-memory segments live in the RAM. If a python process leaks such a
|
|
|
|
# resource, this resource will not be removed till the next reboot. Without
|
|
|
|
# this resource tracker process, "killall python" would probably leave unlinked
|
|
|
|
# resources.
|
2013-08-14 11:35:41 -03:00
|
|
|
|
|
|
|
import os
|
|
|
|
import signal
|
|
|
|
import sys
|
|
|
|
import threading
|
|
|
|
import warnings
|
|
|
|
|
|
|
|
from . import spawn
|
|
|
|
from . import util
|
|
|
|
|
|
|
|
__all__ = ['ensure_running', 'register', 'unregister']
|
|
|
|
|
2018-09-04 05:53:54 -03:00
|
|
|
_HAVE_SIGMASK = hasattr(signal, 'pthread_sigmask')
|
|
|
|
_IGNORED_SIGNALS = (signal.SIGINT, signal.SIGTERM)
|
|
|
|
|
2019-05-10 17:59:08 -03:00
|
|
|
_CLEANUP_FUNCS = {
|
|
|
|
'noop': lambda: None,
|
|
|
|
}
|
|
|
|
|
2019-05-13 15:02:46 -03:00
|
|
|
if os.name == 'posix':
|
|
|
|
import _multiprocessing
|
|
|
|
import _posixshmem
|
|
|
|
|
2021-02-07 23:15:51 -04:00
|
|
|
# Use sem_unlink() to clean up named semaphores.
|
|
|
|
#
|
|
|
|
# sem_unlink() may be missing if the Python build process detected the
|
|
|
|
# absence of POSIX named semaphores. In that case, no named semaphores were
|
|
|
|
# ever opened, so no cleanup would be necessary.
|
|
|
|
if hasattr(_multiprocessing, 'sem_unlink'):
|
|
|
|
_CLEANUP_FUNCS.update({
|
|
|
|
'semaphore': _multiprocessing.sem_unlink,
|
|
|
|
})
|
2019-05-13 15:02:46 -03:00
|
|
|
_CLEANUP_FUNCS.update({
|
|
|
|
'shared_memory': _posixshmem.shm_unlink,
|
|
|
|
})
|
|
|
|
|
2013-08-14 11:35:41 -03:00
|
|
|
|
2019-05-10 17:59:08 -03:00
|
|
|
class ResourceTracker(object):
|
2013-10-16 12:41:56 -03:00
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
self._lock = threading.Lock()
|
|
|
|
self._fd = None
|
2017-11-03 10:31:38 -03:00
|
|
|
self._pid = None
|
2013-10-16 12:41:56 -03:00
|
|
|
|
2019-12-17 13:37:26 -04:00
|
|
|
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
|
|
|
|
|
2013-10-16 12:41:56 -03:00
|
|
|
def getfd(self):
|
|
|
|
self.ensure_running()
|
|
|
|
return self._fd
|
|
|
|
|
|
|
|
def ensure_running(self):
|
2019-05-10 17:59:08 -03:00
|
|
|
'''Make sure that resource tracker process is running.
|
2013-10-16 12:41:56 -03:00
|
|
|
|
|
|
|
This can be run from any process. Usually a child process will use
|
2019-05-10 17:59:08 -03:00
|
|
|
the resource created by its parent.'''
|
2013-10-16 12:41:56 -03:00
|
|
|
with self._lock:
|
2019-04-24 16:45:52 -03:00
|
|
|
if self._fd is not None:
|
2019-05-10 17:59:08 -03:00
|
|
|
# resource tracker was launched before, is it still running?
|
2019-04-24 16:45:52 -03:00
|
|
|
if self._check_alive():
|
|
|
|
# => still alive
|
|
|
|
return
|
|
|
|
# => dead, launch it again
|
|
|
|
os.close(self._fd)
|
|
|
|
|
|
|
|
# Clean-up to avoid dangling processes.
|
2018-09-04 05:53:54 -03:00
|
|
|
try:
|
2019-04-24 16:45:52 -03:00
|
|
|
# _pid can be None if this process is a child from another
|
2019-05-10 17:59:08 -03:00
|
|
|
# python process, which has started the resource_tracker.
|
2019-04-24 16:45:52 -03:00
|
|
|
if self._pid is not None:
|
|
|
|
os.waitpid(self._pid, 0)
|
2018-09-04 05:53:54 -03:00
|
|
|
except ChildProcessError:
|
2019-05-10 17:59:08 -03:00
|
|
|
# The resource_tracker has already been terminated.
|
2018-09-04 05:53:54 -03:00
|
|
|
pass
|
2017-11-03 10:31:38 -03:00
|
|
|
self._fd = None
|
|
|
|
self._pid = None
|
|
|
|
|
2019-05-10 17:59:08 -03:00
|
|
|
warnings.warn('resource_tracker: process died unexpectedly, '
|
|
|
|
'relaunching. Some resources might leak.')
|
2017-11-03 10:31:38 -03:00
|
|
|
|
2013-10-16 12:41:56 -03:00
|
|
|
fds_to_pass = []
|
|
|
|
try:
|
|
|
|
fds_to_pass.append(sys.stderr.fileno())
|
|
|
|
except Exception:
|
|
|
|
pass
|
2019-05-10 17:59:08 -03:00
|
|
|
cmd = 'from multiprocessing.resource_tracker import main;main(%d)'
|
2013-10-16 12:41:56 -03:00
|
|
|
r, w = os.pipe()
|
|
|
|
try:
|
|
|
|
fds_to_pass.append(r)
|
|
|
|
# process will out live us, so no need to wait on pid
|
|
|
|
exe = spawn.get_executable()
|
|
|
|
args = [exe] + util._args_from_interpreter_flags()
|
|
|
|
args += ['-c', cmd % r]
|
2018-09-04 05:53:54 -03:00
|
|
|
# bpo-33613: Register a signal mask that will block the signals.
|
|
|
|
# This signal mask will be inherited by the child that is going
|
|
|
|
# to be spawned and will protect the child from a race condition
|
|
|
|
# that can make the child die before it registers signal handlers
|
|
|
|
# for SIGINT and SIGTERM. The mask is unregistered after spawning
|
|
|
|
# the child.
|
|
|
|
try:
|
|
|
|
if _HAVE_SIGMASK:
|
|
|
|
signal.pthread_sigmask(signal.SIG_BLOCK, _IGNORED_SIGNALS)
|
|
|
|
pid = util.spawnv_passfds(exe, args, fds_to_pass)
|
|
|
|
finally:
|
|
|
|
if _HAVE_SIGMASK:
|
|
|
|
signal.pthread_sigmask(signal.SIG_UNBLOCK, _IGNORED_SIGNALS)
|
2013-10-16 12:41:56 -03:00
|
|
|
except:
|
|
|
|
os.close(w)
|
|
|
|
raise
|
|
|
|
else:
|
|
|
|
self._fd = w
|
2017-11-03 10:31:38 -03:00
|
|
|
self._pid = pid
|
2013-10-16 12:41:56 -03:00
|
|
|
finally:
|
|
|
|
os.close(r)
|
|
|
|
|
2019-04-24 16:45:52 -03:00
|
|
|
def _check_alive(self):
|
|
|
|
'''Check that the pipe has not been closed by sending a probe.'''
|
|
|
|
try:
|
|
|
|
# We cannot use send here as it calls ensure_running, creating
|
|
|
|
# a cycle.
|
2019-05-10 17:59:08 -03:00
|
|
|
os.write(self._fd, b'PROBE:0:noop\n')
|
2019-04-24 16:45:52 -03:00
|
|
|
except OSError:
|
|
|
|
return False
|
|
|
|
else:
|
|
|
|
return True
|
|
|
|
|
2019-05-10 17:59:08 -03:00
|
|
|
def register(self, name, rtype):
|
|
|
|
'''Register name of resource with resource tracker.'''
|
|
|
|
self._send('REGISTER', name, rtype)
|
2013-10-16 12:41:56 -03:00
|
|
|
|
2019-05-10 17:59:08 -03:00
|
|
|
def unregister(self, name, rtype):
|
|
|
|
'''Unregister name of resource with resource tracker.'''
|
|
|
|
self._send('UNREGISTER', name, rtype)
|
2013-10-16 12:41:56 -03:00
|
|
|
|
2019-05-10 17:59:08 -03:00
|
|
|
def _send(self, cmd, name, rtype):
|
2013-10-16 12:41:56 -03:00
|
|
|
self.ensure_running()
|
2019-05-10 17:59:08 -03:00
|
|
|
msg = '{0}:{1}:{2}\n'.format(cmd, name, rtype).encode('ascii')
|
2022-10-02 21:41:01 -03:00
|
|
|
if len(msg) > 512:
|
2013-10-16 12:41:56 -03:00
|
|
|
# posix guarantees that writes to a pipe of less than PIPE_BUF
|
|
|
|
# bytes are atomic, and that PIPE_BUF >= 512
|
2022-10-02 21:41:01 -03:00
|
|
|
raise ValueError('msg too long')
|
2013-10-16 12:41:56 -03:00
|
|
|
nbytes = os.write(self._fd, msg)
|
2017-08-29 19:52:18 -03:00
|
|
|
assert nbytes == len(msg), "nbytes {0:n} but len(msg) {1:n}".format(
|
|
|
|
nbytes, len(msg))
|
2013-10-16 12:41:56 -03:00
|
|
|
|
|
|
|
|
2019-05-10 17:59:08 -03:00
|
|
|
_resource_tracker = ResourceTracker()
|
|
|
|
ensure_running = _resource_tracker.ensure_running
|
|
|
|
register = _resource_tracker.register
|
|
|
|
unregister = _resource_tracker.unregister
|
|
|
|
getfd = _resource_tracker.getfd
|
2013-08-14 11:35:41 -03:00
|
|
|
|
|
|
|
def main(fd):
|
2019-05-10 17:59:08 -03:00
|
|
|
'''Run resource tracker.'''
|
2013-08-14 11:35:41 -03:00
|
|
|
# protect the process from ^C and "killall python" etc
|
|
|
|
signal.signal(signal.SIGINT, signal.SIG_IGN)
|
|
|
|
signal.signal(signal.SIGTERM, signal.SIG_IGN)
|
2018-09-04 05:53:54 -03:00
|
|
|
if _HAVE_SIGMASK:
|
|
|
|
signal.pthread_sigmask(signal.SIG_UNBLOCK, _IGNORED_SIGNALS)
|
2013-08-14 11:35:41 -03:00
|
|
|
|
|
|
|
for f in (sys.stdin, sys.stdout):
|
|
|
|
try:
|
|
|
|
f.close()
|
|
|
|
except Exception:
|
|
|
|
pass
|
|
|
|
|
2019-05-10 17:59:08 -03:00
|
|
|
cache = {rtype: set() for rtype in _CLEANUP_FUNCS.keys()}
|
2013-08-14 11:35:41 -03:00
|
|
|
try:
|
2019-05-10 17:59:08 -03:00
|
|
|
# keep track of registered/unregistered resources
|
2013-08-14 11:35:41 -03:00
|
|
|
with open(fd, 'rb') as f:
|
|
|
|
for line in f:
|
|
|
|
try:
|
2019-05-10 17:59:08 -03:00
|
|
|
cmd, name, rtype = line.strip().decode('ascii').split(':')
|
|
|
|
cleanup_func = _CLEANUP_FUNCS.get(rtype, None)
|
|
|
|
if cleanup_func is None:
|
|
|
|
raise ValueError(
|
|
|
|
f'Cannot register {name} for automatic cleanup: '
|
|
|
|
f'unknown resource type {rtype}')
|
|
|
|
|
|
|
|
if cmd == 'REGISTER':
|
|
|
|
cache[rtype].add(name)
|
|
|
|
elif cmd == 'UNREGISTER':
|
|
|
|
cache[rtype].remove(name)
|
|
|
|
elif cmd == 'PROBE':
|
2019-04-24 16:45:52 -03:00
|
|
|
pass
|
2013-08-14 11:35:41 -03:00
|
|
|
else:
|
|
|
|
raise RuntimeError('unrecognized command %r' % cmd)
|
|
|
|
except Exception:
|
|
|
|
try:
|
|
|
|
sys.excepthook(*sys.exc_info())
|
|
|
|
except:
|
|
|
|
pass
|
|
|
|
finally:
|
2019-05-10 17:59:08 -03:00
|
|
|
# all processes have terminated; cleanup any remaining resources
|
|
|
|
for rtype, rtype_cache in cache.items():
|
|
|
|
if rtype_cache:
|
2013-08-14 11:35:41 -03:00
|
|
|
try:
|
2019-05-10 17:59:08 -03:00
|
|
|
warnings.warn('resource_tracker: There appear to be %d '
|
|
|
|
'leaked %s objects to clean up at shutdown' %
|
|
|
|
(len(rtype_cache), rtype))
|
|
|
|
except Exception:
|
|
|
|
pass
|
|
|
|
for name in rtype_cache:
|
|
|
|
# For some reason the process which created and registered this
|
|
|
|
# resource has failed to unregister it. Presumably it has
|
|
|
|
# died. We therefore unlink it.
|
|
|
|
try:
|
|
|
|
try:
|
|
|
|
_CLEANUP_FUNCS[rtype](name)
|
|
|
|
except Exception as e:
|
|
|
|
warnings.warn('resource_tracker: %r: %s' % (name, e))
|
|
|
|
finally:
|
|
|
|
pass
|