From c3f485e194eb32939755178b11d472e1e69a94ad Mon Sep 17 00:00:00 2001 From: Ivan Alejandro Date: Wed, 2 Jul 2014 16:43:58 -0300 Subject: Handle SIGINT/SIGTERM in processes. --- src/leap/bitmask/app.py | 36 ++++++++++++++++++++++++++++++++---- 1 file changed, 32 insertions(+), 4 deletions(-) (limited to 'src/leap/bitmask/app.py') diff --git a/src/leap/bitmask/app.py b/src/leap/bitmask/app.py index d1a2a111..fa244470 100644 --- a/src/leap/bitmask/app.py +++ b/src/leap/bitmask/app.py @@ -44,6 +44,8 @@ import os import signal import sys +from functools import partial + from leap.bitmask.backend.utils import generate_certificates from leap.bitmask import __version__ as VERSION @@ -109,12 +111,33 @@ def do_mail_plumbing(opts): # XXX catch when import is used w/o acct +def sigterm_handler(logger, gui_process, backend_process, signum, frame): + """ + Signal handler that quits the running app cleanly. + + :param logger: the configured logger object. + :type logger: logging.Logger + :param gui_process: the GUI process + :type gui_process: multiprocessing.Process + :param backend_process: the backend process + :type backend_process: multiprocessing.Process + :param signum: number of the signal received (e.g. SIGINT -> 2) + :type signum: int + :param frame: current stack frame + :type frame: frame or None + """ + logger.debug("SIGTERM catched, terminating processes.") + gui_process.terminate() + # Don't terminate the backend, the frontend takes care of that. + # backend_process.terminate() + + def start_app(): """ Starts the main event loop and launches the main window. """ - # Ensure that the application quits using CTRL-C - signal.signal(signal.SIGINT, signal.SIG_DFL) + # Ignore the signals since we handle them in the subprocesses + # signal.signal(signal.SIGINT, signal.SIG_IGN) # Parse arguments and store them opts = leap_argparse.get_options() @@ -181,13 +204,18 @@ def start_app(): flags_dict = flags_to_dict() app = lambda: run_frontend(options, flags_dict) - gui_process = multiprocessing.Process(target=app) + gui_process = multiprocessing.Process(target=app, name='Frontend') gui_process.start() backend = lambda: run_backend(opts.danger, flags_dict) - backend_process = multiprocessing.Process(target=backend) + backend_process = multiprocessing.Process(target=backend, name='Backend') backend_process.start() + handle_sigterm = partial(sigterm_handler, logger, + gui_process, backend_process) + signal.signal(signal.SIGTERM, handle_sigterm) + signal.signal(signal.SIGINT, handle_sigterm) + if __name__ == "__main__": start_app() -- cgit v1.2.3