From 3782a7def971268d2718346da425e46ac6bc27da Mon Sep 17 00:00:00 2001 From: jugglinmike Date: Fri, 19 Jul 2019 12:24:59 +0000 Subject: [PATCH] Bug 1559241 [wpt PR 17325] - [serve] Gracefully shut down on SIGTERM, a=testonly Automatic update from web-platform-tests [serve] Gracefully shut down on SIGTERM (#17325) -- wpt-commits: 2fdf0cbae4b8292b7edee9fb08d3d877bfe75e1d wpt-pr: 17325 --- .../web-platform/tests/tools/serve/serve.py | 30 +++++++++++-------- 1 file changed, 18 insertions(+), 12 deletions(-) diff --git a/testing/web-platform/tests/tools/serve/serve.py b/testing/web-platform/tests/tools/serve/serve.py index c50f4b484017..bf11d01ba104 100644 --- a/testing/web-platform/tests/tools/serve/serve.py +++ b/testing/web-platform/tests/tools/serve/serve.py @@ -8,6 +8,7 @@ import json import logging import os import platform +import signal import socket import sys import threading @@ -845,12 +846,18 @@ def get_parser(): def run(**kwargs): + received_signal = threading.Event() + with build_config(os.path.join(repo_root, "config.json"), **kwargs) as config: global logger logger = config.logger set_logger(logger) + def handle_signal(signum, frame): + logger.debug("Received signal %s. Shutting down.", signum) + received_signal.set() + bind_address = config["bind_address"] if kwargs.get("alias_file"): @@ -872,20 +879,19 @@ def run(**kwargs): with stash.StashServer(stash_address, authkey=str(uuid.uuid4())): servers = start(config, build_routes(config["aliases"]), **kwargs) + signal.signal(signal.SIGTERM, handle_signal) + signal.signal(signal.SIGINT, handle_signal) - try: - while all(item.is_alive() for item in iter_procs(servers)): - for item in iter_procs(servers): - item.join(1) - exited = [item for item in iter_procs(servers) if not item.is_alive()] - subject = "subprocess" if len(exited) == 1 else "subprocesses" - - logger.info("%s %s exited:" % (len(exited), subject)) - + while all(item.is_alive() for item in iter_procs(servers)) and not received_signal.is_set(): for item in iter_procs(servers): - logger.info("Status of %s:\t%s" % (item.name, "running" if item.is_alive() else "not running")) - except KeyboardInterrupt: - logger.info("Shutting down") + item.join(1) + exited = [item for item in iter_procs(servers) if not item.is_alive()] + subject = "subprocess" if len(exited) == 1 else "subprocesses" + + logger.info("%s %s exited:" % (len(exited), subject)) + + for item in iter_procs(servers): + logger.info("Status of %s:\t%s" % (item.name, "running" if item.is_alive() else "not running")) def main():