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/frontend_app.py | 43 ++++++++++++++++++++++++++++------------ 1 file changed, 30 insertions(+), 13 deletions(-) (limited to 'src/leap/bitmask/frontend_app.py') diff --git a/src/leap/bitmask/frontend_app.py b/src/leap/bitmask/frontend_app.py index 1fe4cd0a..5dc42287 100644 --- a/src/leap/bitmask/frontend_app.py +++ b/src/leap/bitmask/frontend_app.py @@ -14,10 +14,13 @@ # # You should have received a copy of the GNU General Public License # along with this program. If not, see . +import multiprocessing import signal import sys import os +from functools import partial + from PySide import QtCore, QtGui from leap.bitmask.config import flags @@ -29,15 +32,20 @@ import logging logger = logging.getLogger(__name__) -def sigint_handler(*args, **kwargs): +def signal_handler(window, signum, frame): """ - Signal handler for SIGINT + Signal handler that quits the running app cleanly. + + :param window: a window with a `quit` callable + :type window: MainWindow + :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 = kwargs.get('logger', None) - if logger: - logger.debug("SIGINT catched. shutting down...") - mainwindow = args[0] - mainwindow.quit() + pname = multiprocessing.current_process().name + logger.debug("{0}: SIGNAL #{1} catched.".format(pname, signum)) + window.quit() def run_frontend(options, flags_dict): @@ -79,12 +87,21 @@ def run_frontend(options, flags_dict): qApp.setApplicationName("leap") qApp.setOrganizationDomain("leap.se") - MainWindow(start_hidden=start_hidden) - - # sigint_window = partial(sigint_handler, window, logger=logger) - # signal.signal(signal.SIGINT, sigint_window) - # Ensure that the application quits using CTRL-C - signal.signal(signal.SIGINT, signal.SIG_DFL) + # HACK: + # We need to do some 'python work' once in a while, otherwise, no python + # code will be called and the Qt event loop will prevent the signal + # handlers for SIGINT/SIGTERM to be called. + # see: http://stackoverflow.com/a/4939113/687989 + timer = QtCore.QTimer() + timer.start(500) # You may change this if you wish. + timer.timeout.connect(lambda: None) # Let the interpreter run each 500 ms. + + window = MainWindow(start_hidden=start_hidden) + + sigterm_handler = partial(signal_handler, window) + # ignore SIGINT since app.py takes care of signaling SIGTERM to us. + signal.signal(signal.SIGINT, signal.SIG_IGN) + signal.signal(signal.SIGTERM, sigterm_handler) sys.exit(qApp.exec_()) -- cgit v1.2.3