summaryrefslogtreecommitdiff
path: root/src/leap/bitmask/frontend_app.py
diff options
context:
space:
mode:
authorIvan Alejandro <ivanalejandro0@gmail.com>2014-07-02 16:43:58 -0300
committerIvan Alejandro <ivanalejandro0@gmail.com>2014-07-14 12:15:25 -0300
commitc3f485e194eb32939755178b11d472e1e69a94ad (patch)
treeaf773573bbc494bbc85c257e634441cb67d23f3c /src/leap/bitmask/frontend_app.py
parenteab69607ba4a65acf5c7745134d74917c76c6bf8 (diff)
Handle SIGINT/SIGTERM in processes.
Diffstat (limited to 'src/leap/bitmask/frontend_app.py')
-rw-r--r--src/leap/bitmask/frontend_app.py43
1 files changed, 30 insertions, 13 deletions
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 <http://www.gnu.org/licenses/>.
+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_())