Skip to content
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

Ensure workers get killed on unregister call #942

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ private CompletableFuture<HttpResponseStatus> shutdownServerThread(
Process workerProcess = lifecycle.getProcess();
if (workerProcess.isAlive()) {
boolean workerDestroyed = false;
workerProcess.destroyForcibly();
workerProcess.destroy(); // sends SIGTERM to workerProcess
try {
workerDestroyed =
workerProcess.waitFor(
Expand Down
21 changes: 21 additions & 0 deletions mms/model_service_worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,25 @@ def start_worker(self, cl_socket):
cl_socket.close()
sys.exit(0)

def server_worker_sigterm_handler(self, signum, frame):
# Frontend Process.destroy() sends sigterm
logging.info("PID=%s, received signal %s server worker", os.getpid(), signum)
for p in multiprocessing.active_children():
logging.info("PID=%s, Killing child %s", os.getpid(), p.pid)
p.terminate()

if self.sock_type == 'unix' and os.path.exists(self.sock_name):
os.remove(self.sock_name)

logging.info("PID=%s, Sending self kill signal", os.getpid())
os.kill(os.getpid(), 9)

def sigchld_handler(self, signum, frame):
# This is to handle zombie processes.
# Calling `active_children` has the side effect of `joining` any processes which have already finished
val = len(multiprocessing.active_children())


def run_server(self):
"""
Run the backend worker process and listen on a socket
Expand All @@ -200,6 +219,8 @@ def run_server(self):
logging.info("[PID] %d", os.getpid())
logging.info("MMS worker started.")
logging.info("Python runtime: %s", platform.python_version())
signal.signal(signal.SIGCHLD, self.sigchld_handler)
signal.signal(signal.SIGTERM, self.server_worker_sigterm_handler)
while True:
if self.service is None and self.preload is True:
# Lazy loading the models
Expand Down
8 changes: 5 additions & 3 deletions tests/performance/agents/metrics/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ def get_metrics(server_process, child_processes, logger):
"""
result = {}
children.update(child_processes)
logger.debug("children : {0}".format(",".join([str(c.pid) for c in children])))
logger.info("children : {0}".format(",".join([str(c.pid) for c in children])))

def update_metric(metric_name, proc_type, stats):
stats = list(filter(lambda x: isinstance(x, (float, int)), stats))
Expand Down Expand Up @@ -125,10 +125,12 @@ def update_metric(metric_name, proc_type, stats):
processes_stats.append({'type': ProcessType.WORKER, 'stats': child.as_dict()})
else:
reclaimed_pids.append(child)
logger.debug('child {0} no longer available'.format(child.pid))
logger.info('child {0} no longer available'.format(child.pid))
except (NoSuchProcess, ZombieProcess):
reclaimed_pids.append(child)
logger.debug('child {0} no longer available'.format(child.pid))
logger.info('child {0} no longer available'.format(child.pid))
except psutil.AccessDenied as e:
logger.info('child {0} threw access denied {1}'.format(child.pid, str(e)))

for p in reclaimed_pids:
children.remove(p)
Expand Down