More robustly stop the reactor

Was getting a "cannot stop a reactor that isn't running error"
when playing around with error handling on startup.

Following the suggestion from http://stackoverflow.com/a/36738480
it seems to work better
This commit is contained in:
Job Evers-Meltzer 2016-11-02 07:23:37 -05:00
parent e5ba70c3ba
commit 815696485f
2 changed files with 8 additions and 6 deletions

View file

@ -225,10 +225,12 @@ class Daemon(AuthJSONRPCServer):
AuthJSONRPCServer.__init__(self, lbrynet_settings.use_auth_http) AuthJSONRPCServer.__init__(self, lbrynet_settings.use_auth_http)
reactor.addSystemEventTrigger('before', 'shutdown', self._shutdown) reactor.addSystemEventTrigger('before', 'shutdown', self._shutdown)
self.allowed_during_startup = ['is_running', 'is_first_run', self.allowed_during_startup = [
'get_time_behind_blockchain', 'stop', 'is_running', 'is_first_run',
'daemon_status', 'get_start_notice', 'get_time_behind_blockchain', 'stop',
'version', 'get_search_servers'] 'daemon_status', 'get_start_notice',
'version', 'get_search_servers'
]
last_version = {'last_version': {'lbrynet': lbrynet_version, 'lbryum': lbryum_version}} last_version = {'last_version': {'lbrynet': lbrynet_version, 'lbryum': lbryum_version}}
lbrynet_settings.update(last_version) lbrynet_settings.update(last_version)
self.db_dir = lbrynet_settings.data_dir self.db_dir = lbrynet_settings.data_dir

View file

@ -121,7 +121,7 @@ def update_settings_from_args(args):
def log_and_kill(failure): def log_and_kill(failure):
log_support.failure(failure, log, 'Failed to startup: %s') log_support.failure(failure, log, 'Failed to startup: %s')
reactor.stop() reactor.callFromThread(reactor.stop)
def start_server_and_listen(launchui, use_auth): def start_server_and_listen(launchui, use_auth):
@ -133,13 +133,13 @@ def start_server_and_listen(launchui, use_auth):
kwargs: passed along to `DaemonServer().start()` kwargs: passed along to `DaemonServer().start()`
""" """
lbry = DaemonServer() lbry = DaemonServer()
d = lbry.start() d = lbry.start()
d.addCallback(lambda _: listen(lbry, use_auth)) d.addCallback(lambda _: listen(lbry, use_auth))
if launchui: if launchui:
d.addCallback(lambda _: webbrowser.open(settings.UI_ADDRESS)) d.addCallback(lambda _: webbrowser.open(settings.UI_ADDRESS))
d.addErrback(log_and_kill) d.addErrback(log_and_kill)
def listen(lbry, use_auth): def listen(lbry, use_auth):
site_base = get_site_base(use_auth, lbry.root) site_base = get_site_base(use_auth, lbry.root)
lbrynet_server = server.Site(site_base) lbrynet_server = server.Site(site_base)