diff --git a/CHANGELOG.md b/CHANGELOG.md index 3a4287894..19ddec1e1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -19,7 +19,7 @@ at anytime. * ### Fixed - * + * Fix restart procedure in DaemonControl * * diff --git a/lbrynet/lbrynet_daemon/DaemonControl.py b/lbrynet/lbrynet_daemon/DaemonControl.py index 8cc4158da..4dbd24411 100644 --- a/lbrynet/lbrynet_daemon/DaemonControl.py +++ b/lbrynet/lbrynet_daemon/DaemonControl.py @@ -131,9 +131,11 @@ def start_server_and_listen(launchui, use_auth, analytics_manager, max_tries=5): break except Exception as e: log.exception('Failed to startup') + yield daemon_server.stop() analytics_manager.send_server_startup_error(str(e)) tries += 1 else: + log.warn("Exceeded max tries to start up, stopping") reactor.callFromThread(reactor.stop) diff --git a/lbrynet/lbrynet_daemon/DaemonServer.py b/lbrynet/lbrynet_daemon/DaemonServer.py index a6a197da9..1af7045de 100644 --- a/lbrynet/lbrynet_daemon/DaemonServer.py +++ b/lbrynet/lbrynet_daemon/DaemonServer.py @@ -1,6 +1,5 @@ import logging import os -import sys from twisted.web import server, guard from twisted.internet import defer, reactor, error @@ -19,7 +18,9 @@ log = logging.getLogger(__name__) class DaemonServer(object): def __init__(self, analytics_manager=None): + self._api = None self.root = None + self.server_port = None self.analytics_manager = analytics_manager def _setup_server(self, use_auth): @@ -34,11 +35,11 @@ class DaemonServer(object): lbrynet_server.requestFactory = DaemonRequest try: - reactor.listenTCP( + self.server_port = reactor.listenTCP( conf.settings['api_port'], lbrynet_server, interface=conf.settings['api_host']) except error.CannotListenError: log.info('Daemon already running, exiting app') - sys.exit(1) + raise return defer.succeed(True) @@ -47,6 +48,12 @@ class DaemonServer(object): yield self._setup_server(use_auth) yield self._api.setup(launch_ui) + @defer.inlineCallbacks + def stop(self): + if self._api is not None: + yield self._api._shutdown() + if self.server_port is not None: + yield self.server_port.stopListening() def get_site_base(use_auth, root): if use_auth: