From 5500eca64474c61305965e082bb74d875c5b4276 Mon Sep 17 00:00:00 2001 From: Joshua Harlow Date: Wed, 30 Apr 2014 20:36:47 -0700 Subject: [PATCH] Handle exiting more gracefully Set all threads to daemon mode and close the write end of the pipe when exiting to ensure we cleanup in a friendly manner. Change-Id: I4cf64fa39e820c95d11943b848db87e5db15212e --- gertty/gertty.py | 15 +++++++++++++-- gertty/sync.py | 1 + 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/gertty/gertty.py b/gertty/gertty.py index 4f4a65f..407efdc 100644 --- a/gertty/gertty.py +++ b/gertty/gertty.py @@ -13,6 +13,7 @@ # under the License. import argparse +import contextlib import logging import os import sys @@ -121,15 +122,21 @@ class App(object): self.status.update(title=screen.title) self.loop = urwid.MainLoop(screen, palette=palette, unhandled_input=self.unhandledInput) - sync_pipe = self.loop.watch_pipe(self.refresh) + self.sync_pipe = self.loop.watch_pipe(self.refresh) #self.loop.screen.set_terminal_properties(colors=88) if not disable_sync: - self.sync_thread = threading.Thread(target=self.sync.run, args=(sync_pipe,)) + self.sync_thread = threading.Thread(target=self.sync.run, args=(self.sync_pipe,)) + self.sync_thread.daemon = True self.sync_thread.start() else: self.sync_thread = None + + def run(self): self.loop.run() + def close(self): + os.close(self.sync_pipe) + def changeScreen(self, widget): self.status.update(title=widget.title) self.screens.append(self.loop.widget) @@ -173,6 +180,8 @@ class App(object): self.backScreen() elif key == 'f1': self.help() + elif key in ('q', 'Q'): + raise urwid.ExitMainLoop() def getRepo(self, project_name): local_path = os.path.join(self.config.git_root, project_name) @@ -192,3 +201,5 @@ if __name__ == '__main__': help='the server to use (as specified in config file)') args = parser.parse_args() g = App(args.server, args.debug, args.no_sync) + with contextlib.closing(g): + g.run() diff --git a/gertty/sync.py b/gertty/sync.py index 59b9019..5c0095d 100644 --- a/gertty/sync.py +++ b/gertty/sync.py @@ -377,6 +377,7 @@ class Sync(object): self.submitTask(SyncSubscribedProjectsTask(HIGH_PRIORITY)) self.submitTask(UploadReviewsTask(HIGH_PRIORITY)) self.periodic_thread = threading.Thread(target=self.periodicSync) + self.periodic_thread.daemon = True self.periodic_thread.start() def periodicSync(self):