Handle SIGTERM/SIGINT gracefully.

This commit is contained in:
Florian Bruhin 2014-07-30 18:11:22 +02:00
parent 17220d8ef5
commit 19d0f832ad

View File

@ -351,8 +351,8 @@ class Application(QApplication):
exit status, and handles Ctrl+C properly by passing control to the exit status, and handles Ctrl+C properly by passing control to the
Python interpreter once all 500ms. Python interpreter once all 500ms.
""" """
signal.signal(signal.SIGINT, signal.signal(signal.SIGINT, self.interrupt)
lambda *args: self.exit(128 + signal.SIGINT)) signal.signal(signal.SIGTERM, self.interrupt)
timer = Timer(self, 'python_hacks') timer = Timer(self, 'python_hacks')
timer.start(500) timer.start(500)
timer.timeout.connect(lambda: None) timer.timeout.connect(lambda: None)
@ -646,17 +646,34 @@ class Application(QApplication):
self._crashdlg = ReportDialog(pages, history, widgets, objects) self._crashdlg = ReportDialog(pages, history, widgets, objects)
self._crashdlg.show() self._crashdlg.show()
def interrupt(self, signum, _frame):
"""Handler for signals to gracefully shutdown (SIGINT/SIGTERM)."""
log.destroy.info("SIGINT/SIGTERM received, shutting down!")
log.destroy.info("Press Ctrl-C again to forcefully quit.")
signal.signal(signal.SIGINT, self.interrupt_forcefully)
signal.signal(signal.SIGTERM, self.interrupt_forcefully)
self.shutdown(128 + signum)
def interrupt_forcefully(self, signum, frame):
"""Interrupt forcefully on the second SIGINT/SIGTERM request."""
log.destroy.info("Forceful quit requested, goodbye cruel world!")
print("Forceful quit requested, goodbye cruel world!")
self.exit(128 + signum)
@pyqtSlot() @pyqtSlot()
def shutdown(self): def shutdown(self, status=0):
"""Try to shutdown everything cleanly. """Try to shutdown everything cleanly.
For some reason lastWindowClosing sometimes seem to get emitted twice, For some reason lastWindowClosing sometimes seem to get emitted twice,
so we make sure we only run once here. so we make sure we only run once here.
Args:
status: The status code to exit with.
""" """
if self._shutting_down: if self._shutting_down:
return return
self._shutting_down = True self._shutting_down = True
log.destroy.debug("Shutting down...") log.destroy.debug("Shutting down with status {}...".format(status))
to_save = [] to_save = []
# Save everything # Save everything
if self.config.get('general', 'auto-save-config'): if self.config.get('general', 'auto-save-config'):
@ -678,4 +695,4 @@ class Application(QApplication):
self._destroy_crashlogfile() self._destroy_crashlogfile()
# If we don't kill our custom handler here we might get segfaults # If we don't kill our custom handler here we might get segfaults
qInstallMessageHandler(None) qInstallMessageHandler(None)
self.quit() self.exit(status)